Repository navigation
fix: parse application/*+json request bodies as JSON - #811
Open
fadhlaouir wants to merge 1 commit into
Open
fadhlaouir wants to merge 1 commit into
fadhlaouir wants to merge 1 commit into
Conversation
The default JSON parser was registered without a type, so body-parser matched only application/json. Bodies sent with a structured syntax suffix type, such as application/vnd.api+json (required by JSON:API) or application/merge-patch+json, fell through to the raw parser and reached the function as a Buffer. Give the JSON parser its own options with type ['application/json', 'application/*+json']. The CloudEvents parser still runs first for application/cloudevents+json, and the text parser keeps its existing options. Fixes GoogleCloudPlatform#809
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 #809
Problem
HTTP functions receive request bodies sent with a structured syntax suffix type, such as
application/vnd.api+json(required by JSON:API) orapplication/merge-patch+json, as a rawBuffer, while the same body sent asapplication/jsonarrives parsed. A+jsonsuffix means the payload is JSON (RFC 6839 §3.1).The default JSON parser is registered without a
type, so body-parser only matchesapplication/json, and these requests fall through to the catch-all raw parser.Change
The JSON parser now gets its own options with
type: ['application/json', 'application/*+json'].defaultBodySavingOptionsis left unchanged, because the text parser also uses it.application/cloudevents+jsonis handled as before.+jsonbody now gets a 400 response, as a malformedapplication/jsonbody already does. Before, it reached the function as a rawBuffer.Testing
test/integration/http.tsforapplication/vnd.api+jsonandapplication/merge-patch+json. Both fail without the change and pass with it.npm test: 109 passing (107 before, plus the 2 new tests).application/*+jsonbodies (e.g. JSON:APIapplication/vnd.api+json) as a raw Buffer #809: both+jsonrequests now arrive parsed.application/json,application/cloudevents+json,text/plainandapplication/octet-streambehave as before.