fix(multipart): fix error when parsing file name in utf8 format (#5428)

This commit is contained in:
zfx 2020-05-20 02:22:26 +08:00 committed by GitHub
parent 9752b853dd
commit 7589d4d7c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 13 deletions

View file

@ -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)

View file

@ -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();
},
});

View file

@ -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--

View file

@ -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