Skip to content

Commit f7c74d2

Browse files
authored
feat: support SHA-2 subject digests (#446)
Signed-off-by: Brian DeHamer <[email protected]>
1 parent 88633d1 commit f7c74d2

4 files changed

Lines changed: 238 additions & 20 deletions

File tree

‎__tests__/integration/main.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,5 +662,44 @@ describe('run', () => {
662662
})
663663
)
664664
})
665+
666+
it('should fail when subject-digest uses sha512 with push-to-registry', async () => {
667+
await run({
668+
...registryInputs,
669+
subjectDigest: `sha512:${'a'.repeat(128)}`
670+
})
671+
672+
expect(setFailedMock).toHaveBeenCalledWith(
673+
expect.objectContaining({
674+
message: expect.stringMatching(
675+
/push-to-registry requires a subject with a SHA-256 digest/
676+
)
677+
})
678+
)
679+
expect(mockAttest).not.toHaveBeenCalled()
680+
})
681+
})
682+
683+
describe('non-sha256 digest without registry', () => {
684+
it('should succeed with sha512 subject-digest when not pushing to registry', async () => {
685+
await run({
686+
...defaultInputs,
687+
subjectName: 'artifact',
688+
subjectDigest: `sha512:${'a'.repeat(128)}`,
689+
predicateType: 'https://example.com/predicate',
690+
predicate: '{}'
691+
})
692+
693+
expect(setFailedMock).not.toHaveBeenCalled()
694+
expect(mockAttest).toHaveBeenCalledWith(
695+
expect.objectContaining({
696+
subjects: [
697+
expect.objectContaining({
698+
digest: { sha512: 'a'.repeat(128) }
699+
})
700+
]
701+
})
702+
)
703+
})
665704
})
666705
})

‎__tests__/unit/subject.test.ts‎

Lines changed: 122 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,48 @@ describe('subjectFromInputs', () => {
346346
expect(subjects[0].name).toBe('ghcr.io/foo/bar')
347347
})
348348

349+
// Table-driven acceptance tests for all six canonical SHA-2 algorithms
350+
const algorithmTests: { algorithm: string; hexLength: number }[] = [
351+
{ algorithm: 'sha224', hexLength: 56 },
352+
{ algorithm: 'sha256', hexLength: 64 },
353+
{ algorithm: 'sha384', hexLength: 96 },
354+
{ algorithm: 'sha512', hexLength: 128 },
355+
{ algorithm: 'sha512_224', hexLength: 56 },
356+
{ algorithm: 'sha512_256', hexLength: 64 }
357+
]
358+
359+
it.each(algorithmTests)(
360+
'should accept canonical $algorithm digest ($hexLength hex chars)',
361+
async ({ algorithm, hexLength }) => {
362+
const hex = 'a'.repeat(hexLength)
363+
const inputs: SubjectInputs = {
364+
...blankInputs,
365+
subjectName: 'artifact',
366+
subjectDigest: `${algorithm}:${hex}`
367+
}
368+
369+
const subjects = await subjectFromInputs(inputs)
370+
371+
expect(subjects).toHaveLength(1)
372+
expect(subjects[0].digest).toEqual({ [algorithm]: hex })
373+
}
374+
)
375+
376+
it('should accept uppercase hex digits in digest', async () => {
377+
const inputs: SubjectInputs = {
378+
...blankInputs,
379+
subjectName: 'artifact',
380+
subjectDigest: 'sha256:7D070F6B64D9BCC530FE99CC21EAAA4B3C364E0B2D367D7735671FA202A03B32'
381+
}
382+
383+
const subjects = await subjectFromInputs(inputs)
384+
385+
expect(subjects).toHaveLength(1)
386+
expect(subjects[0].digest).toEqual({
387+
sha256: '7D070F6B64D9BCC530FE99CC21EAAA4B3C364E0B2D367D7735671FA202A03B32'
388+
})
389+
})
390+
349391
it('should throw for malformed digest format', async () => {
350392
const inputs: SubjectInputs = {
351393
...blankInputs,
@@ -366,7 +408,7 @@ describe('subjectFromInputs', () => {
366408
}
367409

368410
await expect(subjectFromInputs(inputs)).rejects.toThrow(
369-
/subject-digest must be in the format/
411+
/subject-digest has unsupported algorithm "md5"/
370412
)
371413
})
372414

@@ -377,34 +419,106 @@ describe('subjectFromInputs', () => {
377419
subjectDigest: 'sha256:deadbeef'
378420
}
379421

422+
await expect(subjectFromInputs(inputs)).rejects.toThrow(
423+
/subject-digest has invalid length for algorithm "sha256"/
424+
)
425+
})
426+
427+
it('should reject uppercase algorithm names', async () => {
428+
const inputs: SubjectInputs = {
429+
...blankInputs,
430+
subjectName: 'artifact',
431+
subjectDigest: `SHA256:${'a'.repeat(64)}`
432+
}
433+
434+
await expect(subjectFromInputs(inputs)).rejects.toThrow(
435+
/subject-digest has unsupported algorithm "SHA256"/
436+
)
437+
})
438+
439+
it('should reject non-canonical algorithm aliases', async () => {
440+
const inputs: SubjectInputs = {
441+
...blankInputs,
442+
subjectName: 'artifact',
443+
subjectDigest: `sha-256:${'a'.repeat(64)}`
444+
}
445+
446+
await expect(subjectFromInputs(inputs)).rejects.toThrow(
447+
/subject-digest has unsupported algorithm "sha-256"/
448+
)
449+
})
450+
451+
it('should reject extra colons in digest', async () => {
452+
const inputs: SubjectInputs = {
453+
...blankInputs,
454+
subjectName: 'artifact',
455+
subjectDigest: `sha256:${'a'.repeat(32)}:${'b'.repeat(32)}`
456+
}
457+
380458
await expect(subjectFromInputs(inputs)).rejects.toThrow(
381459
/subject-digest must be in the format/
382460
)
383461
})
384462

385-
it('should throw for non-hex characters in digest', async () => {
463+
it('should reject empty algorithm component', async () => {
386464
const inputs: SubjectInputs = {
387465
...blankInputs,
388466
subjectName: 'artifact',
389-
subjectDigest: `sha256:${'g'.repeat(64)}`
467+
subjectDigest: `:${'a'.repeat(64)}`
390468
}
391469

392470
await expect(subjectFromInputs(inputs)).rejects.toThrow(
393471
/subject-digest must be in the format/
394472
)
395473
})
396474

397-
it('should accept valid uppercase hex digest', async () => {
475+
it('should reject empty digest component', async () => {
398476
const inputs: SubjectInputs = {
399477
...blankInputs,
400478
subjectName: 'artifact',
401-
subjectDigest: `sha256:${'A'.repeat(64)}`
479+
subjectDigest: 'sha256:'
402480
}
403481

404-
const subjects = await subjectFromInputs(inputs)
482+
await expect(subjectFromInputs(inputs)).rejects.toThrow(
483+
/subject-digest must be in the format/
484+
)
485+
})
405486

406-
expect(subjects).toHaveLength(1)
407-
expect(subjects[0].digest).toEqual({ sha256: 'A'.repeat(64) })
487+
it('should reject non-hex characters in digest', async () => {
488+
const inputs: SubjectInputs = {
489+
...blankInputs,
490+
subjectName: 'artifact',
491+
subjectDigest: `sha256:${'g'.repeat(64)}`
492+
}
493+
494+
await expect(subjectFromInputs(inputs)).rejects.toThrow(
495+
/subject-digest has invalid hex digits/
496+
)
497+
})
498+
499+
it('should reject npm SRI format', async () => {
500+
// npm SRI: "sha512-<base64>" is not the canonical "algorithm:hex" form
501+
const inputs: SubjectInputs = {
502+
...blankInputs,
503+
subjectName: 'artifact',
504+
subjectDigest: 'sha512-n4bQgYhMfWtsxiT7nrnlA0leSv4+C2CDkMXOUOEJoiQ='
505+
}
506+
507+
await expect(subjectFromInputs(inputs)).rejects.toThrow(
508+
/subject-digest must be in the format/
509+
)
510+
})
511+
512+
it('should reject digest with no colon separator', async () => {
513+
const inputs: SubjectInputs = {
514+
...blankInputs,
515+
subjectName: 'artifact',
516+
subjectDigest: `sha256${'a'.repeat(64)}`
517+
}
518+
519+
await expect(subjectFromInputs(inputs)).rejects.toThrow(
520+
/subject-digest must be in the format/
521+
)
408522
})
409523
})
410524

‎dist/index.js‎

Lines changed: 32 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/subject.ts‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ const MAX_SUBJECT_CHECKSUM_SIZE_BYTES = 512 * MAX_SUBJECT_COUNT
1515
const DIGEST_ALGORITHM = 'sha256'
1616
const HEX_STRING_RE = /^[0-9a-fA-F]+$/
1717

18+
// Canonical SHA-2 algorithms and their expected hex digest lengths
19+
const SUPPORTED_DIGEST_ALGORITHMS: Record<string, number> = {
20+
sha224: 56,
21+
sha256: 64,
22+
sha384: 96,
23+
sha512: 128,
24+
sha512_224: 56,
25+
sha512_256: 64
26+
}
27+
1828
export type SubjectInputs = {
1929
subjectPath: string
2030
subjectName: string
@@ -144,22 +154,50 @@ const getSubjectFromPath = async (
144154
return digestedSubjects
145155
}
146156

157+
// Parses a subject digest string of the form "algorithm:hex_digest" and
158+
// validates the algorithm name and hex digest length against the supported
159+
// canonical in-toto SHA-2 algorithm set.
160+
export const parseSubjectDigest = (
161+
input: string
162+
): { algorithm: string; digest: string } => {
163+
const match = input.match(/^([^:]+):([^:]+)$/)
164+
if (!match) {
165+
throw new Error(
166+
'subject-digest must be in the format "algorithm:hex_digest"'
167+
)
168+
}
169+
170+
const [, algorithm, digest] = match
171+
172+
const expectedLength = SUPPORTED_DIGEST_ALGORITHMS[algorithm]
173+
if (expectedLength === undefined) {
174+
throw new Error(`subject-digest has unsupported algorithm "${algorithm}"`)
175+
}
176+
177+
if (!HEX_STRING_RE.test(digest)) {
178+
throw new Error('subject-digest has invalid hex digits')
179+
}
180+
181+
if (digest.length !== expectedLength) {
182+
throw new Error(
183+
`subject-digest has invalid length for algorithm "${algorithm}" (expected ${expectedLength}, got ${digest.length})`
184+
)
185+
}
186+
187+
return { algorithm, digest }
188+
}
189+
147190
// Returns the subject specified by the digest of a file. The digest is returned
148191
// along with the subject's name.
149192
const getSubjectFromDigest = (
150193
subjectDigest: string,
151194
subjectName: string
152195
): Subject => {
153-
if (!subjectDigest.match(/^sha256:[0-9a-fA-F]{64}$/)) {
154-
throw new Error(
155-
'subject-digest must be in the format "sha256:<hex-digest>"'
156-
)
157-
}
158-
const [alg, digest] = subjectDigest.split(':')
196+
const { algorithm, digest } = parseSubjectDigest(subjectDigest)
159197

160198
return {
161199
name: subjectName,
162-
digest: { [alg]: digest }
200+
digest: { [algorithm]: digest }
163201
}
164202
}
165203

0 commit comments

Comments
 (0)