Skip to content

Commit a6811cb

Browse files
claudiahdzisaacs
authored andcommitted
fix: throw null when sri is empty or bad
BREAKING CHANGE: adds a new error that will be thrown. Empty SRIs are no longer considered valid for checking, only when using integrityStream to calculate the SRI value. PR-URL: #12 Credit: @claudiahdz Close: #12 Reviewed-by: @isaacs
1 parent 1727a7c commit a6811cb

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

Diff for: index.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ class Integrity {
177177
return this.toString()
178178
}
179179

180+
isEmpty () {
181+
return Object.keys(this).length === 0
182+
}
183+
180184
toString (opts) {
181185
opts = ssriOpts(opts)
182186
let sep = opts.sep || ' '
@@ -240,11 +244,6 @@ class Integrity {
240244
opts = ssriOpts(opts)
241245
const pickAlgorithm = opts.pickAlgorithm
242246
const keys = Object.keys(this)
243-
if (!keys.length) {
244-
throw new Error(`No algorithms available for ${
245-
JSON.stringify(this.toString())
246-
}`)
247-
}
248247
return keys.reduce((acc, algo) => {
249248
return pickAlgorithm(acc, algo) || acc
250249
})
@@ -253,6 +252,7 @@ class Integrity {
253252

254253
module.exports.parse = parse
255254
function parse (sri, opts) {
255+
if (!sri) return null
256256
opts = ssriOpts(opts)
257257
if (typeof sri === 'string') {
258258
return _parse(sri, opts)
@@ -271,7 +271,7 @@ function _parse (integrity, opts) {
271271
if (opts.single) {
272272
return new Hash(integrity, opts)
273273
}
274-
return integrity.trim().split(/\s+/).reduce((acc, string) => {
274+
const hashes = integrity.trim().split(/\s+/).reduce((acc, string) => {
275275
const hash = new Hash(string, opts)
276276
if (hash.algorithm && hash.digest) {
277277
const algo = hash.algorithm
@@ -280,6 +280,7 @@ function _parse (integrity, opts) {
280280
}
281281
return acc
282282
}, new Integrity())
283+
return hashes.isEmpty() ? null : hashes
283284
}
284285

285286
module.exports.stringify = stringify
@@ -347,7 +348,7 @@ module.exports.checkData = checkData
347348
function checkData (data, sri, opts) {
348349
opts = ssriOpts(opts)
349350
sri = parse(sri, opts)
350-
if (!Object.keys(sri).length) {
351+
if (!sri || !Object.keys(sri).length) {
351352
if (opts.error) {
352353
throw Object.assign(
353354
new Error('No valid integrity hashes to check against'), {
@@ -386,6 +387,14 @@ module.exports.checkStream = checkStream
386387
function checkStream (stream, sri, opts) {
387388
opts = ssriOpts(opts)
388389
opts.integrity = sri
390+
sri = parse(sri, opts)
391+
if (!sri || !Object.keys(sri).length) {
392+
return Promise.reject(Object.assign(
393+
new Error('No valid integrity hashes to check against'), {
394+
code: 'EINTEGRITY'
395+
}
396+
))
397+
}
389398
const checker = integrityStream(opts)
390399
return new Promise((resolve, reject) => {
391400
stream.pipe(checker)

Diff for: test/integrity.js

-3
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ test('pickAlgorithm()', t => {
125125
'sha384',
126126
'custom pickAlgorithm function accepted'
127127
)
128-
t.throws(() => {
129-
ssri.parse('').pickAlgorithm()
130-
}, /No algorithms available/, 'SRIs without algorithms are invalid')
131128
t.done()
132129
})
133130

0 commit comments

Comments
 (0)