Skip to content

🐛 [RUM-11848] Guard window/location access for non-browser environments#4674

Merged
mormubis merged 1 commit into
mainfrom
adlrb/rum-11848-location-not-defined
May 29, 2026
Merged

🐛 [RUM-11848] Guard window/location access for non-browser environments#4674
mormubis merged 1 commit into
mainfrom
adlrb/rum-11848-location-not-defined

Conversation

@mormubis
Copy link
Copy Markdown
Contributor

@mormubis mormubis commented May 26, 2026

Motivation

The SDK crashes with ReferenceError: location is not defined and window is not defined when initialized in Web Workers or Node.js. Three code paths access browser globals without guards: normalizeUrl() uses bare location.href, storeContextManager() uses bare window and localStorage, and getCurrentSite() uses location.hostname in a default parameter.

~107 telemetry errors/week across v6.21.x and v7.1.0. Telemetry link.

Relates to RUM-11848.

Changes

Each function now handles non-browser environments gracefully instead of crashing:

  • normalizeUrl() uses globalObject.location?.href as base URL. For absolute URLs (the common case with proxy), the base is ignored anyway.
  • storeContextManager() returns early when window or localStorage are unavailable. It becomes a silent no-op in workers/Node.
  • getCurrentSite() uses safe defaults for hostname and referrer parameters instead of bare location/document access.

Test instructions

  1. yarn dev
  2. Open the sandbox in a browser
  3. Run this in the console to verify the SDK initializes in a Worker without errors:
const workerCode = `
  importScripts(location.origin + '/datadog-logs.js');
  try {
    DD_LOGS.init({
      clientToken: 'test-token',
      site: 'datadoghq.eu',
      storeContextsAcrossPages: true,
    });
    DD_LOGS.logger.info('Hello from worker');
    self.postMessage({ status: 'ok' });
  } catch (e) {
    self.postMessage({ status: 'error', message: e.message });
  }
`;
const blob = new Blob([workerCode], { type: 'application/javascript' });
const worker = new Worker(URL.createObjectURL(blob));
worker.onmessage = (e) => { console.log('Worker result:', e.data); worker.terminate(); };

Expected: { status: 'ok' }. Without the fix: ReferenceError: window is not defined.

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

@cit-pr-commenter-54b7da
Copy link
Copy Markdown

cit-pr-commenter-54b7da Bot commented May 26, 2026

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 173.64 KiB 173.79 KiB +155 B +0.09%
Rum Profiler 8.08 KiB 8.08 KiB 0 B 0.00%
Rum Recorder 21.21 KiB 21.21 KiB 0 B 0.00%
Logs 55.47 KiB 55.61 KiB +149 B +0.26%
Rum Slim 131.39 KiB 131.53 KiB +149 B +0.11%
Worker 22.99 KiB 22.99 KiB 0 B 0.00%

🔗 RealWorld

@datadog-prod-us1-3
Copy link
Copy Markdown

datadog-prod-us1-3 Bot commented May 26, 2026

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 60.00%
Overall Coverage: 76.76% (-0.01%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: bec77f6 | Docs | Datadog PR Page | Give us feedback!

@mormubis mormubis force-pushed the adlrb/rum-11848-location-not-defined branch from 41392e6 to 7884e3d Compare May 27, 2026 13:08
@mormubis mormubis marked this pull request as ready for review May 28, 2026 09:24
@mormubis mormubis requested a review from a team as a code owner May 28, 2026 09:24
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7884e3df33

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/core/src/browser/cookie.ts
@mormubis mormubis force-pushed the adlrb/rum-11848-location-not-defined branch from 7884e3d to 984726c Compare May 28, 2026 13:46
Comment on lines +84 to +86
if (typeof document === 'undefined') {
return undefined
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I needed to add this bit to actually fix the codex p2.

@mormubis mormubis force-pushed the adlrb/rum-11848-location-not-defined branch from 984726c to bec77f6 Compare May 28, 2026 16:04
@mormubis mormubis merged commit 016e242 into main May 29, 2026
30 checks passed
@mormubis mormubis deleted the adlrb/rum-11848-location-not-defined branch May 29, 2026 09:39
@github-actions github-actions Bot locked and limited conversation to collaborators May 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants