← Back to Pull requests

fix(analyzer): include the port and path of URLs with an explicit port #2340

Open
usmanmateen opened this pull request with 11 lines diff across 2 files
View on GitHub
usmanmateen usmanmateen commented on Oct 09, 2026 09:05 UTC
Author

Change Description

BASE_URL_REGEX allowed a path after the TLD but not a port, so for a URL with an explicit port only the scheme and host were detected, and anonymizing left the port, path and query in the output. This adds an optional :port ((?::\d{1,5}(?!\d))?) before the path.

| Input | Anonymized on main | With this PR |
|---|---|---|
| Open https://intranet.example.com:8080/patients/12345/records today | Open <URL>:8080/patients/12345/records today | Open <URL> today |
| Download from https://files.example.net:8443/reports/2026/salary.xlsx | Download from <URL>:8443/reports/2026/salary.xlsx | Download from <URL> |
| Visit https://example.com/patients/12345/records today (no port) | Visit <URL> today | unchanged |

Behaviour change

  • A URL with an explicit port is now detected through the end of its path instead of ending at the host. Scores are unchanged (0.6 with a scheme, 0.5 without).

  • URLs without a port are unchanged, and so is a host followed by a colon without digits or by more than 5 digits (example.com:123456 is still detected as example.com).
  • Tests

  • Added cases to test_when_all_urls_then_succeed: a port with a path, with a query, a port only, a non-schema URL with a port, and a port with a non-ASCII path. They fail on main and pass here. Two more pin that a colon without digits and a 6-digit number are not taken as a port.

  • tests/test_url_recognizer.py, test_analyzer_engine.py and test_context_support.py: 87 passed, including the existing long-input timing tests.

  • Full presidio-analyzer suite: same results as on main plus the new tests (locally the only failures are tests for optional extras I don't have installed: azure, langextract, transformers). ruff check passes.
  • Open PRs #2044 and #2245 also edit BASE_URL_REGEX (the TLD boundary). This change only adds the port group before the path, so it should be easy to combine with either.

    Issue reference

    Fixes #2339

    Checklist

  • I have reviewed the contribution guidelines

  • I agree to follow this project's Code of Conduct

  • I confirm that I have the right to submit this contribution and that it does not knowingly contain proprietary or confidential code.

  • My code includes unit tests

  • All unit tests and lint checks pass locally

  • My PR contains documentation updates / additions if required
  • Diff: 11 lines (+10 / -1) across 2 files Reviewer: developer-rpai Review duration: 78709m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.26) × (1 − Review Confidence (0.88)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.26 · 11 lines diff across 2 files
    0.26
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.91 · 4 substantive comment(s)
    0.91
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    88%
    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.149
    Real PR Conversation Timeline
    6 items
    developer-rpai developer-rpai commented on Oct 09, 2026 16:38 UTC
    Reviewer

    Good follow-up to #2338 — the :8080/patients/... tail leaking into anonymized output was a real gap, and extending the match through port and path is the right fix.

    A few things:
    1. Port-shape validation: if the pattern matches :\d+ loosely, strings like example.com:99999 (invalid port) or a trailing bare colon (example.com:) could still match. Worth checking the regex binds to a sensible port shape and the : doesn't greedily swallow adjacent path?query text.
    2. Interplay with #2338: the new port-aware spans carry the same 0.6/0.5 scores as the pre-#2338 patterns, so for an input like "https://x.com:8080/a" which pattern wins post-dedup comes down to PATTERNS order. One combined test case would pin the behavior down.
    3. The new span assertions are all happy-path ASCII. A unicode-path case (/über) would exercise whether spans are byte- or character-based, which matters downstream for anonymization offsets.

    usmanmateen usmanmateen commented on Oct 09, 2026 19:26 UTC
    Author

    Thanks for the review, @developer-rpai!

    1. Port shape. A bare colon wasn't an issue: example.com: closed still detects example.com and leaves the colon. But your point turned up a real edge: example.com:123456/x matched the first 5 digits as a port and anonymized to <URL>6/x. Fixed in 5459228: the port group is now (?::\d{1,5}(?!\d))?, so a 6+ digit number isn't taken as a port and the span ends at the host, as on main. I kept 5-digit values above 65535 (e.g. :99999) as part of the URL. Checking the range in a regex is awkward, and for redaction covering a malformed port beats leaving the path in clear. The path group is unchanged (/[^\s()<>"']*), so ?query is only taken after a /.
    2. Interplay with #2338. I merged both branches locally. {"url": "https://x.com:8080/a"} gives one span, https://x.com:8080/a (15–35): quotes excluded, port and path included. After #2338 the quoted patterns produce the same span as Standard Url, so the pattern order doesn't change the result. The two PRs only conflict in the test table. Whichever lands second, I'll rebase it and add this combined case then, since it can't pass on either branch alone.
    3. Unicode path. Spans are str (character) offsets, and the path class matches non-ASCII. Added https://example.de:8443/über/straße?q=ä → (0, 39), which anonymizes to <URL> with nothing left over.

    tests/test_url_recognizer.py passes (25 tests).

    usmanmateen usmanmateen commented on Oct 17, 2026 01:10 UTC
    Author

    Hi @omri374, this one is ready for a look too whenever you have time, same as #2338. Happy to adjust anything.

    usmanmateen usmanmateen commented on Oct 27, 2026 02:02 UTC
    Author

    @SharonHart, could you take a look at this one too? I've updated the branch with main.

    usmanmateen usmanmateen commented on Nov 18, 2026 12:55 UTC
    Author

    @omri374 this one fixes URL spans with an explicit port and sits next to #2338 (quoted URLs), in case it's easier to review them together.

    Assignees
    usmanmateen usmanmateen
    Labels
    risk: low confidence: 88% 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: 26.3% (LOW) · Higher = larger blast radius
    26.3%
    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: 88% · Lower = rushed or shallow review
    88%
    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: 3.2% · Safe to merge
    3.2%