Repository navigation
Answer input the UI never sends with 400s instead of 500s - #283
Merged
Merged
Conversation
analyze.query refuses a query upload larger than QUERY_UPLOAD_LIMITS allows for the account's role (1 MiB for a visitor by default), but the /api routes that queue the same kind of query, query/binary and query/function, never looked at it. A visitor's token could queue a query of up to MAX_CONTENT_LENGTH that the same account's browser would have been refused. Both routes now apply the same mapping and answer 403, as the form does. The declared Content-Length is compared first, so an oversized body is turned away without being read. A body with no declared length (chunked) is read with one byte more than the cap as the request's limit, so it is measured without reading more than that. The other /api routes that take a body are id lists, not queries, and are not capped here.
This was referenced Oct 9, 2026
A body without a Content-Length was measured by setting request.max_content_length to one byte over the cap and reading it. That assignment needs Flask 3.1, which made it per-request; on Flask 3.0, which requirements.txt still allows, the property has no setter and the request ended in AttributeError, a 500. Checked with Flask 3.0.3. A capped role's chunked body is now refused without being read, as analyze.query will do for its uploads. McritClient sends bytes through requests, which always declares a length, so no client of the API is turned away by this. Uncapped roles are not affected.
r0ny123
force-pushed
the
fix/bad-input-answers-not-500
branch
from
October 13, 2026 09:26
c48e441 to
939f086
Compare
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.
Several routes parsed request input without handling a bad value, so a hand-made request ended in an unhandled exception: - cross_compare: a non-numeric samples or cache list (int()) - import: JSON nested deeper than json.load follows (RecursionError) - request_filename_info: a filename that is not a string, and a base_addr in the file header longer than int() converts - submit_or_query: an unknown form_type, for which the view returned nothing - the job page: a refresh or forward value that is not a number - /api/query/function and POST /api/samples: a body that is not a report, which SmdaReport.fromDict raised on, or nested too deep for get_json Each now answers 400, or reads the value as absent where it is only a display option. The same kind of parse in analyze.query and submit is left to #141, which already reworks both. The function comparison's edit distance mapped instructions to the characters chr(0x20) to chr(0xff) and raised past 223 distinct ones, so two large or unusual functions failed the comparison page. The symbols now run up the code points, skipping the surrogates; rapidfuzz compares them as fast. Such a pair now costs what any pair of that size costs in the block-pair loop, instead of failing early. test_the_api_blueprint_is_exempt took any 400 from POST /api/samples for the CSRF refusal; that route now answers {} with its own 400, so the test looks for the CSRF message instead.
r0ny123
force-pushed
the
fix/bad-input-answers-not-500
branch
from
October 13, 2026 09:46
939f086 to
c068526
Compare
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.
A security review of master listed four robustness findings: requests the UI never sends that end in an unhandled exception, a 500 and a traceback in the log. None of them reads or writes anything it should not; each is a parse of request input with no answer for a bad value. This fixes them, except the upload parsing in
analyze.queryandsubmit, which #141 already reworks (see the last section).Change.
/analyze/cross_compare?samples=abcor?cache=1,xValueErrorfromint()mcritweb/views/analyze.py:251), asstart_cross_comparealready says/data/importwith JSON nested too deepRecursionError(the handler caughtKeyErrorandValueError)mcritweb/views/data.py:217)/data/request_filename_infowith afilenamethat is not a stringAttributeErroron.endswithdata.py:1417)/data/request_filename_infowith an.smdaheader whosebase_addrhas 5000 digitsValueError: Python refuses to convert an int of more than 4300 digitsdata.py:1441)/data/submit_or_querywith an unknownform_typeTypeError: the view returned nothingdata.py:1466)/data/jobs/<id>?refresh=abcor?forward=xValueError(the code caught only theTypeErrorof a missing value)parse_integer_query_param(data.py:1297)POST /api/query/functionandPOST /api/sampleswith a body that is not a reportValueErrorfromSmdaReport.fromDict, orRecursionErrorfromget_jsonfor deep nestingmcritweb/views/api.py:116,:174)Exception("Too many distinct instructions ...")out ofget_function_diff(data.py:372)The two API routes now read the body with
read_smda_report(mcritweb/views/utility.py:25). It catchesValueErrorandRecursionErrorfromjson.loads, and whatSmdaReport.fromDictraises for something that is not a report. With smda 4.9.0 that wasValueErrorfor every malformed shape tried; smda 4.2.13, the oldest mcrit allows, raisesTypeErrorandKeyErrorfor some of the same input, so those are caught too. Valid reports read as before: a UTF-8 BOM, UTF-16 and an empty body parse asget_json(force=True)parsed them. On/api/query/functionthe body is read after #273's cap, which reads no further than it needs to.The function comparison's edit distance maps each distinct instruction to one character. The characters ran from
chr(0x20)and stopped atchr(0xff), so the 224th distinct instruction raised._symbol(mcritweb/views/functiondiff.py:164) now continues up the code points and skips the surrogates, which are not characters. rapidfuzz compares any code points; on 1000 x 1000 random blocks of 3 to 12 instructions it took 0.18 s with characters past0x10000against 0.21 s with the current ones (integer lists, the other option, took 0.37 s). A pair past 223 now costs what any pair of that size costs in the block-pair loop (0.4 s for 1000 x 1000 blocks and 2.5 s for 3000 x 3000, as measured in the review), instead of failing early; a finished comparison is memoized as before.Verification. On the live testbed (mcrit 1.13.0), master (1cdc195) on :5000 against a side instance of this branch, same backend and account, requests that are refused before anything reaches the backend:
GET /analyze/cross_compare?samples=abcGET /analyze/cross_compare?cache=1,xGET /data/jobs/<id>?refresh=abcGET /data/jobs/<id>?forward=xThe branch's log has no traceback for them, and the corpus check afterwards reports it unchanged. The side instance ran an earlier version of this commit that also changed
analyze.query; that part is gone, the four routes above are unchanged since. The function comparison limit did not show on the testbed's corpus: comparing the largest function of each of eight samples against each other (28 pairs) stayed below 223 distinct unmatched instructions, since operands are escaped. Like the review, I could show it only at the function level; a crafted sample, whose instructions its author chooses, reaches it at will.Tests. In
tests/testBadRequests.py:test_input_the_ui_never_sends_is_answered_not_raisedsends each request in the first table but the comparison (11 cases) and asserts an answer below 500. 10 fail without this commit; anullfilename with anullheader already fell into master's ownexcept.test_the_api_answers_a_body_that_is_not_a_report_with_400checks the 400 on both API routes, andtest_a_report_that_older_smda_cannot_read_is_nonepins the exceptions older smda raises.TestJobPageParametersruns the job page on a captured job made to look unfinished, so the auto-refresh tag can appear: a number still sets it, a non-number leaves it out.test_a_hand_edited_sample_list_is_reportedcoverscross_compare.In
tests/testFunctionPages.py,test_more_than_223_distinct_instructions_are_comparedpairs 300 one-instruction blocks with their twins at distance 0, andtest_the_symbols_skip_the_surrogatespins the jump over them.22 of the 24 new tests fail with the views before this commit (#273's head); the
nullcase above and the guardTestJobPageParameters::test_a_number_still_sets_the_refreshpass there.tests/testCsrf.py::test_the_api_blueprint_is_exempttook any 400 fromPOST /api/samplesfor the CSRF refusal, and that route now answers its{}with its own 400, so the test looks for the CSRF message instead; it still fails when the exemption is removed.Relation to open PRs.
fix/api-query-upload-cap): both change the same lines of/api/query/function, and the order there matters, since Apply the per-role query upload cap to the /api query routes #273's cap has to run before the body is read. Merge Apply the per-role query upload cap to the /api query routes #273 first.fix/44-dedumped-is-not-a-dump) already answers a bad base address, bitness or.smdafile inanalyze.queryandsubmitwith a 400, which is the rest of the review's finding; this branch leaves those two routes to it. A dedumped sample is not a dump, and an empty base address is not zero (#44) #141 conflicts with master indata.pyandparams.py, and the same two files with this branch added.git merge-tree, against this head) with Check the sha256 of an SMDA report before it goes into a backend path #271, Serve the dot-graph routes as text/plain and send nosniff #272, Keep query values inside the URL strings of the pagination handlers #275 to Answer findLoops' dominators from the dominator tree, and bound it #282, Lay out a large function's control flow graph only when asked (#267) #268 and Fix hovering in the control flow graphs of both function pages #269 is clean.