summaryrefslogtreecommitdiff
path: root/cli/tests
diff options
context:
space:
mode:
authorAndreu Botella <andreu@andreubotella.com>2023-05-16 17:49:35 +0200
committerGitHub <noreply@github.com>2023-05-16 17:49:35 +0200
commit9ba2c4c42fcbadea1f19c67c88b5cbc4c97804f3 (patch)
tree4cd2d0eb702251736af4ca6b9c86bc49ac4a5bf8 /cli/tests
parent1c74b41855b85c9ec2ee1d83ac0f6b04e1461788 (diff)
fix(fetch): Correctly decode `multipart/form-data` names and filenames (#19145)
Currently the `multipart/form-data` parser in `Request.prototype.formData` and `Response.prototype.formData` decodes non-ASCII filenames incorrectly, as if they were encoded in Latin-1 rather than UTF-8. This happens because the header section of each `multipart/form-data` entry is decoded as Latin-1 in order to be parsed with `Headers`, which only allows `ByteString`s, but the names and filenames are never decoded correctly. This PR fixes this as a post-processing step. Note that the `multipart/form-data` parsing for this APIs in the Fetch spec is very much underspecified, and it does not specify that names and filenames must be decoded as UTF-8. However, it does require that the bodies of non-`File` entries are decoded as UTF-8, and in browsers, names and filenames always use the same encoding as the body. Closes #19142.
Diffstat (limited to 'cli/tests')
-rw-r--r--cli/tests/unit/body_test.ts53
1 files changed, 53 insertions, 0 deletions
diff --git a/cli/tests/unit/body_test.ts b/cli/tests/unit/body_test.ts
index e7a38b7a6..8aebfadd3 100644
--- a/cli/tests/unit/body_test.ts
+++ b/cli/tests/unit/body_test.ts
@@ -53,6 +53,59 @@ Deno.test(
},
);
+// FormData: non-ASCII names and filenames
+Deno.test(
+ { permissions: { net: true } },
+ async function bodyMultipartFormDataNonAsciiNames() {
+ const boundary = "----01230123";
+ const payload = [
+ `--${boundary}`,
+ `Content-Disposition: form-data; name="文字"`,
+ "",
+ "文字",
+ `--${boundary}`,
+ `Content-Disposition: form-data; name="file"; filename="文字"`,
+ "Content-Type: application/octet-stream",
+ "",
+ "",
+ `--${boundary}--`,
+ ].join("\r\n");
+
+ const body = buildBody(
+ new TextEncoder().encode(payload),
+ new Headers({
+ "Content-Type": `multipart/form-data; boundary=${boundary}`,
+ }),
+ );
+
+ const formData = await body.formData();
+ assert(formData.has("文字"));
+ assertEquals(formData.get("文字"), "文字");
+ assert(formData.has("file"));
+ assert(formData.get("file") instanceof File);
+ assertEquals((formData.get("file") as File).name, "文字");
+ },
+);
+
+// FormData: non-ASCII names and filenames roundtrip
+Deno.test(
+ { permissions: { net: true } },
+ async function bodyMultipartFormDataNonAsciiRoundtrip() {
+ const inFormData = new FormData();
+ inFormData.append("文字", "文字");
+ inFormData.append("file", new File([], "文字"));
+
+ const body = buildBody(inFormData);
+
+ const formData = await body.formData();
+ assert(formData.has("文字"));
+ assertEquals(formData.get("文字"), "文字");
+ assert(formData.has("file"));
+ assert(formData.get("file") instanceof File);
+ assertEquals((formData.get("file") as File).name, "文字");
+ },
+);
+
Deno.test(
{ permissions: { net: true } },
async function bodyURLEncodedFormData() {