You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(api): remove a secret only at the version that was read - #357
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]>
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.
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.
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: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
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
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
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
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #349. Raised by the security audit of 19 November (finding 8).
What changed
PrismaSecretStore.removedeletes withversionequal to the one it read, asputalready updates, and treats anything but one deleted row asConflictError('secret_changed')(409). The command runs inside the tenant transaction, so the conflict rolls back with it and nosecret.removedevent is recorded.Why
Removal read the secret, then deleted it by id. With two authorized accounts:
How it was tested
Two integration tests on a real PostgreSQL, through the HTTP API with two different accounts (
ownerandenvironmentAdmin). 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:secret.removedevent is added;secret.removedevent 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:lintandapi:typecheckpass.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