Security hardening: response replay, in-flight locks, cache timeout, and doc accuracy#1
Merged
Merged
Conversation
- Add per-key in-flight locks to prevent concurrent duplicate processing - Store and replay original status/headers/body on cache hits - Scope cache keys with HTTP method + URL + idempotency key - Validate idempotency keys (max 128 chars, safe charset) - Catch extractor errors gracefully - Implement keyPrefix and maxResponseSize options - Cap TTL at 24 hours and validate cache value shape - Update vulnerable dependencies (npm audit fix) - Harden CI workflow (checkout/setup-node v4, npm ci, read-only lint) - Update demos and README with secure usage patterns - Expand tests for race conditions, key validation, and replay
- Add tests for all validation branches and edge cases - Cover buffer serialization/deserialization and hop-by-hop header filtering - Cover concurrent deduplication with three simultaneous requests - Cover req.originalUrl fallback and falsy body edge cases - Remove unreachable defensive fallbacks in buildCacheKey - 43/43 tests passing with 100% statement/branch/function/line coverage
- Fix in-flight lock leak on response close/abort - Add cacheTimeout option with validation and timeout wrapping - Prevent unknown body types from bypassing maxResponseSize - Guard replay when response headers already sent - Improve test mock to mirror on-http-end payload; add coverage for new behavior - Align README, demos, and TypeScript declarations with implementation
e411f0f to
0cb9606
Compare
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.
What changed
This PR addresses the findings from a security-focused code review of the idempotency middleware and brings the docs/demos in line with the actual implementation.
Security & reliability fixes
res.closeif the client aborts or the response is destroyed beforeres.end()fires, preventing a per-key denial-of-service.cacheTimeoutoption (default5000ms) so a hanging cache backend cannot hold a lock forever.maxResponseSizebypass: unknown/non-string/non-buffer body types are no longer cached whenmaxResponseSizeis configured.headersSentguard: replay aborts gracefully when headers were already sent by an earlier middleware.Docs & examples
USER_IDin the demos with a realisticgetCurrentUserId(req)helper.cacheTimeoutoption.Tests
on-http-end's payload shape (includingheaders).cacheTimeoutvalidation, cache timeout lock release, unknown body types,headersSentreplay abort, and close-event lock cleanup.Verification
No breaking public API changes were introduced. The only new option,
cacheTimeout, is optional and defaults to the previous effective wait behavior with an explicit upper bound.