Repository navigation
fix: unblock Microsoft Clarity in CSP and send consent with correct keys - #1736
Merged
Merged
Conversation
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>
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.
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-srcallowed onlywww.clarity.ms. That host serves only the small tag; the tag then loads the tracker fromhttps://scripts.clarity.ms/<version>/clarity.js, which the browser blocked:Following Clarity's CSP guidance,
script-srcnow allowshttps://*.clarity.msandhttps://c.bing.com, andconnect-srcaddshttps://c.bing.com.www.clarity.mswas added to the CSP in 5fe0bb6 (2026-04-24), so Clarity has probably recorded nothing since then. The Clarity dashboard should confirm this.connect-srcalso addshttps://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_Storageandanalytics_Storage, spelled with a capital S. We sent lowercaseanalytics_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.jsnow sends{ ad_Storage: "denied", analytics_Storage: "granted" | "denied" }. Ads are always denied because the site has no advertising.updateClarityConsent()call fromconsent-manager.js.clarity-manager.jsalready applies consent through theecs:consent-changedevent.3. Google Consent Mode defaults
The
gtag('consent', 'default', …)call now explicitly deniesad_storage,ad_user_dataandad_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 readsad_storagefrom 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 newContentSecurityPolicyTestschecks the Clarity, Application Insights and Google Analytics hosts. All 358 tests pass locally.Reviewer notes / verification after deploy
!!clarity.vin the console. It should printtrue, and the_clckand_clskcookies should appear.GRANTEDafter Accept andDENIEDafter Reject or Withdraw, with the_cl*cookies removed:clarity('metadata', (d, u, c) => console.log(c), false, true, true);