🐛 Resolve document-relative URLs against document.baseURI in normalizeUrl - #4943
Open
odonckers wants to merge 1 commit into
Open
🐛 Resolve document-relative URLs against document.baseURI in normalizeUrl#4943odonckers wants to merge 1 commit into
odonckers wants to merge 1 commit into
Conversation
…eUrl normalizeUrl resolved relative URLs against location.href, but the browser resolves relative request URLs against the document base URI (<base href>). On a route deeper than the base href, the URL recorded for a fetch/XHR differed from the one actually requested, so the resource event never paired with its PerformanceResourceTiming entry — dropping method, status_code, _dd.trace_id and _dd.span_id and breaking RUM → APM correlation. Resolve against document.baseURI, falling back to location.href where there is no document (workers, SSR). Absolute and root-relative inputs are unaffected.
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Fixes #4944.
normalizeUrlresolves relative URLs againstlocation.href, but the browser resolves relativerequest URLs against the document base URI (
<base href>). On any page whose path is deeperthan the base href, the URL the RUM SDK records for a fetch/XHR differs from the URL the browser
actually requested. The resulting resource event cannot be matched to its
PerformanceResourceTimingentry, so it silently losesmethod,status_code,_dd.trace_idand
_dd.span_id— breaking RUM → APM correlation.This affects any app that issues document-relative request URLs (no leading
/, no scheme)from a route deeper than its base href — the default shape for SPAs served at
/with a router:<base href="/">plus an API base ofapi/.With
<base href="/">on route/deep/route, a request toapi/foogives:PerformanceResourceTiming.namehttps://host/api/foonormalizeUrl(basehttps://host/deep/route)https://host/deep/api/footraceparent/x-datadog-*headers are still injected (tracing usually prefix-matches on origin,which the mis-resolved URL preserves), so the backend span looks correct — the breakage is only
visible on the RUM side, where the resource event carries no
_dd.trace_id.Changes
packages/js-core/src/util/urlPolyfill.ts— resolve againstdocument.baseURI, falling back tolocation.hrefwhere there is no document:is the large majority of
normalizeUrlcalls (view URLs, resource entry names, intake URLs).<base href>differing from the current path — where the new result is what the browseractually requested.
location.hreffallback.@datadog/js-corepublic API surface is unaffected(
api:checkpasses).Test instructions
Unit tests (added in
packages/browser-core/src/tools/utils/urlPolyfill.spec.ts) cover a<base href>differing from the current path:Manual reproduction:
/deep/routewith<base href="/">and RUM initialised withallowedTracingUrls: [window.location.origin].new XMLHttpRequest().open('GET', 'api/foo')/fetch('api/foo').traceparentbut the RUM resource event has no_dd.trace_id,_dd.span_id,methodorstatus_code. After this change, all four arepresent and the resource event matches its
PerformanceResourceTimingentry.Checklist