Skip to content

fix: unblock Microsoft Clarity in CSP and send consent with correct keys - #1736

Merged
BenjaminMichaelis merged 3 commits into
mainfrom
fix/clarity-csp-and-consent
Nov 27, 2026
Merged

BenjaminMichaelis merged 3 commits into
mainfrom
fix/clarity-csp-and-consent

Conversation

@BenjaminMichaelis

Copy link
Copy Markdown
Member

Summary

Microsoft Clarity has not been running for any visitor. This PR unblocks it and fixes how analytics consent reaches it.

1. CSP blocked Clarity's tracker

script-src allowed only www.clarity.ms. That host serves only the small tag; the tag then loads the tracker from https://scripts.clarity.ms/<version>/clarity.js, which the browser blocked:

Loading the script 'https://scripts.clarity.ms/0.8.77/clarity.js' violates the following Content Security Policy directive: "script-src 'self' 'unsafe-inline' cdn.jsdelivr.net www.clarity.ms ..."

Following Clarity's CSP guidance, script-src now allows https://*.clarity.ms and https://c.bing.com, and connect-src adds https://c.bing.com. www.clarity.ms was added to the CSP in 5fe0bb6 (2026-04-24), so Clarity has probably recorded nothing since then. The Clarity dashboard should confirm this.

connect-src also adds https://js.monitor.azure.com, so Application Insights can fetch its CDN config (ai.config.1.cfg.json). Today that fetch is blocked and the SDK falls back to its defaults.

2. Clarity ignored our consent call

The Consent API v2 requires both ad_Storage and analytics_Storage, spelled with a capital S. We sent lowercase analytics_storage. In Clarity's source (clarity-js/src/data/metadata.ts), a missing key falls back to the current state, so our call did nothing. Consent reached Clarity only indirectly, through its Google Consent Mode listener. That path fails when gtag.js is blocked but Clarity isn't, and Clarity's own FAQ still calls Google Consent Mode support "coming soon".

  • clarity-manager.js now sends { ad_Storage: "denied", analytics_Storage: "granted" | "denied" }. Ads are always denied because the site has no advertising.
  • I removed the duplicate updateClarityConsent() call from consent-manager.js. clarity-manager.js already applies consent through the ecs:consent-changed event.

3. Google Consent Mode defaults

The gtag('consent', 'default', …) call now explicitly denies ad_storage, ad_user_data and ad_personalization. Google's docs say no consent values are set by default, and Consent Mode v2 expects these two newer parameters to be sent. Clarity also reads ad_storage from Consent Mode.

Tests

The CSP header is only emitted outside Development, and the tests run in Development. So I moved the policy string into Program.BuildContentSecurityPolicy(...), with no change in behavior. The new ContentSecurityPolicyTests checks the Clarity, Application Insights and Google Analytics hosts. All 358 tests pass locally.

Reviewer notes / verification after deploy

  • With ad blockers off, accept analytics on the live site, then run !!clarity.v in the console. It should print true, and the _clck and _clsk cookies should appear.
  • Clarity's verification snippet should print GRANTED after Accept and DENIED after Reject or Withdraw, with the _cl* cookies removed:
    clarity('metadata', (d, u, c) => console.log(c), false, true, true);
  • Clarity dashboard → Settings → Setup: cookies should be off (Consent Mode), as Clarity recommends. This is a dashboard setting, not part of this PR.
  • Once Clarity starts loading, its data volume will jump back up.

BenjaminMichaelis and others added 3 commits November 26, 2026 12:49
The CSP allowed only www.clarity.ms, but Clarity's tag loads its tracker
from scripts.clarity.ms, so the browser blocked Clarity for every visitor.
Allow https://*.clarity.ms and https://c.bing.com per Clarity's CSP docs,
and allow js.monitor.azure.com in connect-src for the Application Insights
CDN config fetch.

Clarity's consentv2 API reads ad_Storage/analytics_Storage (capital S) and
silently ignores lowercase keys, so our consent signal was a no-op and
consent only reached Clarity indirectly through Google Consent Mode.
Send both keys with the documented casing (ads always denied) from
clarity-manager.js only, and drop the duplicate call in consent-manager.js.

Explicitly deny ad_storage, ad_user_data and ad_personalization in the
gtag consent default, since unset consent types are treated as granted.

Extract the CSP into Program.BuildContentSecurityPolicy so tests can check
the analytics hosts; the header is only emitted outside Development.
Define each shared source group once (hCaptcha, Clarity) and splice it into
the directives that need it with collection-expression spreads, instead of
repeating hosts across interpolated strings. The TryDotNet origin and the
Application Insights ingestion endpoint are now returned as source lists, so
an absent or invalid value simply adds nothing (no leading-space strings).

The emitted policy is unchanged except for dropping
https://newassets.hcaptcha.com from frame-src, which https://*.hcaptcha.com
already covers.

Tests cover configured origins, invalid origins, and the directive order and
spacing of the policy string.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Development now sends the same policy as Content-Security-Policy-Report-Only,
  so a host the policy would block shows up as a browser console violation
  locally instead of failing silently in production (as Clarity did). Nothing
  is blocked in Development.
- script-src allows only https://cdn.jsdelivr.net/npm/@docsearch/ instead of
  all of cdn.jsdelivr.net; DocSearch is the only script loaded from jsDelivr.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@BenjaminMichaelis
BenjaminMichaelis marked this pull request as ready for review November 27, 2026 02:11
@BenjaminMichaelis
BenjaminMichaelis merged commit 383489c into main Nov 27, 2026
14 checks passed
@BenjaminMichaelis
BenjaminMichaelis deleted the fix/clarity-csp-and-consent branch November 27, 2026 02:12
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.

1 participant