From 1918b9b525c40dce416988a22854fcca3dcc94a8 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:50:52 -0300 Subject: [PATCH] fix: handle concurrent first module configuration writes Refs #11 Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- lib/Service/ModuleService.php | 39 ++++++++++++------ tests/ci/smoke.sh | 2 +- tests/integration/module-toggle-race.php | 50 ++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 tests/integration/module-toggle-race.php diff --git a/lib/Service/ModuleService.php b/lib/Service/ModuleService.php index 481a00c..b40f72b 100644 --- a/lib/Service/ModuleService.php +++ b/lib/Service/ModuleService.php @@ -129,18 +129,27 @@ public function toggle(int $spaceId, string $id, bool $enabled): array { if (!isset($this->discover()[$id])) { throw new DomainException('module_not_found'); } - $q = $this->db->getQueryBuilder(); - $q->select('id')->from('lf_module_flags') - ->where($q->expr()->eq('space_id', $q->createNamedParameter($spaceId, IQueryBuilder::PARAM_INT))) - ->andWhere($q->expr()->eq('module_id', $q->createNamedParameter($id))); - $existing = $q->executeQuery()->fetchOne(); - if ($existing === false) { - $this->write->insert('lf_module_flags', [ - 'space_id' => $spaceId, 'module_id' => $id, 'enabled' => $enabled ? 1 : 0, - ]); + $key = ['space_id' => $spaceId, 'module_id' => $id]; + $values = ['enabled' => $enabled ? 1 : 0]; + if ($this->flagExists($spaceId, $id)) { + $this->write->update('lf_module_flags', $values, $key); } else { - $this->write->update('lf_module_flags', - ['enabled' => $enabled ? 1 : 0], ['space_id' => $spaceId, 'module_id' => $id]); + try { + $this->write->insert('lf_module_flags', $key + $values); + } catch (\OCP\DB\Exception $e) { + // Another request may have created the same unique flag after + // our read. Only recover when the precise scoped row exists; + // preserve unrelated database errors and recheck authority. + if ($e->getReason() !== \OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION + || !$this->flagExists($spaceId, $id)) { + throw $e; + } + $this->access->requireFinancialCapability($spaceId, 'modules.configure'); + if (!isset($this->discover()[$id])) { + throw new DomainException('module_not_found'); + } + $this->write->update('lf_module_flags', $values, $key); + } } // Return the effective state, not a misleading 'enabled' response // when an ancestor policy still prohibits the module. @@ -149,6 +158,14 @@ public function toggle(int $spaceId, string $id, bool $enabled): array { 'blockedByPolicy' => $blockedByPolicy]; } + private function flagExists(int $spaceId, string $id): bool { + $q = $this->db->getQueryBuilder(); + $q->select('id')->from('lf_module_flags') + ->where($q->expr()->eq('space_id', $q->createNamedParameter($spaceId, IQueryBuilder::PARAM_INT))) + ->andWhere($q->expr()->eq('module_id', $q->createNamedParameter($id))); + return $q->executeQuery()->fetchOne() !== false; + } + /** Does not grant access or reveal a parent's confidential settings. */ private function blockedByPolicy(int $spaceId, string $id): bool { $policy = match ($id) { diff --git a/tests/ci/smoke.sh b/tests/ci/smoke.sh index 64ba7b1..9eaa1c6 100644 --- a/tests/ci/smoke.sh +++ b/tests/ci/smoke.sh @@ -311,7 +311,7 @@ assert_integration_complete() { if [ "$(printenv LF_BROWSER_DEBUG_ONLY || echo 0)" != 1 ]; then # Run native PHP integration suites inside the same Nextcloud PHP runtime. -for script in basic personal-finance agenda-search-sort spaces advanced governance catalog categories companies custom-fields field-studio-v3 ux-enhancements company-logo bank-identity payment-methods currency-rate-history; do +for script in basic personal-finance agenda-search-sort spaces advanced governance catalog categories companies custom-fields field-studio-v3 ux-enhancements company-logo bank-identity payment-methods currency-rate-history module-toggle-race; do assert_integration_complete "/var/www/html/custom_apps/librefinance/tests/integration/$script.php" done echo "PASS: Native PHP authorization, ledger, group access and concurrency tests on Nextcloud Server master." diff --git a/tests/integration/module-toggle-race.php b/tests/integration/module-toggle-race.php new file mode 100644 index 0000000..28d049a --- /dev/null +++ b/tests/integration/module-toggle-race.php @@ -0,0 +1,50 @@ + $t->check('create isolated module configuration space', $a, + 'POST', '/spaces', ['name' => $name, 'kind' => 'collective', 'currency' => 'BRL'], 201); +$control = $create('Synthetic module isolation control'); +$controlPath = '/spaces/' . (int)$control['id']; + +$hasModule = static fn(bool $enabled): callable => static fn(array $r): bool => + count(array_filter($r['modules'] ?? [], static fn(array $m): bool => + $m['id'] === $report && $m['enabled'] === $enabled)) === 1; + +for ($i = 1; $i <= 12; ++$i) { + $space = $create('Synthetic module first-write race ' . $i); + $path = '/spaces/' . (int)$space['id']; + $t->check('grant independent manager', $a, 'POST', $path . '/members', + ['uid' => $b, 'role' => 'manager'], 200); + $t->check('module starts enabled without a configuration row', $a, 'GET', + $path . '/modules', null, 200, $hasModule(true)); + $route = $path . '/modules/' . $report; + $request = ['method' => 'PUT', 'path' => $route, 'data' => ['enabled' => false]]; + [$first, $second] = $t->parallel(['user' => $a, ...$request], ['user' => $b, ...$request]); + $t->record('concurrent first disables both succeed #' . $i, + $first['code'] === 200 && $second['code'] === 200 + && ($first['body']['enabled'] ?? null) === false + && ($second['body']['enabled'] ?? null) === false, + 'HTTP ' . $first['code'] . '/' . $second['code'], [$first, $second]); + $t->check('both authorized readers observe the disabled module', $b, 'GET', + $path . '/modules', null, 200, $hasModule(false)); + $t->check('report access denied after concurrent disable', $a, 'GET', + $path . '/trial-balance?through=2026-12-31', null, 403, + static fn(array $r): bool => ($r['error'] ?? null) === 'module_disabled'); + $t->check('owner can re-enable the existing flag', $a, 'PUT', $route, + ['enabled' => true], 200, static fn(array $r): bool => ($r['enabled'] ?? null) === true); + $t->check('other workspace remains unaffected', $a, 'GET', + $controlPath . '/modules', null, 200, $hasModule(true)); +} +$t->finish();