Repository navigation
Conversation
Nonce-only script policies block WebMCP because its script is inserted after hydration without the document nonce. Forward the nonce through browser initialization and framework bindings, with regression tests and updated public API docs. Co-authored-by: Codex <codex@openai.com>
|
Oxygen deployed a preview of your
Learn more about Hydrogen's GitHub integration. |
fredericoo
left a comment
There was a problem hiding this comment.
all good thank you for catching that!
only one test that i was able to make pass with bad data, if you can fix that it'd be great (suggested a fix right there)
| it.each([undefined, "test-nonce", ""])("loads WebMCP with nonce %s", async (nonce) => { | ||
| (navigator as any).modelContext = { registerTool: vi.fn() }; | ||
|
|
||
| await expect(initializeShopifyScripts({ routes: emptyRouteTemplates })).resolves.toBe(true); | ||
| await expect(initializeShopifyScripts({ routes: emptyRouteTemplates, nonce })).resolves.toBe( | ||
| true, | ||
| ); | ||
|
|
||
| expect(loadScript).toHaveBeenCalledWith(SHOPIFY_STOREFRONT_WEBMCP_SCRIPT, { | ||
| in: "head", | ||
| attributes: { id: "shopify-webmcp", crossorigin: "anonymous" }, | ||
| attributes: { | ||
| id: "shopify-webmcp", | ||
| crossorigin: "anonymous", | ||
| ...(nonce !== undefined ? { nonce } : {}), | ||
| }, | ||
| }); | ||
| }); |
There was a problem hiding this comment.
non-blocking: the undefined case can't catch a nonce="undefined" regression.
toHaveBeenCalledWith ignores undefined properties, so if webmcp.ts always passed nonce, loadScript would setAttribute("nonce", "undefined") and this still passes. The expected value also re-implements the production spread, so it mirrors the implementation rather than pinning it.
Reproduced: swapped the spread in webmcp.ts for nonce: nonce as string and all 3 cases still pass. With the version below, the undefined case fails.
| it.each([undefined, "test-nonce", ""])("loads WebMCP with nonce %s", async (nonce) => { | |
| (navigator as any).modelContext = { registerTool: vi.fn() }; | |
| await expect(initializeShopifyScripts({ routes: emptyRouteTemplates })).resolves.toBe(true); | |
| await expect(initializeShopifyScripts({ routes: emptyRouteTemplates, nonce })).resolves.toBe( | |
| true, | |
| ); | |
| expect(loadScript).toHaveBeenCalledWith(SHOPIFY_STOREFRONT_WEBMCP_SCRIPT, { | |
| in: "head", | |
| attributes: { id: "shopify-webmcp", crossorigin: "anonymous" }, | |
| attributes: { | |
| id: "shopify-webmcp", | |
| crossorigin: "anonymous", | |
| ...(nonce !== undefined ? { nonce } : {}), | |
| }, | |
| }); | |
| }); | |
| it.each([ | |
| { nonce: undefined, attributes: { id: "shopify-webmcp", crossorigin: "anonymous" } }, | |
| { | |
| nonce: "test-nonce", | |
| attributes: { id: "shopify-webmcp", crossorigin: "anonymous", nonce: "test-nonce" }, | |
| }, | |
| { nonce: "", attributes: { id: "shopify-webmcp", crossorigin: "anonymous", nonce: "" } }, | |
| ])("loads WebMCP with nonce $nonce", async ({ nonce, attributes }) => { | |
| (navigator as any).modelContext = { registerTool: vi.fn() }; | |
| await expect(initializeShopifyScripts({ routes: emptyRouteTemplates, nonce })).resolves.toBe( | |
| true, | |
| ); | |
| expect(vi.mocked(loadScript).mock.lastCall).toStrictEqual([ | |
| SHOPIFY_STOREFRONT_WEBMCP_SCRIPT, | |
| { in: "head", attributes }, | |
| ]); | |
| }); |
WebMCP's script is inserted after hydration without the document's CSP nonce. A nonce-only
script-src-elemblocks it even whenShopifyScriptsreceives the nonce. Forward that existing value through browser initialization to the dynamic script loader.Before
Server-rendered Shopify scripts receive
csp.nonce, but the dynamically inserted WebMCP script does not and is blocked by this policy.After
React and Vue forward the existing
<ShopifyScripts nonce={nonce} />value to WebMCP automatically. Custom framework integrations can pass the same document nonce when initializing scripts:What this changes
initializeShopifyScriptsand the WebMCP loader to the script's attributes before insertion.Developer impact
Adds an optional
noncefield toInitializeShopifyScriptsOptions. React and Vue integrations use the nonce already supplied toShopifyScripts.Includes a patch changeset for fixing nonce propagation to an existing dynamically loaded script. No new exports.
Verified in the built React Router template and its dev server: the CSP response header, SSR bootstrap, and dynamically inserted WebMCP script share the same nonce with no hydration errors. The browser probe supplied WebMCP support and intercepted its CDN response. A separate Chromium check confirmed that a nonce-only policy blocks a dynamic script without its nonce and allows it with the nonce.
How to Test
From the repository root, run
pnpm install && pnpm run build:pkgsand thenpnpm dev:rr. Use the local URL printed by Vite.Open the template in a browser with WebMCP enabled (
navigator.modelContextordocument.modelContextmust be present). Without a configured store, the template uses its tokenless mock.shop fallback.In DevTools, confirm the
shopify-webmcpscript loads. Compare its nonce with the bootstrap's:Confirm both values match the nonce in the document's CSP response header. Read
.nonce; browsers conceal the value fromgetAttribute('nonce').