Skip to content

♻️ detect cookie API availability when selecting session store strategy#4624

Open
thomas-lebeau wants to merge 2 commits into
thomas.lebeau/e2e-latest-browsers-in-cifrom
thomas.lebeau/cookie-api-detection
Open

♻️ detect cookie API availability when selecting session store strategy#4624
thomas-lebeau wants to merge 2 commits into
thomas.lebeau/e2e-latest-browsers-in-cifrom
thomas.lebeau/cookie-api-detection

Conversation

@thomas-lebeau
Copy link
Copy Markdown
Collaborator

@thomas-lebeau thomas-lebeau commented May 14, 2026

Motivation

In WebKit (Safari) on localhost-based subdomains, window.cookieStore is exposed and cookieStore.set(...) resolves without throwing — but the cookie is silently dropped because localhost is not a registrable domain per RFC 6761 (Safari rejects explicit domain= attributes for non-registrable hosts). This breaks session persistence end-to-end on the latest WebKit in our e2e setup (which uses foo.bar.localhost), even though document.cookie writes still succeed.

Previously, the SDK preferred the CookieStore API whenever window.cookieStore was present, with no end-to-end verification that a cookie actually round-trips. This change makes the session store strategy probe each API by writing and reading back a test cookie, and records the chosen API on the strategy so the runtime uses the same one that was verified during initialization.

Changes

  • Move areCookiesAuthorized into cookieAccess.ts and make it async, taking a CookieAccess factory so it can verify each API end-to-end.
  • Split createCookieAccess into createCookieStoreAccess and createDocumentCookieAccess.
  • selectCookieStrategy probes the CookieStore API first and falls back to document.cookie, recording the chosen cookieApi on the strategy type.
  • Thread cookieApi through CookieSessionStoreStrategyType and initCookieStrategy so the runtime strategy uses the probed API.
  • selectSessionStoreStrategyType is now async and takes Configuration (with cookieOptions/sessionPersistence); sessionManager awaits it during startup.
  • Drop areCookiesAuthorized from the public core index export.
  • CI: run latest Firefox and WebKit alongside Chromium; default local yarn test:e2e to Chromium for faster feedback.

Test instructions

  • Reproduce the original WebKit failure (before this change) and verify it's fixed by running the sessionStore e2e suite on WebKit:
    yarn test:e2e:ci --project=webkit -g sessionStore
    
    Expected: 11 passed, 1 skipped (the firefox-only iframe test).
  • Run unit tests: yarn test:unit --spec packages/core/src/browser/cookieAccess.spec.ts and yarn test:unit --spec packages/core/src/domain/session/sessionStore.spec.ts.
  • Verify in a browser with cookieStore available (e.g. Chromium) that the SDK selects the cookieStore API; in WebKit on localhost, verify it falls back to document.cookie.

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

@thomas-lebeau thomas-lebeau changed the base branch from main to thomas.lebeau/e2e-latest-browsers-in-ci May 14, 2026 11:16
@thomas-lebeau thomas-lebeau marked this pull request as ready for review May 14, 2026 11:38
@thomas-lebeau thomas-lebeau requested a review from a team as a code owner May 14, 2026 11:38
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: dac2e44ca3

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

deleteCookie,
resetInitCookies,
} from './browser/cookie'
export { getCookie, getInitCookie, setCookie, deleteCookie, resetInitCookies } from './browser/cookie'
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restore the areCookiesAuthorized root export

When downstream code imports areCookiesAuthorized from the published @datadog/browser-core package, this changed root export no longer exposes the symbol that existed before this commit, so upgrades fail with a missing export even though the helper still exists internally. If the API needs to change, keep a compatibility export/wrapper or otherwise preserve the public root surface for existing consumers.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is fine, people are not supposed to use @datadog/browser-core and I did not find any other package in the dataDog github org that uses it.

@thomas-lebeau thomas-lebeau force-pushed the thomas.lebeau/e2e-latest-browsers-in-ci branch 2 times, most recently from 573ba97 to 0c3132a Compare May 15, 2026 19:21
- Move areCookiesAuthorized from cookie.ts to cookieAccess.ts and make it async, taking a CookieAccess factory so it can verify each API end-to-end
- Split createCookieAccess into createCookieStoreAccess and createDocumentCookieAccess and export both
- selectCookieStrategy now probes the CookieStore API first and falls back to document.cookie, recording the chosen cookieApi on the strategy type
- Thread cookieApi through CookieSessionStoreStrategyType and initCookieStrategy so the runtime strategy uses the same API that was probed
- selectSessionStoreStrategyType is async and takes Configuration (with cookieOptions/sessionPersistence) instead of InitConfiguration; sessionManager awaits it during startup
- Drop areCookiesAuthorized from the public core index export
@thomas-lebeau thomas-lebeau force-pushed the thomas.lebeau/cookie-api-detection branch from dac2e44 to 84b60e7 Compare May 16, 2026 06:33
@thomas-lebeau
Copy link
Copy Markdown
Collaborator Author

/trigger-ci

@gh-worker-devflow-routing-ef8351
Copy link
Copy Markdown

gh-worker-devflow-routing-ef8351 Bot commented May 16, 2026

View all feedbacks in Devflow UI.

2026-05-16 06:34:13 UTC ℹ️ Start processing command /trigger-ci


2026-05-16 06:34:17 UTC ℹ️ Gitlab pipeline started

Started pipeline #113522402

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

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

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 169.51 KiB 169.77 KiB +263 B +0.15%
Rum Profiler 5.97 KiB 5.97 KiB 0 B 0.00%
Rum Recorder 21.23 KiB 21.23 KiB 0 B 0.00%
Logs 54.70 KiB 54.93 KiB +244 B +0.44%
Rum Slim 127.85 KiB 128.08 KiB +238 B +0.18%
Worker 22.99 KiB 22.99 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base CPU Time (ms) Local CPU Time (ms) 𝚫%
RUM - add global context 0.0027 0.0022 -18.52%
RUM - add action 0.0149 0.0109 -26.85%
RUM - add error 0.0133 0.0105 -21.05%
RUM - add timing 0.0007 0.0005 -28.57%
RUM - start view 0.0117 0.0134 +14.53%
RUM - start/stop session replay recording 0.0012 0.0008 -33.33%
Logs - log message 0.0184 0.018 -2.17%
🧠 Memory Performance
Action Name Base Memory Consumption Local Memory Consumption 𝚫
RUM - add global context 38.42 KiB 39.51 KiB +1.09 KiB
RUM - add action 67.15 KiB 67.63 KiB +495 B
RUM - add timing 38.76 KiB 39.67 KiB +933 B
RUM - add error 71.49 KiB 70.13 KiB -1.36 KiB
RUM - start/stop session replay recording 45.87 KiB 40.89 KiB -4.98 KiB
RUM - start view 487.17 KiB 487.27 KiB +100 B
Logs - log message 70.08 KiB 110.44 KiB +40.36 KiB

🔗 RealWorld

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

datadog-datadog-prod-us1-2 Bot commented May 16, 2026

Pipelines  Tests

Fix all issues with BitsAI

⚠️ Warnings

🚦 1 Pipeline job failed

DataDog/browser-sdk | check-staging-merge   View in Datadog   GitLab

⬆️ Not caused by your changes. Rebase on a base branch once a fix is merged. Merge conflict in packages/core/src/domain/session/storeStrategies/sessionInCookie.ts. Please resolve conflicts and then commit the result.

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 80.65%
Overall Coverage: 77.02% (+0.06%)

Useful? React with 👍 / 👎

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

trackingConsentState
)

// Allow startSessionManager to await selectSessionStoreStrategyType and call setSessionState
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.

minor

maybe it's time to add smth like flushPromises helper to test utils? I didn't find anything similar

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

you're right, looks like we have waitNextMicrotask() already, so using that.

- Replace `await Promise.resolve()` with `waitNextMicrotask()` for clarity
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.

2 participants