ci: fail prechecks on regexes whose time grows superlinearly #22282
All Vouch review confidence checks passed
Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #22282"
general
weight: 0.35
christophe-papazian
K9 innovation week project
Description
APPSEC-72086
Adds a Check regex linearity step to the
prechecksjob:scripts/lint regex-linearityrunsscripts/check_regex_linearity.py(standard library only) whenddtrace/or the script changes. It is also part ofscripts/lint checks.It fails only on a regex whose superlinear time it has measured in CPython:
1. Collect every
recall inddtrace/and resolve its pattern and flags from the source (literals, concatenation, f-strings,%,.format, joins,re.escape, constants, loops and comprehensions over constant lists). Anything ambiguous (a name with conflicting bindings, an attribute, a list or generator something else could change or exhaust, a loop whose call does not run once per item,args/kwargs) is skipped and listed by--verbose, never guessed.vendorand_vendordirectories are skipped.2. Time it the way the code calls it:
match,fullmatch, a singlesearch, or every match forsub,findall,finditerandsplit. A compiled regex is timed with the methods it is called with, followed only through an unambiguous binding (a use that could read another binding makes it unresolved); one stored where the check cannot follow it, such as a container or another object, is timed withfinditer. A use that may not run as timed is also listed as unresolved: amatchorfullmatchfrom a nonzeroposis not timed, while asearch,findallorfinditerfrom a nonzeropos, or amatchorfullmatchmethod kept to be called later, is timed from position 0.3. Build attack inputs (about 800 per regex) from CPython's own parse tree of the pattern: repeated texts and delimiters, with endings that make the match fail.
4. Screen them in a subprocess at 4 to 16 KB, using CPU time: a candidate's time must grow at least 2.5x per doubling, twice.
5. Confirm each candidate with a growth curve in fresh subprocesses, timed again since noise only adds time: at least three points up to a quarter of a second with a fitted exponent of at least 1.5 and every step superlinear, or exponential growth, or a timeout seen twice.
Example report (from the
ddtrace/tree before #22194):CodeQL is not used: it isn't in the testrunner image, and it skips patterns with a backreference or built by concatenation, both of which hid superlinear regexes here.
Customer impact
None: CI and lint tooling only.
Testing
tests/internal/test_check_regex_linearity.py(44 tests) covers resolution and unresolved calls, method selection, attack generation, the verdict rules (repeated timing, early stop, timeouts, crashes), the report and the exit code. It passes on Python 3.9 and 3.14, and in a testrunner container limited to 2 CPUs.ddtrace/the check resolves 134 regexes, lists 18 calls as unresolved (4 of them still timed) and reports none (at 98411c2456). On the tree before the APPSEC-72086 fixes (498511e8e1) it reports the 18 regexes fixed under that ticket, and on a synthetic corpus every exponential, quadratic and cubic case and no linear one.Risks
ddtrace/must be linear for the precheck to pass. The regexes it found were fixed first, in #22183, #22192, #22194, #22200, #22215, #22216, #22226, #22283 and #22292.finditer, the costliest method butfullmatch, for a compiled regex; no limit for an unresolvedcount,maxsplitorendpos; position 0 for asearchfrom a laterpos) and it does not see changes made by other modules, so it can report a regex the code runs faster; that would be fixed in the check itself.--verbose), inputs the heuristic attack generation never builds, and calls with a constantendposof at most 16 are not checked. A compiled regex the check cannot follow (stored in a container or on another object, or only used from another module) is timed withfinditeronly, so one that is slow only withfullmatchpasses. A call listed but still timed (a laterpos, a method kept for later) can also be slower at run time than as timed.ddtrace/takes 4 to 9 s on a laptop and was measured at 36 to 39 s on a 5-CPU sandbox (180 s under heavy load). Confirming a real finding costs seconds, up to about a minute under load.Additional Notes
Run it locally with
scripts/lint regex-linearity [paths...] [--verbose].🤖 Generated with Claude Code
[APPSEC-72086]: https://datadoghq.atlassian.net/browse/APPSEC-72086?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ