fix(nginx): leave multipart headroom above the upload limit (#8580)

* fix(nginx): leave multipart headroom above the upload limit

Gateway's default 100 MiB upload ceiling counts file bytes only, while nginx's 100M limit also has to carry multipart headers and boundaries. Two valid 50 MiB files therefore exceed nginx and return 413 before Gateway can apply its limits. Raise the thread-upload and project-document locations to 101M in the Docker, local-dev and Helm configs, and assert the proxy limit is strictly greater than Gateway's total content limit.

* test(nginx): size upload headroom from multipart framing

Compare the proxy ceiling with a representative multipart body: Gateway's maximum content plus part headers, closing boundary and filename slack. Base project-document headroom on its single-file limit. Document that uploads.* is runtime-configurable while the nginx ceilings are static, and drop the hardcoded 101M from unrelated prose.

---------

Co-authored-by: Tong-bit-art <Tong-bit-art@users.noreply.github.com>
This commit is contained in:
ordinary being 2026-11-24 23:11:16 +08:00 committed by GitHub
parent 3a2ebca717
commit 8d6e774de6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 58 additions and 11 deletions

View File

@ -58,6 +58,24 @@ def parse_body_size_bytes(block: str) -> int:
return int(value) * _SIZE_MULTIPLIERS[unit.lower()]
_MULTIPART_BOUNDARY = "----deerflow-upload-boundary"
_MULTIPART_PART_HEADERS = (f'--{_MULTIPART_BOUNDARY}\r\nContent-Disposition: form-data; name="files"; filename="upload.bin"\r\nContent-Type: application/octet-stream\r\n\r\n').encode()
_MULTIPART_CLOSING = f"--{_MULTIPART_BOUNDARY}--\r\n".encode()
_MULTIPART_FILENAME_SLACK_BYTES = 64 * 1024
def multipart_body_size_floor(content_bytes: int, part_count: int) -> int:
"""A representative multipart body for *content_bytes* over *part_count* files.
Includes the part headers and closing boundary plus generous slack for
longer filenames. A proxy ceiling at or below this value can reject a valid
request before Gateway sees it.
"""
assert part_count > 0
framing = part_count * len(_MULTIPART_PART_HEADERS) + len(_MULTIPART_CLOSING)
return content_bytes + framing + _MULTIPART_FILENAME_SLACK_BYTES
def parse_read_timeout_seconds(block: str) -> int:
# Anchored to the start of a line so a commented-out directive does not count.
match = re.search(r"^\s*proxy_read_timeout\s+(\d+)([smh]?)\s*;", block, re.M)

View File

@ -33,13 +33,16 @@ from __future__ import annotations
import pytest
from support.nginx_conf import NGINX_CONFIGS
from support.nginx_conf import extract_location_block as _extract_location_block
from support.nginx_conf import multipart_body_size_floor as _multipart_body_size_floor
from support.nginx_conf import parse_body_size_bytes as _parse_body_size_bytes
from support.nginx_conf import parse_read_timeout_seconds as _parse_read_timeout_seconds
from support.nginx_conf import read_config as _read
from app.gateway.routers.uploads import DEFAULT_MAX_FILES, DEFAULT_MAX_TOTAL_SIZE
# Text prompts never carry binary file attachments (those go through the
# dedicated uploads route), so the ceiling here is intentionally well below
# the uploads route's 100M -- generous for even a very long pasted document,
# the uploads route's ceiling -- generous for even a very long pasted document,
# while avoiding needlessly letting one chat request force Gateway to buffer
# and JSON-parse an arbitrarily large body in memory.
_MIN_EXPECTED_BODY_SIZE_BYTES = 5 * 1024 * 1024
@ -73,7 +76,7 @@ def test_langgraph_route_raises_body_size_limit_for_text_prompts(path):
f"{path}: /api/langgraph/ client_max_body_size is {size_bytes} bytes, "
f"expected between {_MIN_EXPECTED_BODY_SIZE_BYTES} and "
f"{_MAX_EXPECTED_BODY_SIZE_BYTES} bytes -- comfortably above nginx's 1m "
"default (the actual bug) but below the uploads route's 100M, since "
"default (the actual bug) but below the uploads route's ceiling, since "
"this route only ever carries JSON chat text, never binary files"
)
@ -83,11 +86,19 @@ def test_uploads_route_still_has_its_own_body_size_settings(path):
"""Non-regression guard: confirms the extraction helper is precise (the
uploads location has both directives with its own, larger value) and that
fixing the langgraph route does not accidentally touch the upload route's
existing configuration."""
existing configuration.
Gateway's 100 MiB default counts file bytes only. Multipart headers and
boundaries add framing overhead, so nginx must admit strictly more than
that total or a valid request is rejected with a bare 413."""
content = _read(path)
block = _extract_location_block(content, r"~ ^/api/threads/[^/]+/uploads")
assert "client_max_body_size 100M;" in block
size_bytes = _parse_body_size_bytes(block)
required_bytes = _multipart_body_size_floor(DEFAULT_MAX_TOTAL_SIZE, DEFAULT_MAX_FILES)
assert size_bytes > required_bytes, (
f"{path}: uploads client_max_body_size is {size_bytes} bytes but a representative multipart request with Gateway's {DEFAULT_MAX_TOTAL_SIZE} bytes of content needs more than {required_bytes} bytes; nginx would 413"
)
assert "proxy_request_buffering off;" in block

View File

@ -17,7 +17,7 @@ from __future__ import annotations
import re
import pytest
from support.nginx_conf import NGINX_CONFIGS, extract_location_block, parse_body_size_bytes, parse_read_timeout_seconds, read_config
from support.nginx_conf import NGINX_CONFIGS, extract_location_block, multipart_body_size_floor, parse_body_size_bytes, parse_read_timeout_seconds, read_config
from app.gateway.routers.project_documents import router as project_documents_router
from app.gateway.routers.uploads import DEFAULT_MAX_FILE_SIZE
@ -55,6 +55,8 @@ def test_project_documents_route_allows_the_upload_limit_gateway_enforces(path):
uploads_size_bytes = parse_body_size_bytes(extract_location_block(read_config(path), UPLOADS_LOCATION))
assert size_bytes >= DEFAULT_MAX_FILE_SIZE, f"{path}: project documents client_max_body_size is {size_bytes} bytes but Gateway accepts {DEFAULT_MAX_FILE_SIZE} by default; nginx would 413 a file Gateway allows"
required_bytes = multipart_body_size_floor(DEFAULT_MAX_FILE_SIZE, 1)
assert size_bytes > required_bytes, f"{path}: project documents client_max_body_size is {size_bytes} bytes but a representative one-file multipart request needs more than {required_bytes} bytes; nginx would 413"
assert size_bytes == uploads_size_bytes, f"{path}: project documents and thread uploads share uploads.max_file_size, so their nginx ceilings must match"
assert "proxy_request_buffering off;" in block, f"{path}: uploads must stream to Gateway instead of spooling to client_body_temp"

View File

@ -1756,6 +1756,10 @@ uploads:
max_files: 10
max_file_size: 52428800 # 50 MiB
max_total_size: 104857600 # 100 MiB
# nginx cannot read these runtime values. If you raise any of them, raise
# client_max_body_size above the new content total plus multipart framing in
# docker/nginx/nginx.conf, docker/nginx/nginx.local.conf and the Helm nginx
# ConfigMap too.
# Automatic Office/PDF conversion runs on the backend host before sandbox
# isolation applies. Keep this disabled unless uploads come from a fully
# trusted source and you intentionally accept host-side parser risk.

View File

@ -183,7 +183,9 @@ data:
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $forwarded_proto;
client_max_body_size 100M;
# Gateway counts file bytes only; leave room for multipart framing.
# If uploads.* is raised, raise this ceiling in all maintained nginx configs.
client_max_body_size 101M;
proxy_request_buffering off;
}
@ -199,7 +201,9 @@ data:
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $forwarded_proto;
client_max_body_size 100M;
# Gateway counts file bytes only; leave room for multipart framing.
# If uploads.* is raised, raise this ceiling in all maintained nginx configs.
client_max_body_size 101M;
proxy_request_buffering off;
proxy_read_timeout 600s;
}

View File

@ -222,7 +222,9 @@ http {
proxy_set_header X-Forwarded-Proto $forwarded_proto;
# Large file upload support
client_max_body_size 100M;
# Gateway counts file bytes only; leave room for multipart framing.
# If uploads.* is raised, raise this ceiling in all maintained nginx configs.
client_max_body_size 101M;
proxy_request_buffering off;
# Disable response buffering to avoid permission errors
@ -243,7 +245,9 @@ http {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $forwarded_proto;
client_max_body_size 100M;
# Gateway counts file bytes only; leave room for multipart framing.
# If uploads.* is raised, raise this ceiling in all maintained nginx configs.
client_max_body_size 101M;
proxy_request_buffering off;
proxy_read_timeout 600s;
}

View File

@ -234,7 +234,9 @@ http {
proxy_set_header X-Forwarded-Proto $scheme;
# Large file upload support
client_max_body_size 100M;
# Gateway counts file bytes only; leave room for multipart framing.
# If uploads.* is raised, raise this ceiling in all maintained nginx configs.
client_max_body_size 101M;
proxy_request_buffering off;
# Disable response buffering to avoid permission errors
@ -257,7 +259,9 @@ http {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 100M;
# Gateway counts file bytes only; leave room for multipart framing.
# If uploads.* is raised, raise this ceiling in all maintained nginx configs.
client_max_body_size 101M;
proxy_request_buffering off;
proxy_read_timeout 600s;