From 7589d4d7c4a7c95298268eb8bea0e808feb0c6a9 Mon Sep 17 00:00:00 2001 From: zfx <502545703@qq.com> Date: Wed, 20 May 2020 02:22:26 +0800 Subject: [PATCH] fix(multipart): fix error when parsing file name in utf8 format (#5428) --- std/mime/multipart.ts | 2 +- std/mime/multipart_test.ts | 4 ++++ std/mime/testdata/sample.txt | 30 +++++++++++++++++++----------- std/textproto/mod.ts | 8 +++++++- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/std/mime/multipart.ts b/std/mime/multipart.ts index abe46f8201559..75d418c92e329 100644 --- a/std/mime/multipart.ts +++ b/std/mime/multipart.ts @@ -206,7 +206,7 @@ class PartReader implements Reader, Closer { const cd = this.headers.get("content-disposition"); const params: { [key: string]: string } = {}; assert(cd != null, "content-disposition must be set"); - const comps = cd.split(";"); + const comps = decodeURI(cd).split(";"); this.contentDisposition = comps[0]; comps .slice(1) diff --git a/std/mime/multipart_test.ts b/std/mime/multipart_test.ts index e12a2f3998d2d..61afdba03635b 100644 --- a/std/mime/multipart_test.ts +++ b/std/mime/multipart_test.ts @@ -188,6 +188,10 @@ test({ const file = form.file("file"); assert(isFormFile(file)); assert(file.content !== void 0); + const file2 = form.file("file2"); + assert(isFormFile(file2)); + assert(file2.filename === "中文.json"); + assert(file2.content !== void 0); o.close(); }, }); diff --git a/std/mime/testdata/sample.txt b/std/mime/testdata/sample.txt index 97e9bf5531be5..8c7a1c204074c 100644 --- a/std/mime/testdata/sample.txt +++ b/std/mime/testdata/sample.txt @@ -12,16 +12,24 @@ bar content-disposition: form-data; name="file"; filename="tsconfig.json" content-type: application/octet-stream -{ - "compilerOptions": { - "target": "es2018", - "baseUrl": ".", - "paths": { - "deno": ["./deno.d.ts"], - "https://*": ["../../.deno/deps/https/*"], - "http://*": ["../../.deno/deps/http/*"] - } - } -} +{ + "compilerOptions": { + "target": "es2018", + "baseUrl": ".", + "paths": { + "deno": ["./deno.d.ts"], + "https://*": ["../../.deno/deps/https/*"], + "http://*": ["../../.deno/deps/http/*"] + } + } +} + +----------------------------434049563556637648550474 +content-disposition: form-data; name="file2"; filename="中文.json" +content-type: application/octet-stream + +{ + "test": "filename" +} ----------------------------434049563556637648550474-- diff --git a/std/textproto/mod.ts b/std/textproto/mod.ts index 9b843e5b12dd7..48bbed8bf5f00 100644 --- a/std/textproto/mod.ts +++ b/std/textproto/mod.ts @@ -8,6 +8,9 @@ import { charCode } from "../io/util.ts"; import { concat } from "../bytes/mod.ts"; import { decode } from "../encoding/utf8.ts"; +// FROM https://github.com/denoland/deno/blob/b34628a26ab0187a827aa4ebe256e23178e25d39/cli/js/web/headers.ts#L9 +const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/g; + function str(buf: Uint8Array | null | undefined): string { if (buf == null) { return ""; @@ -102,7 +105,10 @@ export class TextProtoReader { ) { i++; } - const value = str(kv.subarray(i)); + const value = str(kv.subarray(i)).replace( + invalidHeaderCharRegex, + encodeURI + ); // In case of invalid header we swallow the error // example: "Audio Mode" => invalid due to space in the key