Skip to content

Commit

Permalink
fix(multipart): fix error when parsing file name in utf8 format (deno…
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingZhang committed May 19, 2020
1 parent 9752b85 commit 7589d4d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion std/mime/multipart.ts
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions std/mime/multipart_test.ts
Expand Up @@ -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();
},
});
Expand Down
30 changes: 19 additions & 11 deletions std/mime/testdata/sample.txt
Expand Up @@ -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--
8 changes: 7 additions & 1 deletion std/textproto/mod.ts
Expand Up @@ -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 "";
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7589d4d

Please sign in to comment.