@@ -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- / s u b j e c t - d i g e s t m u s t b e i n t h e f o r m a t /
411+ / s u b j e c t - d i g e s t h a s u n s u p p o r t e d a l g o r i t h m " m d 5 " /
370412 )
371413 } )
372414
@@ -377,34 +419,106 @@ describe('subjectFromInputs', () => {
377419 subjectDigest : 'sha256:deadbeef'
378420 }
379421
422+ await expect ( subjectFromInputs ( inputs ) ) . rejects . toThrow (
423+ / s u b j e c t - d i g e s t h a s i n v a l i d l e n g t h f o r a l g o r i t h m " s h a 2 5 6 " /
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+ / s u b j e c t - d i g e s t h a s u n s u p p o r t e d a l g o r i t h m " S H A 2 5 6 " /
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+ / s u b j e c t - d i g e s t h a s u n s u p p o r t e d a l g o r i t h m " s h a - 2 5 6 " /
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 / s u b j e c t - d i g e s t m u s t b e i n t h e f o r m a t /
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 / s u b j e c t - d i g e s t m u s t b e i n t h e f o r m a t /
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+ / s u b j e c t - d i g e s t m u s t b e i n t h e f o r m a t /
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+ / s u b j e c t - d i g e s t h a s i n v a l i d h e x d i g i t s /
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+ / s u b j e c t - d i g e s t m u s t b e i n t h e f o r m a t /
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+ / s u b j e c t - d i g e s t m u s t b e i n t h e f o r m a t /
521+ )
408522 } )
409523 } )
410524
0 commit comments