Fix quadratic base64 scan in SCP classifier#14
Draft
cursor[bot] wants to merge 2 commits into
Draft
Conversation
Co-authored-by: Andre Schu <Art.Int.Interface@gmail.com>
Co-authored-by: Andre Schu <Art.Int.Interface@gmail.com>
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.
Bug and impact
A benign untrusted input such as
"Z" * 32000could makescp_utils.inspect()take multiple seconds due to quadratic base64 marker scanning. This path is used byscp_inspect,scp_validate_output,run_pipeline, and contribution anonymization, so inputs well below the configured 2M character cap could cause significant scanner/API latency or practical denial of service.Root cause
ENCODING_BASE64used a variable-length lookahead over[A-Za-z0-9+/]*to require a base64 marker. For long alphabetic runs with no marker, the regex rescanned the remainder of the string from many start positions.Fix
Replaced the lookahead regex with a linear candidate regex and moved the marker check into Python after each match. Added a regression test covering long benign alphabetic input and preserved existing padded-base64 corpus behavior.
Validation
PYTHONPATH=src python3 -m pytest -q tests/test_sanitize_encoding_evasion.py::TestBase64Chains::test_long_alpha_run_classifies_without_quadratic_base64_scan tests/test_corpus_wave3.py::test_wave3_inspect_tier-> 18 passedscp_utils.inspect("Z" * 32000)->tier=clean, elapsed0.3403sPYTHONPATH=src python3 -m pytest -q-> 281 passed, 3 skipped, 3 failed on known vendored contract hash mismatches (tests/test_contract_document_hash.py)git diff --check origin/main..HEAD-> clean