Repository navigation
Check the query upload cap before the body is read - #290
Merged
Merged
Conversation
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.
This was referenced Oct 13, 2026
danielplohmann
added a commit
that referenced
this pull request
Oct 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The fourth finding in #284.
analyze.querycompared 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 isMAX_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, throughrequest.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.
request.filesis touched (mcritweb/views/analyze.py:479), the declaredContent-Lengthis compared with the cap plusQUERY_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)._submitted_token()in the CSRF check reads theX-CSRFToken/X-CSRF-Tokenheader 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_lengthis not used to stop the read: Flask allows setting it per request only from 3.1, andrequirements.txtallows 3.0. On Flask 3.0.3 the assignment raisesAttributeError. Apply the per-role query upload cap to the /api query routes #273, which caps the/apiquery 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, readsform_typefrom the form before it hands over toanalyze.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 theX-CSRFTokenheader as the dropzone sends it: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_throughis 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.pyback fails the CSRF test; letting an undeclared length through fails the chunked test; an allowance of 0 fails the guard andtest_the_cap_is_the_config_value_and_not_a_literal; removing the exact check on the file fails two tests intests/testAppConfig.py, which are unchanged.tests/testCsrf.pypasses unchanged.The second commit is the test fix from #285 for mcrit 1.15.0, which CI now installs: its
McritClientopens arequests.Session(), which the stub intests/testSubmitMetadata.pydid not have. It is a no-op once #285 is merged.Relation to open PRs. #273 adds the same cap to the
/apiquery routes. #141 changes the base-address handling a few lines further down inanalyze.query. No open PR touchescsrf.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.