Repository navigation
[Core] Fix urlParseFullHost and urlParseHost for IPv6 literals and URLs with userinfo (#2799) - #2800
viradiyashivali-cmd wants to merge 5 commits into
Conversation
… userinfo (microsoft#2799) Support bracketed IPv6 addresses and strip userinfo in urlParseFullHost and urlParseHost. Fixes microsoft#2799.
|
@microsoft-github-policy-service agree |
There was a problem hiding this comment.
🟡 Changes recommended
The no-DOM URL parser mishandles backslashes and userinfo, producing incorrect hosts that can expose credentials in telemetry or allow correlation headers on unintended destinations.
2 open issues
What changed in this PR
Addresses #2799 by improving the SDK’s fallback URL host parsing for bracketed IPv6 addresses and URLs containing credentials, supporting accurate dependency targets and correlation-header checks.
Changes:
- Updates host-matching expressions while preserving capture groups and port handling.
- Adds regression tests for IPv6, userinfo, and correlation-header decisions.
| File | Description |
|---|---|
| shared/AppInsightsCore/Tests/Unit/src/ai/Util.tests.ts | Adds host-parsing and correlation-header regression cases. |
| shared/AppInsightsCore/src/utils/UrlHelperFuncs.ts | Updates fallback parsing to recognize bracketed IPv6 hosts and strip userinfo. |
Review effort: Balanced
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Treat backslashes as authority boundaries in urlParseHost and urlParseFullHost. Adds no-DOM regression test for correlationIdCanIncludeCorrelationHeader.
|
Great catch! Thank you for pointing this out. I have updated the regexes in With this change:
Pushed in the latest commit! |
|
Hector Hernandez (@hectorhdzg) This is my first PR. Any suggestions on this PR ? |
Adds unit and no-DOM regression tests for empty userinfo and multiple @ credentials in URLs.
|
Both review items have been resolved:
All 996 unit tests are passing! |
|
Hector Hernandez (@hectorhdzg) Any suggestions on this PR ? |
I am taking a look at this PR. I will let you know once the review is completed. |
|
Thank you Ma'am. |
| let fullHost = urlParseFullHost(url, inclPort) || STR_EMPTY; | ||
| if (fullHost) { | ||
| const match = fullHost.match(/(www\d{0,5}\.)?([^\/:]{1,256})(:\d{1,20})?/i); | ||
| const match = fullHost.match(/(www\d{0,5}\.)?(\[[^\]\/\?#\\]{1,256}\]|[^\[\]\/\?:#\\]{1,256})(:\d{1,20})?/i); |
There was a problem hiding this comment.
I found one edge case I’d like to address before merging: in the no-DOM fallback, the regex can skip the request’s actual host and use a hostname from a URL embedded in the path or query. That can cause the SDK to attach correlation headers to the wrong destination.
Please limit parsing to the outer URL’s authority and add a no-DOM regression test. If the destination can’t be determined, the SDK should omit correlation headers.
There was a problem hiding this comment.
"Addressed in the latest commit: anchored the regex to ^ with authority boundary lookahead so embedded URLs in the path/query are ignored, and added a no-DOM regression test."
Radhika Gupta (rads-1996)
left a comment
There was a problem hiding this comment.
Can you also add a CHANGELOG entry in RELEASES.md?
| } | ||
| }); | ||
|
|
||
| this.testCase({ |
There was a problem hiding this comment.
Could you add two no-DOM regression tests with the fix? One should verify that a URL embedded in the path or query never supplies the request host. The other should verify that correlationIdCanIncludeCorrelationHeader returns false when the destination can’t be determined, even without a domain allowlist. That second case makes sure we don’t fix the parsing but still allow headers on an empty host.
There was a problem hiding this comment.
Added both no-DOM regression tests in Util.tests.ts:
- UrlHelper: URLs embedded in path or query never supply request host without DOM: Verifies that relative paths or outer URLs with embedded URLs in query parameters or paths never supply the host, and outer URLs with empty authority fail to parse rather than falling back to an embedded URL.
- CorrelationidHelper: canIncludeCorrelationHeader returns false when destination cannot be determined without DOM: Verifies that correlationIdCanIncludeCorrelationHeader returns false on relative URLs, URLs with embedded targets, and empty authority URLs, even without a domain allowlist or when enableCorsCorrelation is true.
| }); | ||
|
|
||
| this.testCase({ | ||
| name: "UrlHelper: parseUrl and correlationIdCanIncludeCorrelationHeader treat backslash as authority boundary without DOM", |
There was a problem hiding this comment.
The parser assertions look good. It’d also be useful to check the dependency telemetry target directly in the no-DOM path. A single URL with fake credentials and distinct path, query, and fragment values would be enough: assert that target is exactly the expected host and non-default port.
There was a problem hiding this comment.
Added a dedicated no-DOM regression test (RemoteDependencyData: dependency telemetry target is expected host and non-default port without DOM) in Util.tests.ts.
It tests with document.createElement = null using a URL with fake credentials and distinct path, query, and fragment values (https://fakeuser:fakepassword@service.example.com:8443/api/v1/resource?param=value#section), and directly asserts that both RemoteDependencyData.target and AjaxHelperParseDependencyPath.target equal exactly service.example.com:8443.
… empty host - Anchor regex to start of string and enforce authority boundaries with lookahead in urlParseFullHost and urlParseHost so embedded URLs in path/query are not treated as request host - Omit correlation headers in correlationIdCanIncludeCorrelationHeader when destination host cannot be determined - Add changelog entry in RELEASES.md - Add no-DOM regression tests for embedded URLs, unresolvable destination host, and dependency telemetry target
"Done! Added the changelog entry in RELEASES.md." |
|
Hi ma'am, Thank you for the review! I have addressed all the feedback in the latest commit:
All 999 unit tests are passing! |

Fixes #2799
Summary of the Problem When the SDK runs in environments without the DOM (like Web Workers or Node.js), it falls back to regex-based helpers urlParseFullHost and urlParseHost to extract the host and port from a URL. We noticed two bugs with this:
IPv6 URLs: For URLs using bracketed IPv6 literals (for example, http://[::1]:8080), the parser stopped reading at the first colon inside the brackets and returned just [ instead of [::1]:8080.
URLs with credentials (userinfo): For URLs that contain authentication information (like https://user:password@example.com), the parser mistakenly returned the username user as the host instead of ignoring the credentials.
Because of this, distributed tracing headers were being dropped whenever a request targeted an IPv6 host or included ports with 80/443, because the SDK thought the host was [ instead of the actual server.
What was changed
Updated the regex in urlParseFullHost and urlParseHost to properly support bracketed IPv6 addresses ([...]) and standard hostnames, while safely stripping out any username:password@ credentials before reading the host.
Maintained exact regex capture group positions so no other logic is affected.
Added comprehensive unit tests in Util.tests.ts covering IPv6 addresses, URLs with user credentials, and correlation header verification.
Testing Done
Ran all 995 unit tests in the core package (qunit:core), and all 995 passed with 0 failures.
Successfully built the @microsoft/applicationinsights-core-js package using Rush.
Verified that PURE annotation and ESLint checks pass with 0 errors.