Skip to content

Commit 201aa5a

Browse files
committedApr 17, 2023
deps: ssri@10.0.3
1 parent acb9120 commit 201aa5a

File tree

4 files changed

+71
-23
lines changed

4 files changed

+71
-23
lines changed
 

‎node_modules/ssri/lib/index.js

+65-17
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,33 @@ class IntegrityStream extends MiniPass {
2929
this.#getOptions()
3030

3131
// options used for calculating stream. can't be changed.
32-
const algorithms = opts?.algorithms || DEFAULT_ALGORITHMS
33-
this.algorithms = Array.from(
34-
new Set(algorithms.concat(this.algorithm ? [this.algorithm] : []))
35-
)
32+
if (opts?.algorithms) {
33+
this.algorithms = [...opts.algorithms]
34+
} else {
35+
this.algorithms = [...DEFAULT_ALGORITHMS]
36+
}
37+
if (this.algorithm !== null && !this.algorithms.includes(this.algorithm)) {
38+
this.algorithms.push(this.algorithm)
39+
}
40+
3641
this.hashes = this.algorithms.map(crypto.createHash)
3742
}
3843

3944
#getOptions () {
4045
// For verification
4146
this.sri = this.opts?.integrity ? parse(this.opts?.integrity, this.opts) : null
4247
this.expectedSize = this.opts?.size
43-
this.goodSri = this.sri ? !!Object.keys(this.sri).length : false
44-
this.algorithm = this.goodSri ? this.sri.pickAlgorithm(this.opts) : null
48+
49+
if (!this.sri) {
50+
this.algorithm = null
51+
} else if (this.sri.isHash) {
52+
this.goodSri = true
53+
this.algorithm = this.sri.algorithm
54+
} else {
55+
this.goodSri = !this.sri.isEmpty()
56+
this.algorithm = this.sri.pickAlgorithm(this.opts)
57+
}
58+
4559
this.digests = this.goodSri ? this.sri[this.algorithm] : null
4660
this.optString = getOptString(this.opts?.options)
4761
}
@@ -159,6 +173,29 @@ class Hash {
159173
return this.toString()
160174
}
161175

176+
match (integrity, opts) {
177+
const other = parse(integrity, opts)
178+
if (!other) {
179+
return false
180+
}
181+
if (other.isIntegrity) {
182+
const algo = other.pickAlgorithm(opts, [this.algorithm])
183+
184+
if (!algo) {
185+
return false
186+
}
187+
188+
const foundHash = other[algo].find(hash => hash.digest === this.digest)
189+
190+
if (foundHash) {
191+
return foundHash
192+
}
193+
194+
return false
195+
}
196+
return other.digest === this.digest ? other : false
197+
}
198+
162199
toString (opts) {
163200
if (opts?.strict) {
164201
// Strict mode enforces the standard as close to the foot of the
@@ -285,8 +322,9 @@ class Integrity {
285322
if (!other) {
286323
return false
287324
}
288-
const algo = other.pickAlgorithm(opts)
325+
const algo = other.pickAlgorithm(opts, Object.keys(this))
289326
return (
327+
!!algo &&
290328
this[algo] &&
291329
other[algo] &&
292330
this[algo].find(hash =>
@@ -297,12 +335,22 @@ class Integrity {
297335
) || false
298336
}
299337

300-
pickAlgorithm (opts) {
338+
// Pick the highest priority algorithm present, optionally also limited to a
339+
// set of hashes found in another integrity. When limiting it may return
340+
// nothing.
341+
pickAlgorithm (opts, hashes) {
301342
const pickAlgorithm = opts?.pickAlgorithm || getPrioritizedHash
302-
const keys = Object.keys(this)
303-
return keys.reduce((acc, algo) => {
304-
return pickAlgorithm(acc, algo) || acc
343+
const keys = Object.keys(this).filter(k => {
344+
if (hashes?.length) {
345+
return hashes.includes(k)
346+
}
347+
return true
305348
})
349+
if (keys.length) {
350+
return keys.reduce((acc, algo) => pickAlgorithm(acc, algo) || acc)
351+
}
352+
// no intersection between this and hashes,
353+
return null
306354
}
307355
}
308356

@@ -365,7 +413,7 @@ function fromHex (hexDigest, algorithm, opts) {
365413

366414
module.exports.fromData = fromData
367415
function fromData (data, opts) {
368-
const algorithms = opts?.algorithms || DEFAULT_ALGORITHMS
416+
const algorithms = opts?.algorithms || [...DEFAULT_ALGORITHMS]
369417
const optString = getOptString(opts?.options)
370418
return algorithms.reduce((acc, algo) => {
371419
const digest = crypto.createHash(algo).update(data).digest('base64')
@@ -399,7 +447,7 @@ function fromStream (stream, opts) {
399447
sri = s
400448
})
401449
istream.on('end', () => resolve(sri))
402-
istream.on('data', () => {})
450+
istream.resume()
403451
})
404452
}
405453

@@ -466,7 +514,7 @@ function checkStream (stream, sri, opts) {
466514
verified = s
467515
})
468516
checker.on('end', () => resolve(verified))
469-
checker.on('data', () => {})
517+
checker.resume()
470518
})
471519
}
472520

@@ -477,7 +525,7 @@ function integrityStream (opts = Object.create(null)) {
477525

478526
module.exports.create = createIntegrity
479527
function createIntegrity (opts) {
480-
const algorithms = opts?.algorithms || DEFAULT_ALGORITHMS
528+
const algorithms = opts?.algorithms || [...DEFAULT_ALGORITHMS]
481529
const optString = getOptString(opts?.options)
482530

483531
const hashes = algorithms.map(crypto.createHash)
@@ -512,7 +560,7 @@ function createIntegrity (opts) {
512560
}
513561
}
514562

515-
const NODE_HASHES = new Set(crypto.getHashes())
563+
const NODE_HASHES = crypto.getHashes()
516564

517565
// This is a Best Effort™ at a reasonable priority for hash algos
518566
const DEFAULT_PRIORITY = [
@@ -522,7 +570,7 @@ const DEFAULT_PRIORITY = [
522570
'sha3',
523571
'sha3-256', 'sha3-384', 'sha3-512',
524572
'sha3_256', 'sha3_384', 'sha3_512',
525-
].filter(algo => NODE_HASHES.has(algo))
573+
].filter(algo => NODE_HASHES.includes(algo))
526574

527575
function getPrioritizedHash (algo1, algo2) {
528576
/* eslint-disable-next-line max-len */

‎node_modules/ssri/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ssri",
3-
"version": "10.0.2",
3+
"version": "10.0.3",
44
"description": "Standard Subresource Integrity library -- parses, serializes, generates, and verifies integrity metadata according to the SRI spec.",
55
"main": "lib/index.js",
66
"files": [

‎package-lock.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
"read-package-json": "^6.0.1",
142142
"read-package-json-fast": "^3.0.2",
143143
"semver": "^7.3.8",
144-
"ssri": "^10.0.2",
144+
"ssri": "^10.0.3",
145145
"tar": "^6.1.13",
146146
"text-table": "~0.2.0",
147147
"tiny-relative-date": "^1.3.0",
@@ -11440,9 +11440,9 @@
1144011440
"dev": true
1144111441
},
1144211442
"node_modules/ssri": {
11443-
"version": "10.0.2",
11444-
"resolved": "https://registry.npmjs.org/ssri/-/ssri-10.0.2.tgz",
11445-
"integrity": "sha512-LWMXUSh7fEfCXNBq4UnRzC4Qc5Y1PPg5ogmb+6HX837i2cKzjB133aYmQ4lgO0shVTcTQHquKp3v5bn898q3Sw==",
11443+
"version": "10.0.3",
11444+
"resolved": "https://registry.npmjs.org/ssri/-/ssri-10.0.3.tgz",
11445+
"integrity": "sha512-lJtX/BFPI/VEtxZmLfeh7pzisIs6micwZ3eruD3+ds9aPsXKlYpwDS2Q7omD6WC42WO9+bnUSzlMmfv8uK8meg==",
1144611446
"inBundle": true,
1144711447
"dependencies": {
1144811448
"minipass": "^4.0.0"

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"read-package-json": "^6.0.1",
111111
"read-package-json-fast": "^3.0.2",
112112
"semver": "^7.3.8",
113-
"ssri": "^10.0.2",
113+
"ssri": "^10.0.3",
114114
"tar": "^6.1.13",
115115
"text-table": "~0.2.0",
116116
"tiny-relative-date": "^1.3.0",

0 commit comments

Comments
 (0)
Please sign in to comment.