|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { createHash } from 'node:crypto'; |
| 5 | +import { |
| 6 | + verifyIntegrity, |
| 7 | + formatIntegrityViolation, |
| 8 | + type IntegrityFile, |
| 9 | +} from './plugin-artifact-integrity.js'; |
| 10 | + |
| 11 | +function sri(data: Uint8Array, alg = 'sha256'): string { |
| 12 | + return `${alg}-${createHash(alg).update(data).digest('base64')}`; |
| 13 | +} |
| 14 | + |
| 15 | +const codeBytes = new Uint8Array(Buffer.from('export const x = 1;\n')); |
| 16 | +const assetBytes = new Uint8Array(Buffer.from('body { color: red }\n')); |
| 17 | + |
| 18 | +function files(): IntegrityFile[] { |
| 19 | + return [ |
| 20 | + { path: 'dist/index.mjs', data: codeBytes }, |
| 21 | + { path: 'assets/app.css', data: assetBytes }, |
| 22 | + ]; |
| 23 | +} |
| 24 | + |
| 25 | +describe('verifyIntegrity', () => { |
| 26 | + it('passes when every declared digest matches and every file is declared', () => { |
| 27 | + const res = verifyIntegrity(files(), { |
| 28 | + 'dist/index.mjs': sri(codeBytes), |
| 29 | + 'assets/app.css': sri(assetBytes), |
| 30 | + }); |
| 31 | + expect(res).toEqual({ ok: true, skipped: false, checked: 2, violations: [] }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('refuses on a single-file digest mismatch, naming declared and actual', () => { |
| 35 | + const declared = sri(new Uint8Array(Buffer.from('tampered'))); |
| 36 | + const res = verifyIntegrity(files(), { |
| 37 | + 'dist/index.mjs': declared, |
| 38 | + 'assets/app.css': sri(assetBytes), |
| 39 | + }); |
| 40 | + expect(res.ok).toBe(false); |
| 41 | + expect(res.skipped).toBe(false); |
| 42 | + expect(res.checked).toBe(2); |
| 43 | + expect(res.violations).toEqual([ |
| 44 | + { kind: 'digest_mismatch', path: 'dist/index.mjs', declared, actual: sri(codeBytes) }, |
| 45 | + ]); |
| 46 | + }); |
| 47 | + |
| 48 | + it('refuses when a declared entry has no corresponding file', () => { |
| 49 | + const res = verifyIntegrity([files()[0]], { |
| 50 | + 'dist/index.mjs': sri(codeBytes), |
| 51 | + 'assets/app.css': sri(assetBytes), |
| 52 | + }); |
| 53 | + expect(res.ok).toBe(false); |
| 54 | + expect(res.violations).toEqual([ |
| 55 | + { kind: 'missing_file', path: 'assets/app.css', declared: sri(assetBytes) }, |
| 56 | + ]); |
| 57 | + }); |
| 58 | + |
| 59 | + it('refuses on a file the integrity map does not declare (stale-map drift)', () => { |
| 60 | + const res = verifyIntegrity(files(), { 'dist/index.mjs': sri(codeBytes) }); |
| 61 | + expect(res.ok).toBe(false); |
| 62 | + expect(res.violations).toEqual([{ kind: 'extra_file', path: 'assets/app.css' }]); |
| 63 | + }); |
| 64 | + |
| 65 | + it('absent map is a permissive pass (the manifest field is optional): ok + skipped, nothing checked', () => { |
| 66 | + for (const absent of [undefined, null] as const) { |
| 67 | + const res = verifyIntegrity(files(), absent); |
| 68 | + expect(res).toEqual({ ok: true, skipped: true, checked: 0, violations: [] }); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it('exempt paths are outside the map coverage in both directions', () => { |
| 73 | + const manifestFile: IntegrityFile = { |
| 74 | + path: 'objectstack.plugin.json', |
| 75 | + data: new Uint8Array(Buffer.from('{}')), |
| 76 | + }; |
| 77 | + const res = verifyIntegrity([...files(), manifestFile], { |
| 78 | + 'dist/index.mjs': sri(codeBytes), |
| 79 | + 'assets/app.css': sri(assetBytes), |
| 80 | + // A (mis)declared exempt entry is skipped rather than compared. |
| 81 | + 'objectstack.plugin.json': 'sha256-not-checked', |
| 82 | + }, { exempt: ['objectstack.plugin.json'] }); |
| 83 | + expect(res).toEqual({ ok: true, skipped: false, checked: 2, violations: [] }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('verifies sha384/sha512 SRI digests by their own algorithm', () => { |
| 87 | + const res = verifyIntegrity(files(), { |
| 88 | + 'dist/index.mjs': sri(codeBytes, 'sha512'), |
| 89 | + 'assets/app.css': sri(assetBytes, 'sha384'), |
| 90 | + }); |
| 91 | + expect(res.ok).toBe(true); |
| 92 | + expect(res.checked).toBe(2); |
| 93 | + }); |
| 94 | + |
| 95 | + it('an unrecognized digest shape is a mismatch (compared as sha256), never a silent pass', () => { |
| 96 | + const res = verifyIntegrity([files()[0]], { 'dist/index.mjs': 'md5-abc' }); |
| 97 | + expect(res.ok).toBe(false); |
| 98 | + expect(res.violations[0]).toMatchObject({ |
| 99 | + kind: 'digest_mismatch', |
| 100 | + path: 'dist/index.mjs', |
| 101 | + declared: 'md5-abc', |
| 102 | + actual: sri(codeBytes), |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('reports every violation, deterministically ordered (map order, then sorted extras)', () => { |
| 107 | + const res = verifyIntegrity( |
| 108 | + [files()[1], { path: 'dist/extra.mjs', data: codeBytes }], |
| 109 | + { |
| 110 | + 'dist/index.mjs': sri(codeBytes), |
| 111 | + 'assets/app.css': sri(codeBytes), // wrong bytes declared |
| 112 | + }, |
| 113 | + ); |
| 114 | + expect(res.ok).toBe(false); |
| 115 | + expect(res.violations.map((v) => `${v.kind}:${v.path}`)).toEqual([ |
| 116 | + 'missing_file:dist/index.mjs', |
| 117 | + 'digest_mismatch:assets/app.css', |
| 118 | + 'extra_file:dist/extra.mjs', |
| 119 | + ]); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe('formatIntegrityViolation', () => { |
| 124 | + it('renders one actionable line per kind', () => { |
| 125 | + expect( |
| 126 | + formatIntegrityViolation({ kind: 'digest_mismatch', path: 'a', declared: 'sha256-x', actual: 'sha256-y' }), |
| 127 | + ).toContain('digest mismatch'); |
| 128 | + expect(formatIntegrityViolation({ kind: 'missing_file', path: 'a', declared: 'sha256-x' })).toContain( |
| 129 | + 'absent from the artifact', |
| 130 | + ); |
| 131 | + expect(formatIntegrityViolation({ kind: 'extra_file', path: 'a' })).toContain('not in the integrity map'); |
| 132 | + }); |
| 133 | +}); |
0 commit comments