Fixes for incorrect parsing of FormData

This commit is contained in:
Jesse Wierzbinski 2024-04-14 02:37:37 -10:00
parent f8309ca3c1
commit 5d4af4adbd
No known key found for this signature in database

View file

@ -46,10 +46,18 @@ export class RequestParser {
* @private * @private
*/ */
private async determineContentType() { private async determineContentType() {
if (this.request.headers.get("Content-Type")) { const content_type = this.request.headers.get("Content-Type");
return (
this.request.headers.get("Content-Type")?.split(";")[0] ?? "" if (content_type?.startsWith("application/json")) {
); return "application/json";
}
if (content_type?.startsWith("application/x-www-form-urlencoded")) {
return "application/x-www-form-urlencoded";
}
if (content_type?.startsWith("multipart/form-data")) {
return "multipart/form-data";
} }
// Check if body is valid JSON // Check if body is valid JSON
@ -68,6 +76,10 @@ export class RequestParser {
// This is not FormData // This is not FormData
} }
if (content_type) {
return content_type.split(";")[0] ?? "";
}
if (this.request.body) { if (this.request.body) {
throw new Error("Invalid body"); throw new Error("Invalid body");
} }
@ -87,7 +99,7 @@ export class RequestParser {
const result: Partial<T> = {}; const result: Partial<T> = {};
for (const [key, value] of formData.entries()) { for (const [key, value] of formData.entries()) {
if (value instanceof File) { if (value instanceof Blob) {
result[key as keyof T] = value as T[keyof T]; result[key as keyof T] = value as T[keyof T];
} else if (key.endsWith("[]")) { } else if (key.endsWith("[]")) {
const arrayKey = key.slice(0, -2) as keyof T; const arrayKey = key.slice(0, -2) as keyof T;