Skip to content

fix(api): remove a secret only at the version that was read - #357

Merged
samuelmbabhazi merged 2 commits into
mainfrom
fix/349-secret-removal-version
Nov 19, 2026
Merged

samuelmbabhazi merged 2 commits into
mainfrom
fix/349-secret-removal-version

Conversation

@samuelmbabhazi

@samuelmbabhazi samuelmbabhazi commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #349. Raised by the security audit of 19 November (finding 8).

What changed

PrismaSecretStore.remove deletes with version equal to the one it read, as put already updates, and treats anything but one deleted row as ConflictError('secret_changed') (409). The command runs inside the tenant transaction, so the conflict rolls back with it and no secret.removed event is recorded.

Why

Removal read the secret, then deleted it by id. With two authorized accounts:

  • a rotation committed between the read and the delete was deleted, while the audit event named the version that was read;
  • two concurrent removals both reported success, with two audit events for one deletion.

How it was tested

Two integration tests on a real PostgreSQL, through the HTTP API with two different accounts (owner and environmentAdmin). The removal is paused right before its delete, after reading version 1, and the other request runs to completion on its own connections in that pause:

  • a rotation to version 2 commits, then the removal resumes: 409, version 2 is still listed, and no secret.removed event is added;
  • a second removal commits, then the first resumes: one 204 and one 409, the secret is gone, and exactly one secret.removed event is added.

With the previous store, the same tests fail on PostgreSQL: the first removal answers 204 (it deleted version 2), and both concurrent removals answer 204.

nx run api:lint and api:typecheck pass.

Security considerations

The audit trail now records the version that was actually deleted, and a removal cannot erase a value written after the remover looked at it. The atomicity of the change and its audit event is unchanged.

Summary by CodeRabbit

  • Bug Fixes
    • Secret removal now detects when a secret has changed or another removal has already completed, returning a conflict instead of reporting success.
    • When a secret is rotated during removal, the newer version is preserved.

Removal read the secret, then deleted it by id: a rotation committed in between was deleted while the audit event named the old version, and two concurrent removals both succeeded with two events. The delete now carries the version it read, like the update path, and anything but one deleted row is a conflict, so the command and its audit event roll back together.

Closes #349

Signed-off-by: samuel mbabhazi <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 566eda8 and 090ab5f.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Secret removal now deletes only the version it read. If that version changed or another request already removed it, the operation returns a conflict. Integration tests cover both race conditions and verify the resulting secret state and removal audit events.

Changes: Secret removal

Layer / File(s) Summary
Version-checked deletion
apps/api/src/modules/secrets/infrastructure/prisma-secret.store.ts
The store includes the read version in its deletion condition. It throws a conflict unless exactly one row is deleted.
Rotation and concurrent-removal coverage
apps/api/src/modules/secrets/secrets-http.integration.spec.ts
Integration tests verify that a rotation during removal preserves the newer version and adds no removal audit event. Concurrent removals produce one successful response, one conflict, and one removal audit event.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · previous review 566ed

Secret removal now deletes only the version it read and returns a conflict if the secret changed or was already removed. Integration tests cover both race scenarios. No merge-blocking risk was identified.

Security architecture risk: Minimal · up to 566ed

Secret removal is restricted to the version read, while existing access restrictions remain intact. No introduced or worsened design-level security risk was identified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed destructive operation remains scoped to the previously read secret row within the authorized organization and environment. Adding the version condition narrows deletion eligibility; it introduces no additional route, secret-read capability, or cross-tenant authority.

Security Findings and Attack Paths

  • inferred — The prior authorized-writer race could delete a newly rotated value or report two successful removals for one deletion. The changed predicate and count check reject those outcomes rather than widening their exposure. No introduced or worsened attack path was identified in the inspected flow.

Trust Boundaries and Controls

  • observed — Caller-selected path identifiers are converted into the secret target, while caller identity comes from resourceContext. The route declares secret.write access, the command invokes account authority and tenant permission checks, and the store retains organization scoping in both lookup and deletion.

Resilience and Maintainability Implications

  • inferred — A repeated removal after success remains a not-found result without another audit event. Deleting and recreating the same name does not make an in-flight removal target the replacement, because insertion assigns a new row identifier and removal matches the original identifier as well as its version.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the main change: PrismaSecretStore.remove now deletes only the version that was read, matching the linked issue's conditional-removal objective. The title is specific and matches the …
Linked Issues check Passed Issue #349 coding requirements are met at the reviewed head.

[#349, criterion 1: version condition and row count check] The diff in PrismaSecretStore.remove adds version: existing.version to the …
Out of Scope Changes check Passed The code change is limited to PrismaSecretStore.remove (+6/-2 lines). It adds the version condition, the affected-row check, and the conflict error, which implement #349. The integration test additi…
Full details: Title check

Explanation

The title states the main change: PrismaSecretStore.remove now deletes only the version that was read, matching the linked issue's conditional-removal objective. The title is specific and matches the raw summary, which describes a versioned delete that throws ConflictError('secret_changed') when no row matches. The conventional-commit prefix 'fix(api)' fits the scope.

Full details: Linked Issues check

Explanation

Issue #349 coding requirements are met at the reviewed head.

[#349, criterion 1: version condition and row count check] The diff in PrismaSecretStore.remove adds version: existing.version to the deleteMany where clause. It throws ConflictError('secret_changed') when deleted.count !== 1. This is the version compare-and-swap that put already uses.

[#349, criterion 2: rotation during pause] If a rotation to v2 commits after the read of v1, the delete matches zero rows. The store throws a conflict. The command transaction rolls back, so no secret.removed event is recorded. The PR summary and the integration test helper in secrets-http.integration.spec.ts describe this scenario. I did not inspect the test file itself.

[#349, criterion 3: concurrent removals] A second concurrent removal that deletes the row first causes the other delete to match zero rows. Exactly one 204 and one audit event result. The PR summary describes this test. I did not inspect the test file itself.

[#349, criterion 4: atomicity] The diff does not change the transaction or audit code. The thrown error is raised inside the existing command flow. The PR states that atomicity is unchanged. I did not verify the enclosing transaction code in this review, so this criterion relies on the PR description and existing code.

Full details: Out of Scope Changes check

Explanation

The code change is limited to PrismaSecretStore.remove (+6/-2 lines). It adds the version condition, the affected-row check, and the conflict error, which implement #349. The integration test additions in secrets-http.integration.spec.ts (+76 lines, per the summary) cover the #349 scenarios and support the change. No unrelated file changes appear in the reviewed summary.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 5357bb9 and 566eda8.

Files selected for processing: 2

  • apps/api/src/modules/secrets/infrastructure/prisma-secret.store.ts
  • apps/api/src/modules/secrets/secrets-http.integration.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

@samuelmbabhazi
samuelmbabhazi merged commit 31e7d67 into main Nov 19, 2026
22 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the fix/349-secret-removal-version branch November 19, 2026 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make secret removal conditional on the version read and audit only the actual deletion

1 participant