Skip to content

Commit

Permalink
fix: allow multipart/form-data boundary to end with a newline
Browse files Browse the repository at this point in the history
The RFC 7578 spec for multipart/form-data requests does not require the
body of a request to end with a CRLF, only that each section begins with
a CRLF. While many clients implement multipart requests with the
trailing CRLF, the implementation for fetch in Node.js version 22 and
below does not.

This caused my a good number of hours debugging!

It does turn out that in September 2024 the Node.js fetch implementation
added the CRLF (nodejs/undici#3625), though this
hasn't made it to a Node.js release yet.

This change allows the boundary to end with a newline or
not, as long as the boundary is followed by the end of the request body.
  • Loading branch information
philnash committed Jan 14, 2025
1 parent e7a2005 commit 08fd912
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/backend/base/langflow/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,12 @@ async def check_boundary(request: Request, call_next):
body = await request.body()

boundary_start = f"--{boundary}".encode()
# The multipart/form-data spec doesn't require a newline after the boundary, however many clients do
# implement it that way
boundary_end = f"--{boundary}--\r\n".encode()
boundary_end_no_newline = f"--{boundary}--".encode()

if not body.startswith(boundary_start) or not body.endswith(boundary_end):
if not body.startswith(boundary_start) or not body.endswith((boundary_end, boundary_end_no_newline)):
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content={"detail": "Invalid multipart formatting"},
Expand Down

0 comments on commit 08fd912

Please sign in to comment.