Skip to content

Commit fab470e

Browse files
committed
fix(pickAlgorithm): error if pickAlgorithm() is used in an empty Integrity
1 parent 99e5e1f commit fab470e

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ class Integrity {
9393
}
9494
pickAlgorithm (opts) {
9595
const pickAlgorithm = (opts && opts.pickAlgorithm) || getPrioritizedHash
96-
return Object.keys(this).reduce((acc, algo) => {
96+
const keys = Object.keys(this)
97+
if (!keys.length) {
98+
throw new Error(`No algorithms available for ${this}`)
99+
}
100+
return keys.reduce((acc, algo) => {
97101
return pickAlgorithm(acc, algo) || acc
98102
})
99103
}

test/integrity.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ test('pickAlgorithm()', t => {
9393
'sha384',
9494
'custom pickAlgorithm function accepted'
9595
)
96+
t.throws(() => {
97+
ssri.parse('').pickAlgorithm()
98+
}, /No algorithms available/, 'SRIs without algorithms are invalid')
9699
t.done()
97100
})
98101

0 commit comments

Comments
 (0)