fix(appsec): make the stack trace leak check and four other regexes linear #22183
All Vouch review confidence checks passed
Residual risk 0.25 = Change Risk (0.48) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 527 lines diff across 6 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.375
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #22183"
general
weight: 0.35
avara1986
Description
APPSEC-72086
IAST's stack trace leak check strips HTML from the response body with one regex before it looks for traceback lines. On some bodies that regex takes time proportional to the square of the body size, because every failed attempt scans to the end of the body. The regex is unchanged. It is renamed
HTML_COMMENTS_TAGS_AND_ENTITIES_REMOVE, since it is the widest of the three._strip_htmlnow runs only the branches that can still match in each part of the body:-->: comments, tags and entities, as before;-->and the last>: tags and entities, since no comment can close there;>: entities only, since no tag or comment can close there.The attempts that used to scan to the end and fail can no longer start, so stripping is linear, and the output is the same as before.
Four other regexes get linear rewrites with the same results at their call sites. None of them sees large or unchecked input today; the rewrites keep them linear if a call site changes:
STACKTRACE_FILE_LINE: anchored with^. Its call site uses.match, so this changes nothing there.STACKTRACE_EXCEPTION_REGEX: themsggroup is removed, since nothing reads it. Everything after:is matched by a single.*, so the spaces after:can be matched only one way.[^<>]+instead of[^>]+, so an attempt stops at the next<._scrub_urls: a scheme cannot start in the middle of a run of scheme characters, the same lookbehind as the OpenFeature copy of this regex.Customer impact
With IAST enabled, detecting stack trace leaks on a large response that contains a Python traceback no longer uses excessive CPU. A release note is included. The other changes give the same results.
Testing
- the stack trace leak check on four body shapes;
- the two traceback line regexes;
- the Flask normalizer;
- AI Guard URL scrubbing.
The
STACKTRACE_FILE_LINEtest calls.search. Its call site uses.match, which was already linear.-
_strip_htmlgives the same output as the single regex on crafted strings that reach each part of the body: a closed comment before tags, a comment that is the last>,<!-->, a closed then an unclosed comment, and entities after the last>;- the leak check reports the same evidence as before for an unclosed
<!--before the traceback and for<inside attribute values;- a Flask converter with
<in its arguments;- an AI Guard scheme glued to a preceding digit.
_strip_htmlgave the same output as the previous regex on 300,000 random strings built from<,!,-,>,&,#,;, letters, spaces, newlines,<!--,-->and<!-->.scripts/run-tests:-
aiguard::ai_guard_api, full suite (Python 3.9 and 3.15);-
appsec::appsec, normalizer module (3.9 and 3.15);-
appsec::appsec_iast_default,-k "stacktrace or strip_html"(3.9 and 3.15);- the stack trace tests of
appsec_integrations_aiohttpandappsec_integrations_flask(3.15).scripts/lint checks.Risks
+,.or-(for example1https://…), the scheme is no longer redacted along with the URL. The authority, path and query still are. A scheme glued to a preceding letter (xhttps://…) is redacted as before.<inside converter arguments the match boundaries change. The normalized route is the same for every route in the test file and for the new case.Additional Notes
The Accept media-range regex in
_use_htmlwas part of this PR until #22179 replaced it with string parsing; that change was dropped when mergingmain.🤖 Generated with Claude Code
[APPSEC-72086]: https://datadoghq.atlassian.net/browse/APPSEC-72086?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ