fix(api-keys): implement write-time cycle detection for replacedById …#379
Open
yunus-dev-codecrafter wants to merge 1 commit into
Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #260
Summary
Add
assertNoCycleguard to prevent circularApiKey.replacedByself-relation chains at write time. Therotatemethod inApiKeysServicepreviously had no cycle detection — if any code path setreplacedByIdto an existing ancestor key (e.g., A→B→A), the chain-walking code would loop infinitely.Closes #260
Testing
TypeScript compilation passes with no errors:
Unit tests added in
api-keys.service.spec.tsfor theassertNoCyclefunction:rejects a 2-node cycleA.replacedById = B; callingassertNoCycle('B', 'A')→ throwsBadRequestExceptionpasses when no cycle existsB.replacedById = null; callingassertNoCycle('A', 'B')→ resolvesrejects a chain that already contains a cycleX→Y→Z→X(pre-existing); callingassertNoCycle('newKey', 'X')→ throwsBadRequestExceptionthrows NotFound if a key in the chain is missingNotFoundExceptionTo run the tests:
(Note: Requires
jestv29+ andts-jestv29+ to be properly installed.)Screenshots
N/A
Checklist
Files changed
src/api-keys/cycle-check.ts(new) —assertNoCycleguard: walks thereplacedBychain from the candidate replacement, tracking visited IDs in aSet. ThrowsBadRequestException(CyclicRotation) if the original key or a duplicate is encountered.src/api-keys/api-keys.service.ts— imports and callsassertNoCycle(existing.id, replacement.id, tx)in therotatemethod after creating the replacement key but before updating the old key.src/api-keys/api-keys.service.spec.ts— 4 new test cases covering cycle rejection, happy path, pre-existing cycle, and missing key.