Skip to content

Check the query upload cap before the body is read - #290

Merged
danielplohmann merged 2 commits into
masterfrom
fix/query-upload-cap-before-read
Oct 16, 2026
Merged

danielplohmann merged 2 commits into
masterfrom
fix/query-upload-cap-before-read

Conversation

@r0ny123

@r0ny123 r0ny123 commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

The fourth finding in #284. analyze.query compared the uploaded file with the role's cap, QUERY_UPLOAD_LIMITS (1 MiB for a visitor, mcritweb/__init__.py:92), only after the whole body had been read and parsed. The only limit before that is MAX_CONTENT_LENGTH, 1 GiB (__init__.py:88), so a visitor could make each refused request cost up to that much. Two things read the body first: the view, through request.files, and before any view, the CSRF check, which looked for its token in the form body before the headers (mcritweb/csrf.py:58).

Change.

  • Before request.files is touched (mcritweb/views/analyze.py:479), the declared Content-Length is compared with the cap plus QUERY_FORM_ALLOWANCE, 64 KiB (:460, :476). The declared length counts the other form fields and the multipart framing as well as the file, so it cannot be held to the cap itself; the exact cap is still checked on the file after it is read (:493).
  • The allowance: the dropzone's fields and framing come to 1,363 bytes beside a 1 MiB file with a short filename, and 2,002 bytes with a 255-character filename and 200-character family and version values (Werkzeug's encoder). 64 KiB leaves room for whatever else is typed into those free-text fields.
  • A capped role's body that declares no length (chunked) is refused, because only reading it would tell its size, and browsers always declare a length for a form upload. Uncapped roles are not affected.
  • _submitted_token() in the CSRF check reads the X-CSRFToken / X-CSRF-Token header first and the form field only when there is none (csrf.py:66, :70). The upload dropzone sends its token as a header (templates/table/submit_or_query_dropzone.html:101), so its uploads are no longer parsed before the cap is checked. A form that carries the token as a field is read exactly as before, and the comparison itself is unchanged. ADR-0002's plan to swap this module for flask-wtf would bring the form-first order back; the comment says why the order matters.
  • request.max_content_length is not used to stop the read: Flask allows setting it per request only from 3.1, and requirements.txt allows 3.0. On Flask 3.0.3 the assignment raises AttributeError. Apply the per-role query upload cap to the /api query routes #273, which caps the /api query routes the same way, had used it for chunked bodies and now refuses them like this.

/data/submit_or_query, the combined form in the navigation bar, reads form_type from the form before it hands over to analyze.query, so it still parses first. It is open to contributors only, whom the default mapping does not cap.

Verification. On the testbed, the master checkout (1cdc195) and this branch as two Flask development-server side instances against the same backend, CSRF on, logged in as a visitor account created in each instance's own copy of the user database. The same 300 MiB upload to /analyze/query, with the token in the X-CSRFToken header as the dropzone sends it:

answer time worker peak RSS
master 403 5.22 s 171 MB → 441 MB
this branch 403 0.79 s 171 MB → 171 MB

One run each. Nothing reached the backend, and the corpus check afterwards reports it unchanged.

Tests. New tests/testQueryUploadCap.py:

  • test_an_upload_far_over_the_cap_is_refused_before_it_is_parsed: a visitor upload over the cap plus the allowance gets 403 and the flashed message, and Flask never parses the form.
  • test_with_csrf_on_the_dropzone_upload_is_still_refused_unparsed: the same with CSRF switched on (the test app switches it off) and the token in the header.
  • test_a_capped_upload_that_declares_no_length_is_refused: a chunked visitor upload gets 403, unparsed.
  • test_a_full_size_upload_with_every_dropzone_field_still_gets_through is a guard: a file of exactly the cap, with every field the dropzone sends and a 255-character filename, is queued (202). It passes on master too.

The first three fail on master. Putting master's form-first csrf.py back fails the CSRF test; letting an undeclared length through fails the chunked test; an allowance of 0 fails the guard and test_the_cap_is_the_config_value_and_not_a_literal; removing the exact check on the file fails two tests in tests/testAppConfig.py, which are unchanged. tests/testCsrf.py passes unchanged.

The second commit is the test fix from #285 for mcrit 1.15.0, which CI now installs: its McritClient opens a requests.Session(), which the stub in tests/testSubmitMetadata.py did not have. It is a no-op once #285 is merged.

Relation to open PRs. #273 adds the same cap to the /api query routes. #141 changes the base-address handling a few lines further down in analyze.query. No open PR touches csrf.py. A trial merge (git merge-tree) of this branch with each of #141, #273, #280, #283, #126, #245 and #286 to #289 on its own is clean.

analyze.query compared the uploaded file with the role's cap
(QUERY_UPLOAD_LIMITS, 1 MiB for visitor) only after the body had been
read and parsed, which MAX_CONTENT_LENGTH lets reach 1 GiB. A visitor
could make each refused request cost that. Two things read it: the view
through request.files, and before that the CSRF check, which looked for
its token in the form body before the headers.

The declared Content-Length is now compared with the cap plus a 64 KiB
allowance for the other form fields and the multipart framing before
request.files is touched; the exact cap is still checked on the file. A
capped role's body that declares no length is refused, since only
reading it would tell its size, and browsers always declare one. The
CSRF check reads the header first. The upload dropzone sends its token
as X-CSRFToken, so its uploads are no longer parsed before the cap is
checked; a form that carries the token as a field is read as before.

request.max_content_length is not used: it can only be set per request
from Flask 3.1, and requirements.txt allows 3.0.
mcrit 1.15.0, which CI now installs, makes McritClient send through a
requests.Session() it opens in its constructor. The test swaps the
client's requests module for a stub with get and post only, so creating
the client raised AttributeError and all eight tests in the file
errored. The stub now hands itself back as the session. Same change as
in #285; it works with 1.13 and 1.15.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants