python / python/cpython

email: unquoted apostrophe in a MIME parameter value is dropped under policy.default

Open
#153,823 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-email type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

With policy.default a parameter value containing an unquoted apostrophe is not returned, while compat32 returns it. The apostrophe is not a tspecial (RFC 2045), so a bare value such as filename=O'Brien.pdf is a valid token and should be parsed whole.

import email, email.policy
msg = email.message_from_string(
    "Content-Disposition: attachment; filename=O'Brien.pdf\n\nbody\n", 
    policy=email.policy.default
)
msg.get_filename() is None
True

Under email.policy.compat32 the same string returns "O'Brien.pdf". Quoting the value (filename="O'Brien.pdf") works under both policies.

For a multipart message an unquoted apostrophe in the boundary is not recognized, so is_multipart() returns False and the message is not split.

ASPECIALS in Lib/email/_header_value_parser.py includes "'" unconditionally, which terminates the value.

Observed on 3.16.0a0.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-153842
  • gh-153846

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

Start in Lib/email/_header_value_parser.py, where the issue identifies ASPECIALS as terminating the parameter value. Add regression coverage for an unquoted apostrophe in a Content-Disposition filename and multipart boundary, then run the relevant email parser tests and confirm both cases work under policy.default.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.