Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from
May 19, 2020
Merged

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

merged 8 commits into from
May 19, 2020

Conversation

fuxingZhang
Copy link
Contributor

@fuxingZhang fuxingZhang commented May 15, 2020

fix error "content-disposition must be set" when parsing file name in utf8 format

What happens

when i upload a file name in utf8 format, it throw an error "content-disposition must be set"
image

How To Reproduce

front

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <form method="POST" action="https://localhost:8000/form-data" enctype="multipart/form-data">
    <input type="file" name="filefield"><br />
    <input type="submit">
  </form>
</body>

</html>

deno code

import { serve } from "https://deno.land/[email protected]/http/server.ts";
import {
  MultipartReader,
} from "https://deno.land/std/mime/multipart.ts";
const s = serve({ port: 8000 });
console.log("https://localhost:8000/");

for await (const req of s) {
  const contentType = req.headers.get("content-type");
  if (!contentType || !contentType.match("multipart/form-data")) {
    throw new Error("is not multipart request");
  }
  let m = contentType.match(/boundary=([^ ]+?)$/);
  if (!m) {
    throw new Error("doesn't have boundary");
  }
  const boundary = m[1];
  const mr = new MultipartReader(req.body, boundary);
  const form = await mr.readForm();
  for (let [key, val] of form.entries()) {
    console.log({ key, val });
  }
  req.respond({ body: "success" });
}

Where is the problem

in my code

const mr = new MultipartReader(req.body, boundary) ;
mr.readForm();

=>

async readForm(maxMemory = 10 << 20): Promise<MultipartFormData> {

=>
const p = await this.nextPart();

=>
const headers = await r.readMIMEHeader();

=>
async readMIMEHeader(): Promise<Headers | null> {

in readMIMEHeader

const m = new Headers();

m.append(key, value);

No problem when the encoding of the value is ASCII.
But it is ignored when the value is in utf8 format( When the file name of the uploaded file is in utf8 format,the value is "form-data; name="filefield"; filename="微信图片_20200211090118.png")

in getContentDispositionParams

const cd = this.headers.get("content-disposition");

cd is null,
so i get error "content-disposition must be set" When executed to
assert(cd != null, "content-disposition must be set");

@CLAassistant
Copy link

CLAassistant commented May 15, 2020

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch!

I wonder if you could provide a test case that demonstrates the failure you're seeing?

Also CI is failing on lint/formatting. You can fix these by running ./tools/lint.py --js and ./tools/format.py --js.

@fuxingZhang
Copy link
Contributor Author

Thanks for the patch!

I wonder if you could provide a test case that demonstrates the failure you're seeing?

Also CI is failing on lint/formatting. You can fix these by running ./tools/lint.py --js and ./tools/format.py --js.

Thanks for your help!
I have added unit test and format my code.

@fuxingZhang fuxingZhang requested a review from ry May 18, 2020 01:19
Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thanks!

@ry ry merged commit 7589d4d into denoland:master May 19, 2020
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 21, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants