Skip to content

[Core] Fix urlParseFullHost and urlParseHost for IPv6 literals and URLs with userinfo (#2799) - #2800

Open
viradiyashivali-cmd wants to merge 5 commits into
microsoft:mainfrom
viradiyashivali-cmd:fix-url-parse-ipv6-userinfo
Open

viradiyashivali-cmd wants to merge 5 commits into
microsoft:mainfrom
viradiyashivali-cmd:fix-url-parse-ipv6-userinfo

Conversation

@viradiyashivali-cmd

Copy link
Copy Markdown

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.

… userinfo (microsoft#2799)

Support bracketed IPv6 addresses and strip userinfo in urlParseFullHost and urlParseHost. Fixes microsoft#2799.
@viradiyashivali-cmd
viradiyashivali-cmd requested a review from a team as a code owner November 15, 2026 19:12
Copilot AI balanced review requested due to automatic review settings November 15, 2026 19:12
@viradiyashivali-cmd

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI 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.

🟡 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

viradiyashivali-cmd and others added 2 commits November 16, 2026 00:46
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.
@viradiyashivali-cmd

Copy link
Copy Markdown
Author

Great catch! Thank you for pointing this out.

I have updated the regexes in urlParseFullHost and urlParseHost to treat backslashes (\) as authority boundaries alongside /, ?, and #.

With this change:

  • For https://external.example:8080\@trusted.example/api, the parser correctly stops at \ and extracts external.example:8080 (or external.example when excluding port), treating \@trusted.example/api as part of the path rather than consuming it as user credentials.
  • Added a dedicated no-DOM regression test (UrlHelper: parseUrl and correlationIdCanIncludeCorrelationHeader treat backslash as authority boundary without DOM) verifying that with document.createElement = null, urlParseUrl resolves the host to external.example:8080 and correlationIdCanIncludeCorrelationHeader rejects sending correlation headers when only trusted.example is allowlisted.

Pushed in the latest commit!

@viradiyashivali-cmd

Copy link
Copy Markdown
Author

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.
@viradiyashivali-cmd

Copy link
Copy Markdown
Author

Both review items have been resolved:

  1. Backslash authority boundaries:

    • Backslashes (\) are now treated as authority boundaries alongside /, ?, and # in both urlParseFullHost and urlParseHost.
    • For https://external.example:8080\@trusted.example/api, the host is correctly identified as external.example:8080 rather than consuming the backslash into userinfo.
    • Added a dedicated no-DOM regression test verifying urlParseUrl and correlationIdCanIncludeCorrelationHeader with document.createElement = null.
  2. Empty userinfo and multiple @:

    • The userinfo non-capturing group (?:[^\/\\\?#]*@)? consumes greedily through the last @ within the authority and permits empty userinfo (@...).
    • For https://@api.example.com:8080/api and https://user:p@ss@api.example.com:8080/api, the host is correctly captured as api.example.com:8080 without exposing credentials or producing empty targets.
    • Added regression test cases covering both DOM and no-DOM runtimes.

All 996 unit tests are passing!

@viradiyashivali-cmd

Copy link
Copy Markdown
Author

Hector Hernandez (@hectorhdzg) Any suggestions on this PR ?

@rads-1996

Copy link
Copy Markdown
Member

Hector Hernandez (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.

@viradiyashivali-cmd

Copy link
Copy Markdown
Author

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

"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."

@rads-1996 Radhika Gupta (rads-1996) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also add a CHANGELOG entry in RELEASES.md?

}
});

this.testCase({

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added both no-DOM regression tests in Util.tests.ts:

  1. 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.
  2. 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",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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
@viradiyashivali-cmd

Copy link
Copy Markdown
Author

Can you also add a CHANGELOG entry in RELEASES.md?

"Done! Added the changelog entry in RELEASES.md."

@viradiyashivali-cmd

Copy link
Copy Markdown
Author

Hi ma'am,

Thank you for the review! I have addressed all the feedback in the latest commit:

  1. Outer authority boundary:

    • Anchored urlParseFullHost to the beginning of the string (^\s*(\w{1,150}):\/\/) and added a lookahead (?=[\/\\\?#]|$) terminating the authority. Any embedded URLs in the path or query (e.g. /path?url=https://...) are now strictly ignored.
    • Anchored urlParseHost to ^...$.
  2. Omit correlation headers on unresolvable destinations:

    • Updated correlationIdCanIncludeCorrelationHeader to return false immediately when requestHost cannot be determined, ensuring correlation headers are omitted even when no domain allowlist is configured.
  3. Changelog entry:

    • Added the bug fix entry under Unreleased Changes in RELEASES.md.
  4. Regression tests added:

    • Added no-DOM regression test verifying that URLs embedded in paths/queries never supply the request host.
    • Added no-DOM regression test verifying that correlationIdCanIncludeCorrelationHeader returns false when the destination host cannot be determined.
    • Added no-DOM regression test asserting that the dependency telemetry target (via RemoteDependencyData and AjaxHelperParseDependencyPath) correctly extracts the host and non-default port (service.example.com:8443) for URLs with credentials, paths, queries, and fragments.

All 999 unit tests are passing!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] urlParseFullHost and urlParseHost return "[" for IPv6 literals and the username for userinfo URLs

3 participants