Skip to content

Commit

Permalink
fix: properly handle missing algorithm type (#48)
Browse files Browse the repository at this point in the history
Fixing a bug that happen in npm cli when I install a library.
in Line 272 the result of parse can be null if integrity is null. Then in line 273 other is null so it breaks as below:

```
TypeError: Cannot read properties of null (reading 'pickAlgorithm')
npm verb stack     at Integrity.match (/usr/lib/node_modules/npm/node_modules/ssri/lib/index.js:273:24)
npm verb stack     at CachePolicy.satisfies (/usr/lib/node_modules/npm/node_modules/make-fetch-happen/lib/cache/policy.js:112:49)
npm verb stack     at Function.find (/usr/lib/node_modules/npm/node_modules/make-fetch-happen/lib/cache/entry.js:178:25)
npm verb stack     at async cacheFetch (/usr/lib/node_modules/npm/node_modules/make-fetch-happen/lib/cache/index.js:8:17)
npm verb stack     at async fetch (/usr/lib/node_modules/npm/node_modules/make-fetch-happen/lib/fetch.js:98:7)
```
  • Loading branch information
ahmedwelhakim authored Oct 4, 2022
1 parent dd14735 commit 2e876d1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ class Integrity {
match (integrity, opts) {
opts = ssriOpts(opts)
const other = parse(integrity, opts)
if (!other) {
return false
}
const algo = other.pickAlgorithm(opts)
return (
this[algo] &&
Expand Down
1 change: 1 addition & 0 deletions test/integrity.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ test('match()', t => {
}, 'returns the strongest match')
t.notOk(sri.match('sha512-foo'), 'falsy when match fails')
t.notOk(sri.match('sha384-foo'), 'falsy when match fails')
t.notOk(sri.match(null), 'falsy when integrity is null')
t.end()
})

Expand Down

0 comments on commit 2e876d1

Please sign in to comment.