Repository navigation
Conversation
3 tasks done
Author
|
@LabsCrypt this PR's CI is green now. Before: Root cause & fix Head |
This branch has not been deployed
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.
Overview
X-Request-IDwas only partially propagated:requestIdMiddlewareset the header but was mounted after the global rate limiter, so any response short-circuited by earlier middleware (the rate limiter's 429, the CORS 403) came back without the header, and the JSON error bodies produced bysendApiErrornever carried the id at all — so a client could not quote the request id from a failed call. This PR makes the id available to every response and every error payload.Related Issue
Changes
requestIdMiddlewareas the very first middleware inbackend/src/app.ts(beforeglobalRateLimiter), so even short-circuited responses carryX-Request-ID. Inbound values keep the existing length/charset sanitisation.AsyncLocalStorageintobackend/src/lib/request-context.tsand exposegetRequestId();logger.tsre-exports it so existing importers are unaffected, and the winston JSON format keeps appendingrequestId.backend/src/types/api-error.ts:sendApiErrornow attachesrequestIdto every error body from the request context.backend/src/middleware/requestId.ts: also expose the id asreq.idfor downstream handlers.backend/src/middleware/rate-limiter.middleware.ts: default 429 handler preserves the configured message and stampsrequestId.backend/src/app.ts: the CORS 403 body now includesrequestIdalongside the existingerrorstring (existing consumers unaffected).backend/tests/requestId.test.tsandbackend/tests/error.middleware.test.tswith propagation coverage, including a real-app supertest case for the pre-router CORS 403 and for asendApiError400.Verification Results
requestIdMiddlewareis now the first middleware; supertest covers a normal response, a client-supplied id, asendApiError400 and a pre-router CORS 403.sendApiErrorreads the id from the request context; the 429 handler and the CORS 403 body stamp it too — covered bytests/requestId.test.tsandtests/error.middleware.test.ts.requestContext.runstill wraps each request (verified in the captured log lines above).Closes #1494