Opened 6 weeks ago

Last modified 4 days ago

#37413 assigned Bug

MultiPartParser treats boundary tokens not preceded by CRLF as delimiters

Reported by: Natalia Bidart Owned by: Sudarshan
Component: HTTP handling Version: dev
Severity: Normal Keywords: MultiPartParser boundary not-security
Cc: Sudarshan Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

BoundaryIter._find_boundary() in django/http/multipartparser.py looks for the bare --<boundary> string and only strips a preceding CRLF if present. Per ​RFC 2046 section 5.1.1, a delimiter is CRLF "--" boundary, so a boundary token appearing inside part data without a leading CRLF should be treated as content, not as the end of the part:

The boundary delimiter MUST occur at the beginning of a line, i.e., following a CRLF, and the initial CRLF is considered to be attached to the boundary delimiter line rather than part of the preceding part.

For example, with boundary XYZ, this body (CRLF line endings):

--XYZ
Content-Disposition: form-data; name="a"

foo--XYZ
Content-Disposition: form-data; name="b"

bar
--XYZ--

is parsed as two fields (a="foo", b="bar"), while a strict parser would return a single field a whose value includes the embedded token.

Requiring the CRLF prefix (other than for the first delimiter at the start of the body) would align Django with the RFC. Lenient clients that rely on the current behavior could be affected, so this may need a deprecation path or at least a release note.

Change History (9)

comment:1 by Jacob Walls, 6 weeks ago

Keywords: not-security added
Triage Stage: Unreviewed → Accepted

comment:2 by Vishy, 6 weeks ago

Owner: set to Vishy
Status: new → assigned

comment:3 by Vishy, 6 weeks ago

Nice find, thanks!

comment:4 by Sarah Boyce, 2 weeks ago

Owner: Vishy removed
Status: assigned → new

comment:5 by Ishan Kumar, 2 weeks ago

Hi! I'd like to work on this ticket.

I reproduced it on current main: with boundary XYZ, the body from the description is parsed as two fields (a="foo", b="bar"), while RFC 2046 would give a single field "a" whose value contains the embedded "--XYZ" line.

My plan is to make BoundaryIter._find_boundary() only accept a boundary that is preceded by a line break, except for the very first delimiter at the start of the body, and to add tests for the embedded-token case.

Before writing the patch, two questions:

  1. Should only CRLF count as the line break, or should a bare LF also be accepted for lenient clients?
  2. Is a release note enough for this change, or would you prefer a deprecation path?

(I used Claude Code to help reproduce the issue and draft this comment.)

comment:6 by Ishan Kumar, 2 weeks ago

Owner: set to Ishan Kumar
Status: new → assigned

comment:7 by Ishan Kumar, 8 days ago

I have a patch ready at ​https://github.com/ishan-one8/django/tree/ticket_37413. My PR for it (​https://github.com/django/django/pull/22272) was closed automatically by the new-contributor PR limit while my other PR (​https://github.com/django/django/pull/22153) is open, so I'll reopen it once that one is resolved. In the patch, a bare LF before the boundary is still accepted, and I added a release note instead of a deprecation path.

comment:8 by Sudarshan, 4 days ago

Cc: Sudarshan added
Has patch: set
Owner: changed from Ishan Kumar to Sudarshan
Note: See TracTickets for help on using tickets.
Back to Top