-
Notifications
You must be signed in to change notification settings - Fork 87
fix(bundle): content-address bundled script filenames for stable SRI #725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c83af87
fix(bundle): content-address bundled script filenames for stable SRI
harlan-zw 184120b
test(bundle): mechanical two-deploy repro for #724
harlan-zw 1fcdabc
test(e2e): update bundle snapshots for content-addressed filenames
harlan-zw ede57c7
test(bundle): assert exact SRI value, not just sha384 prefix
harlan-zw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // Mechanical reproduction of nuxt/scripts#724: | ||
| // Same source URL, different content between deployments must yield different public URLs, | ||
| // otherwise a long-cached asset serves stale bytes against a fresh SRI hash. | ||
| import type { AssetBundlerTransformerOptions } from '../../packages/script/src/plugins/transform' | ||
| import { createHash } from 'node:crypto' | ||
| import { hash } from 'ohash' | ||
| import { hasProtocol } from 'ufo' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { NuxtScriptBundleTransformer } from '../../packages/script/src/plugins/transform' | ||
|
|
||
| vi.mock('ohash', async (og) => { | ||
| const mod = await og<typeof import('ohash')>() | ||
| return { ...mod, hash: vi.fn(mod.hash) } | ||
| }) | ||
| vi.mock('ufo', async (og) => { | ||
| const mod = await og<typeof import('ufo')>() | ||
| return { ...mod, hasProtocol: vi.fn(mod.hasProtocol) } | ||
| }) | ||
|
|
||
| const mockBundleStorage: any = { | ||
| getItem: vi.fn(), | ||
| setItem: vi.fn(), | ||
| getItemRaw: vi.fn(), | ||
| setItemRaw: vi.fn(), | ||
| hasItem: vi.fn(), | ||
| } | ||
| vi.mock('../../packages/script/src/assets', () => ({ | ||
| bundleStorage: vi.fn(() => mockBundleStorage), | ||
| })) | ||
|
|
||
| const fetchMock = vi.fn() | ||
| vi.stubGlobal('fetch', fetchMock) | ||
|
|
||
| vi.mock('@nuxt/kit', async (og) => { | ||
| const mod = await og<typeof import('@nuxt/kit')>() | ||
| const nuxt = { | ||
| options: { buildDir: '.nuxt', app: { baseURL: '/' }, runtimeConfig: { app: {} } }, | ||
| hooks: { hook: vi.fn() }, | ||
| } | ||
| return { ...mod, useNuxt: () => nuxt, tryUseNuxt: () => nuxt } | ||
| }) | ||
|
|
||
| vi.mocked(hasProtocol).mockImplementation(() => true) | ||
| // Source URL hash is stable across both "deploys" β the URL doesn't change between deployments, | ||
| // only the upstream bytes do. This is exactly the real-world scenario in #724. | ||
| vi.mocked(hash).mockImplementation(() => 'adsbygoogle') | ||
|
|
||
| async function runTransform(code: string, options?: AssetBundlerTransformerOptions) { | ||
| mockBundleStorage.hasItem.mockResolvedValue(false) | ||
| const plugin = NuxtScriptBundleTransformer({ renderedScript: new Map(), ...options }).vite() as any | ||
| const out = await plugin.transform.handler.call({}, code, 'file.js') | ||
| return out?.code as string | ||
| } | ||
|
|
||
| function mockUpstreamBody(bytes: Buffer) { | ||
| fetchMock.mockResolvedValueOnce({ | ||
| ok: true, | ||
| arrayBuffer: () => Promise.resolve(bytes), | ||
| headers: { get: () => null }, | ||
| _data: bytes, | ||
| } as any) | ||
| } | ||
|
|
||
| function extractPublicUrl(code: string): string { | ||
| const match = code.match(/\/_scripts\/assets\/[^'"]+\.js/) | ||
| if (!match) | ||
| throw new Error(`no public asset URL in: ${code}`) | ||
| return match[0] | ||
| } | ||
|
|
||
| describe('two-deploy bundle repro (#724)', () => { | ||
| const src = `const instance = useScript('https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', { bundle: true })` | ||
|
|
||
| it('same source URL + changed upstream content -> different public filenames', async () => { | ||
| // Deploy 1: upstream returns content A | ||
| mockUpstreamBody(Buffer.from('/* adsbygoogle v1 */ (function(){ /* ... */ })()')) | ||
| const deploy1 = await runTransform(src) | ||
|
|
||
| // Deploy 2: upstream returns content B (Google pushed a JS update) | ||
| mockUpstreamBody(Buffer.from('/* adsbygoogle v2 NEW */ (function(){ /* ... */ })()')) | ||
| const deploy2 = await runTransform(src) | ||
|
|
||
| const url1 = extractPublicUrl(deploy1) | ||
| const url2 = extractPublicUrl(deploy2) | ||
|
|
||
| // With the bug present, these would be identical (URL-hash-only filename), | ||
| // so a long-cached v1 asset would be served against a v2 integrity hash. | ||
| expect(url1).not.toBe(url2) | ||
| expect(url1).toMatch(/[a-f0-9]{16}\.js$/) | ||
| expect(url2).toMatch(/[a-f0-9]{16}\.js$/) | ||
| }) | ||
|
|
||
| it('same source URL + same content -> identical public filenames (caching preserved)', async () => { | ||
| const body = Buffer.from('/* adsbygoogle v1 */ (function(){ /* ... */ })()') | ||
| mockUpstreamBody(body) | ||
| const deploy1 = await runTransform(src) | ||
| mockUpstreamBody(body) | ||
| const deploy2 = await runTransform(src) | ||
|
|
||
| expect(extractPublicUrl(deploy1)).toBe(extractPublicUrl(deploy2)) | ||
| }) | ||
|
|
||
| it('integrity hash matches the final served bytes', async () => { | ||
| const body = Buffer.from('/* adsbygoogle v1 */') | ||
| mockUpstreamBody(body) | ||
| const code = await runTransform(src, { integrity: true }) | ||
| const url = extractPublicUrl(code) | ||
| const integrityMatch = code.match(/integrity: '(sha384-[^']+)'/) | ||
|
|
||
| const expectedIntegrity = `sha384-${createHash('sha384').update(body).digest('base64')}` | ||
| expect(url).toMatch(/[a-f0-9]{16}\.js$/) | ||
| expect(integrityMatch?.[1]).toBe(expectedIntegrity) | ||
| // Filename and integrity both derive from the same post-rewrite bytes, | ||
| // so they cannot drift apart across deployments. | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.