fix: prevent duplicate IDs on POST /:name#1733
Open
jara505 wants to merge 1 commit intotypicode:mainfrom
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.
Fix: Prevent Duplicate ID Injection via POST
Overview
Resolves a data integrity vulnerability where
POST /:nameaccepted client-suppliedidvalues and did not enforce uniqueness, allowing duplicate IDs to be inserted into collections. This caused ambiguous and potentially destructive behavior in item-level operations such asGET,PATCH, andDELETE.Problem
As described in #1732, the
create()method insrc/service.tsconstructed items as:Because
...datacame afterid: randomId(), a client-suppliedidin the request body would override the generated one. There was no uniqueness check before insertion.Reproduction
With a collection containing
{ id: "victim", title: "original" }, sending:Results in two records sharing the same ID:
[ { "id": "victim", "title": "original" }, { "id": "victim", "title": "attacker" } ]After that,
GET /posts/victimandPATCH /posts/victimoperate on the first match only, andDELETE /posts/victimremoves one record while leaving the other behind.Solution
Three changes were implemented:
id— Destructure and discard anyidfrom the client payload before creating the item.randomBytes(2)(65,536 possible values) torandomBytes(8)(~3.4 × 10³⁸ possible values) to eliminate random collisions under high insert volume.Key Changes
src/service.tscreate()to strip clientidand add a uniqueness verification loop before insertion.src/random-id.tssrc/service.test.tsidrejection, duplicate ID prevention, and normalPOSTbehavior.Breaking Change
Clients that previously sent
idinPOSTbodies will now have those values ignored. This is consistent with the documented behavior where IDs are auto-generated strings.