fix(http): parse body before guards and return parsed body synchronously#208
Open
nicoske wants to merge 1 commit into
Open
fix(http): parse body before guards and return parsed body synchronously#208nicoske wants to merge 1 commit into
nicoske wants to merge 1 commit into
Conversation
Guards ran before body parsing, and request.body kept returning a Promise even after the pipeline had parsed the body. Any guard that reads request.body (the usual shape of an auth guard that binds a token to payload fields) saw a pending Promise instead of the payload, and the same happened to schema validators that read request.body without awaiting (e.g. Nestia's @TypedBody). On Express none of this is an issue because body-parser middleware runs ahead of the guards. Parse the body before guard execution and store it on the request, and make the body getter return the parsed value directly once it is available. Awaiting request.body keeps working in both states, and pipes overwrite the stored body as before. Streaming content types (multipart, octet-stream) are untouched. The trade-off is that a request rejected by a guard now pays the body parse cost first, which is the same behavior as Express with global body-parser middleware.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Fixes #206
Moves body parsing ahead of guard execution in the route registry and stores the parsed body on the request right away, not only when pipes run. The
bodygetter returns the parsed value synchronously once it exists; before parsing it still returns a Promise, andawait req.bodybehaves the same in both states. Pipes keep overwriting the stored body as before, and streaming content types (multipart, octet-stream) are still skipped.Motivation: guards commonly authorize against payload fields, and libraries like Nestia's
@TypedBodyreadrequest.bodywithout awaiting. Both get a pending Promise on this platform while working fine on Express.The declared getter type changes from
Promise<unknown>tounknown. The only pattern that could notice is calling.then()on it directly instead of awaiting, which nothing in the code base or docs does.Malformed JSON keeps producing the same response it did before the reorder; the new e2e pins that down.
New e2e suite:
test/http/guards-body-access.e2e.spec.ts(guard reads the body synchronously, allow/deny based on payload, synchronous read in the handler, malformed JSON). Lint, unit (975) and e2e (239) suites are green on Node 24.