From aef9640b5033745d9d41212270dcf90e09b04ada Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sat, 24 Oct 2026 16:04:45 -0500 Subject: [PATCH] fix(http): parse every +json media type as JSON (GitHub #36) Exact matching left `application/json-patch+json` arriving as text, where the old substring check had parsed it, and still left the other `+json` types (`application/problem+json`, `application/vnd.api+json`) as text, as they always were. `+json` is the structured-syntax suffix for JSON (RFC 6839), so any media type ending in it is parsed as JSON. `application/jsonx` and parameters that only mention JSON still are not. Signed-off-by: Diego Resendez --- CHANGELOG.md | 6 +++--- src/http/body.ts | 4 +++- test/http.test.ts | 3 +++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c993bed..fb7b9b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -264,9 +264,9 @@ npm treats versions as semver and semver forbids leading zeros. the raw `Content-Type` header, case-sensitively, so `Content-Type: Application/JSON` reached the handler as a string while `text/plain; x=application/json` was parsed as JSON. The same applied to urlencoded and multipart bodies. The parser now compares the media type, the part before any - `;`, trimmed and lowercased, exactly. **A type that only contains one of the three no longer - selects its parser:** `application/json-patch+json` used to be parsed as JSON and now arrives as - text, like any other `+json` type always has. Parse it in the handler. + `;`, trimmed and lowercased, exactly. Any `+json` type (RFC 6839) is parsed as JSON too: + `application/problem+json` and `application/vnd.api+json`, which used to arrive as text, now + arrive parsed, and `application/json-patch+json`, which matched the old substring, still does. - **`listen()` rejects when the port is taken** (#138). A bind failure such as `EADDRINUSE` or `EACCES` arrives as the server's `error` event, which `listen()` did not handle: the promise diff --git a/src/http/body.ts b/src/http/body.ts index c1f7f9d..6516223 100644 --- a/src/http/body.ts +++ b/src/http/body.ts @@ -74,7 +74,9 @@ export async function parseRequestBody( ): Promise { const type = mediaType(contentType); - if (buf !== undefined && type === 'application/json') { + // `+json` is the structured-syntax suffix for JSON (RFC 6839): `application/problem+json`, + // `application/vnd.api+json` and `application/json-patch+json` are JSON documents too. + if (buf !== undefined && (type === 'application/json' || type.endsWith('+json'))) { try { return { body: JSON.parse(buf.toString('utf8')) }; } catch { diff --git a/test/http.test.ts b/test/http.test.ts index 0225be5..42a11c1 100644 --- a/test/http.test.ts +++ b/test/http.test.ts @@ -394,6 +394,9 @@ describe('request hardening', () => { expect(await post('Text/Plain; x=application/json', '{"a":1}')).toEqual({ got: '{"a":1}' }); expect(await post('Application/JSON; charset=utf-8', '{"a":1}')).toEqual({ got: { a: 1 } }); + expect(await post('application/vnd.api+json', '{"a":1}')).toEqual({ got: { a: 1 } }); + expect(await post('application/json-patch+json', '[]')).toEqual({ got: [] }); + expect(await post('application/jsonx', '{"a":1}')).toEqual({ got: '{"a":1}' }); expect(await post('APPLICATION/X-WWW-FORM-URLENCODED', 'a=1')).toEqual({ got: { a: '1' } }); expect(await post('text/plain; x=application/x-www-form-urlencoded', 'a=1')).toEqual({ got: 'a=1' }); server.close();