fix(http): choose the body parser by the media type (GitHub #36)
parseRequestBody picked JSON, urlencoded or multipart with contentType.includes() on the raw header. That is case-sensitive, so a valid `Application/JSON` reached the handler as a string, and it matches anywhere in the header, so a parameter could choose the parser: `text/plain; x=application/json` was parsed as JSON. The media type is now taken from the part before any `;`, trimmed and lowercased (RFC 9110 §8.3.1), and compared exactly. The boundary is still read from the full header. A type that merely 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, as other `+json` types always have. CHANGELOG says so. test/http.test.ts sends both cases from the issue, plus their urlencoded equivalents, through the Node server. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+42
-3
No files matched your search
+15
-3
@@ -49,6 +49,16 @@ export function declaresOverLimit(contentLength: string | null | undefined, limi
|
||||
return contentLength != null && /^\d+$/.test(contentLength) && Number(contentLength) > limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* The media type of a Content-Type value: the part before any parameters, trimmed and lowercased,
|
||||
* since media types are case-insensitive (RFC 9110 §8.3.1). Matching a substring of the raw header
|
||||
* instead let a parameter choose the parser: `text/plain; x=application/json` was parsed as JSON.
|
||||
*/
|
||||
function mediaType(contentType: string): string {
|
||||
const semi = contentType.indexOf(';');
|
||||
return (semi === -1 ? contentType : contentType.slice(0, semi)).trim().toLowerCase();
|
||||
}
|
||||
|
||||
/** Parse outcome: the decoded body, or an error message plus the status to send (400 unless set). */
|
||||
type ParseResult = { body: unknown } | { error: string; status?: number };
|
||||
|
||||
@@ -62,7 +72,9 @@ export async function parseRequestBody(
|
||||
duplicates: 'array' | 'last',
|
||||
maxParts: number,
|
||||
): Promise<ParseResult> {
|
||||
if (buf !== undefined && contentType.includes('application/json')) {
|
||||
const type = mediaType(contentType);
|
||||
|
||||
if (buf !== undefined && type === 'application/json') {
|
||||
try {
|
||||
return { body: JSON.parse(buf.toString('utf8')) };
|
||||
} catch {
|
||||
@@ -70,11 +82,11 @@ export async function parseRequestBody(
|
||||
}
|
||||
}
|
||||
|
||||
if (buf !== undefined && contentType.includes('application/x-www-form-urlencoded')) {
|
||||
if (buf !== undefined && type === 'application/x-www-form-urlencoded') {
|
||||
return { body: collapseDuplicates(new URLSearchParams(buf.toString('utf8')), duplicates) };
|
||||
}
|
||||
|
||||
if (buf !== undefined && contentType.includes('multipart/form-data')) {
|
||||
if (buf !== undefined && type === 'multipart/form-data') {
|
||||
const boundary = extractBoundary(contentType);
|
||||
if (!boundary) return { error: 'Invalid multipart body' };
|
||||
|
||||
|
||||
Reference in new issue
Block a user