Skip to content

Commit ede57c7

Browse files
committed
test(bundle): assert exact SRI value, not just sha384 prefix
1 parent 1fcdabc commit ede57c7

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

test/unit/bundle-two-deploy-repro.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Same source URL, different content between deployments must yield different public URLs,
33
// otherwise a long-cached asset serves stale bytes against a fresh SRI hash.
44
import type { AssetBundlerTransformerOptions } from '../../packages/script/src/plugins/transform'
5+
import { createHash } from 'node:crypto'
56
import { hash } from 'ohash'
67
import { hasProtocol } from 'ufo'
78
import { describe, expect, it, vi } from 'vitest'
@@ -100,13 +101,15 @@ describe('two-deploy bundle repro (#724)', () => {
100101
})
101102

102103
it('integrity hash matches the final served bytes', async () => {
103-
mockUpstreamBody(Buffer.from('/* adsbygoogle v1 */'))
104+
const body = Buffer.from('/* adsbygoogle v1 */')
105+
mockUpstreamBody(body)
104106
const code = await runTransform(src, { integrity: true })
105107
const url = extractPublicUrl(code)
106108
const integrityMatch = code.match(/integrity: '(sha384-[^']+)'/)
107109

110+
const expectedIntegrity = `sha384-${createHash('sha384').update(body).digest('base64')}`
108111
expect(url).toMatch(/[a-f0-9]{16}\.js$/)
109-
expect(integrityMatch).toBeTruthy()
112+
expect(integrityMatch?.[1]).toBe(expectedIntegrity)
110113
// Filename and integrity both derive from the same post-rewrite bytes,
111114
// so they cannot drift apart across deployments.
112115
})

0 commit comments

Comments
 (0)