psf / psf/requests

Bad escaping of double quote in uploaded file

Open
#5,641 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
54.3k
Forks
10.4k
Avg merge
16h 43m
Merged PRs (30d)
3

Description

Uploading a file whose name contains double quotes (") in the file, requests replaces the character with %22. This is not reversible because the % char is not escaped (it's not possible to tell apart a literal %22 from a quote), and it's different from what a browser does. Browsers seem to use backslash escape instead (tested with Firefox).

Expected Result

In a shell run:

$ nc -l 0.0.0.0 8080 | grep filename

In Python

requests.post("http://localhost:8080/", files={'file': ("""foo% 'bar" \u20ac.txt""", open("README.rst"))})

Expected, what a browser returns, in the nc shell:

Content-Disposition: form-data; name="file"; filename="foo% 'bar\" €.txt"

Actual Result

Content-Disposition: form-data; name="file"; filename="foo% 'bar%22 €.txt"

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.8"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.8.5"
  },
  "platform": {
    "release": "5.4.0-51-generic",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.24.0"
  },
  "system_ssl": {
    "version": "1010106f"
  },
  "urllib3": {
    "version": "1.25.8"
  },
  "using_pyopenssl": false
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the multipart upload with the provided Python request and inspect the emitted Content-Disposition header using the shown nc command. Trace the filename encoding path in Requests and its multipart dependency, then add focused coverage for quotes, literal %22, and the Unicode filename. Done means quotes use backslash escaping without making literal percent sequences ambiguous, matching the expected browser output.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.