fix(analyzer): include the port and path of URLs with an explicit port #2340
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.
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).
Hi @omri374, this one is ready for a look too whenever you have time, same as #2338. Happy to adjust anything.
@SharonHart, could you take a look at this one too? I've updated the branch with main.
@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.
developer-rpai
usmanmateen
Change Description
BASE_URL_REGEXallowed 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
example.com:123456is still detected asexample.com).Tests
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 onmainand 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.pyandtest_context_support.py: 87 passed, including the existing long-input timing tests.presidio-analyzersuite: same results as onmainplus the new tests (locally the only failures are tests for optional extras I don't have installed: azure, langextract, transformers).ruff checkpasses.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