diff --git a/backend/tests/support/nginx_conf.py b/backend/tests/support/nginx_conf.py index 5fed8dbead..4ed51e3d57 100644 --- a/backend/tests/support/nginx_conf.py +++ b/backend/tests/support/nginx_conf.py @@ -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) diff --git a/backend/tests/test_nginx_langgraph_body_size.py b/backend/tests/test_nginx_langgraph_body_size.py index 5dee980add..1c3ce306d9 100644 --- a/backend/tests/test_nginx_langgraph_body_size.py +++ b/backend/tests/test_nginx_langgraph_body_size.py @@ -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 diff --git a/backend/tests/test_nginx_project_documents_body_size.py b/backend/tests/test_nginx_project_documents_body_size.py index f4b3a7f25c..90920fa92d 100644 --- a/backend/tests/test_nginx_project_documents_body_size.py +++ b/backend/tests/test_nginx_project_documents_body_size.py @@ -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" diff --git a/config.example.yaml b/config.example.yaml index be8b348a49..8a3ab3c6e3 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -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. diff --git a/deploy/helm/deer-flow/templates/configmap-nginx.yaml b/deploy/helm/deer-flow/templates/configmap-nginx.yaml index 53c7eb2dfd..e662e9967c 100644 --- a/deploy/helm/deer-flow/templates/configmap-nginx.yaml +++ b/deploy/helm/deer-flow/templates/configmap-nginx.yaml @@ -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; } diff --git a/docker/nginx/nginx.conf b/docker/nginx/nginx.conf index ea0806db22..7d063d3f2f 100644 --- a/docker/nginx/nginx.conf +++ b/docker/nginx/nginx.conf @@ -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; } diff --git a/docker/nginx/nginx.local.conf b/docker/nginx/nginx.local.conf index 3cca947f7c..a9be6897a8 100644 --- a/docker/nginx/nginx.local.conf +++ b/docker/nginx/nginx.local.conf @@ -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;