← Back to Pull requests

fix(appsec): make the stack trace leak check and four other regexes linear #22183

Open
unknown opened this pull request with 527 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 30, 2026 14:02 UTC
Author

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_html now runs only the branches that can still match in each part of the body:

  • before the last -->: comments, tags and entities, as before;

  • between the last --> and the last >: tags and entities, since no comment can close there;

  • after the last >: 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:

  • IAST STACKTRACE_FILE_LINE: anchored with ^. Its call site uses .match, so this changes nothing there.

  • IAST STACKTRACE_EXCEPTION_REGEX: the msg group is removed, since nothing reads it. Everything after : is matched by a single .*, so the spaces after : can be matched only one way.

  • API Security Flask normalizer: the converter group is [^<>]+ instead of [^>]+, so an attempt stops at the next <.

  • AI Guard _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

  • New linear-time tests, each bounded at 1 s. With the previous regexes they take between 3 s and 22 s locally:

  • - the stack trace leak check on four body shapes;
    - the two traceback line regexes;
    - the Flask normalizer;
    - AI Guard URL scrubbing.

    The STACKTRACE_FILE_LINE test calls .search. Its call site uses .match, which was already linear.

  • New behavior tests:

  • - _strip_html gives 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.
  • Not committed: _strip_html gave the same output as the previous regex on 300,000 random strings built from <, !, -, >, &, #, ;, letters, spaces, newlines, <!--, --> and <!-->.

  • Local runs with 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_aiohttp and appsec_integrations_flask (3.15).
  • scripts/lint checks.
  • Risks

  • AI Guard: when a run of scheme characters starts with a digit, +, . or - (for example 1https://…), 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.

  • Flask normalizer: with < 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_html was part of this PR until #22179 replaced it with string parsing; that change was dropped when merging main.

    🤖 Generated with Claude Code

    [APPSEC-72086]: https://datadoghq.atlassian.net/browse/APPSEC-72086?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

    Diff: 527 lines (+395 / -132) across 6 files Reviewer: avara1986 Review duration: 1031m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.8% (MEDIUM) · Higher = larger blast radius
    47.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.5% · Safe to merge
    25.5%