Skip to content

Commit cf86553

Browse files
committed
fix(opts): use figgy-pudding to specify consumed opts
1 parent 97b032d commit cf86553

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

index.js

+38-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const crypto = require('crypto')
4+
const figgyPudding = require('figgy-pudding')
45
const Transform = require('stream').Transform
56

67
const SPEC_ALGORITHMS = ['sha256', 'sha384', 'sha512']
@@ -10,10 +11,24 @@ const SRI_REGEX = /^([^-]+)-([^?]+)([?\S*]*)$/
1011
const STRICT_SRI_REGEX = /^([^-]+)-([A-Za-z0-9+/=]{44,88})(\?[\x21-\x7E]*)*$/
1112
const VCHAR_REGEX = /^[\x21-\x7E]+$/
1213

14+
const SsriOpts = figgyPudding({
15+
algorithms: {default: ['sha512']},
16+
error: {default: false},
17+
integrity: {},
18+
options: {default: []},
19+
pickAlgorithm: {default: () => getPrioritizedHash},
20+
Promise: {default: () => Promise},
21+
sep: {default: ' '},
22+
single: {default: false},
23+
size: {},
24+
strict: {default: false}
25+
})
26+
1327
class Hash {
1428
get isHash () { return true }
1529
constructor (hash, opts) {
16-
const strict = !!(opts && opts.strict)
30+
opts = SsriOpts(opts)
31+
const strict = !!opts.strict
1732
this.source = hash.trim()
1833
// 3.1. Integrity metadata (called "Hash" by ssri)
1934
// https://w3c.github.io/webappsec-subresource-integrity/#integrity-metadata-description
@@ -37,7 +52,8 @@ class Hash {
3752
return this.toString()
3853
}
3954
toString (opts) {
40-
if (opts && opts.strict) {
55+
opts = SsriOpts(opts)
56+
if (opts.strict) {
4157
// Strict mode enforces the standard as close to the foot of the
4258
// letter as it can.
4359
if (!(
@@ -70,7 +86,7 @@ class Integrity {
7086
return this.toString()
7187
}
7288
toString (opts) {
73-
opts = opts || {}
89+
opts = SsriOpts(opts)
7490
let sep = opts.sep || ' '
7591
if (opts.strict) {
7692
// Entries must be separated by whitespace, according to spec.
@@ -83,6 +99,7 @@ class Integrity {
8399
}).filter(x => x.length).join(sep)
84100
}
85101
concat (integrity, opts) {
102+
opts = SsriOpts(opts)
86103
const other = typeof integrity === 'string'
87104
? integrity
88105
: stringify(integrity, opts)
@@ -92,6 +109,7 @@ class Integrity {
92109
return parse(this, {single: true}).hexDigest()
93110
}
94111
match (integrity, opts) {
112+
opts = SsriOpts(opts)
95113
const other = parse(integrity, opts)
96114
const algo = other.pickAlgorithm(opts)
97115
return (
@@ -105,7 +123,8 @@ class Integrity {
105123
) || false
106124
}
107125
pickAlgorithm (opts) {
108-
const pickAlgorithm = (opts && opts.pickAlgorithm) || getPrioritizedHash
126+
opts = SsriOpts(opts)
127+
const pickAlgorithm = opts.pickAlgorithm
109128
const keys = Object.keys(this)
110129
if (!keys.length) {
111130
throw new Error(`No algorithms available for ${
@@ -120,7 +139,7 @@ class Integrity {
120139

121140
module.exports.parse = parse
122141
function parse (sri, opts) {
123-
opts = opts || {}
142+
opts = SsriOpts(opts)
124143
if (typeof sri === 'string') {
125144
return _parse(sri, opts)
126145
} else if (sri.algorithm && sri.digest) {
@@ -151,6 +170,7 @@ function _parse (integrity, opts) {
151170

152171
module.exports.stringify = stringify
153172
function stringify (obj, opts) {
173+
opts = SsriOpts(opts)
154174
if (obj.algorithm && obj.digest) {
155175
return Hash.prototype.toString.call(obj, opts)
156176
} else if (typeof obj === 'string') {
@@ -162,7 +182,8 @@ function stringify (obj, opts) {
162182

163183
module.exports.fromHex = fromHex
164184
function fromHex (hexDigest, algorithm, opts) {
165-
const optString = (opts && opts.options && opts.options.length)
185+
opts = SsriOpts(opts)
186+
const optString = opts.options && opts.options.length
166187
? `?${opts.options.join('?')}`
167188
: ''
168189
return parse(
@@ -174,8 +195,8 @@ function fromHex (hexDigest, algorithm, opts) {
174195

175196
module.exports.fromData = fromData
176197
function fromData (data, opts) {
177-
opts = opts || {}
178-
const algorithms = opts.algorithms || ['sha512']
198+
opts = SsriOpts(opts)
199+
const algorithms = opts.algorithms
179200
const optString = opts.options && opts.options.length
180201
? `?${opts.options.join('?')}`
181202
: ''
@@ -196,7 +217,7 @@ function fromData (data, opts) {
196217

197218
module.exports.fromStream = fromStream
198219
function fromStream (stream, opts) {
199-
opts = opts || {}
220+
opts = SsriOpts(opts)
200221
const P = opts.Promise || Promise
201222
const istream = integrityStream(opts)
202223
return new P((resolve, reject) => {
@@ -212,7 +233,7 @@ function fromStream (stream, opts) {
212233

213234
module.exports.checkData = checkData
214235
function checkData (data, sri, opts) {
215-
opts = opts || {}
236+
opts = SsriOpts(opts)
216237
sri = parse(sri, opts)
217238
if (!Object.keys(sri).length) {
218239
if (opts.error) {
@@ -251,9 +272,9 @@ function checkData (data, sri, opts) {
251272

252273
module.exports.checkStream = checkStream
253274
function checkStream (stream, sri, opts) {
254-
opts = opts || {}
275+
opts = SsriOpts(opts)
255276
const P = opts.Promise || Promise
256-
const checker = integrityStream(Object.assign({}, opts, {
277+
const checker = integrityStream(opts.concat({
257278
integrity: sri
258279
}))
259280
return new P((resolve, reject) => {
@@ -269,18 +290,15 @@ function checkStream (stream, sri, opts) {
269290

270291
module.exports.integrityStream = integrityStream
271292
function integrityStream (opts) {
272-
opts = opts || {}
293+
opts = SsriOpts(opts)
273294
// For verification
274295
const sri = opts.integrity && parse(opts.integrity, opts)
275296
const goodSri = sri && Object.keys(sri).length
276297
const algorithm = goodSri && sri.pickAlgorithm(opts)
277298
const digests = goodSri && sri[algorithm]
278299
// Calculating stream
279300
const algorithms = Array.from(
280-
new Set(
281-
(opts.algorithms || ['sha512'])
282-
.concat(algorithm ? [algorithm] : [])
283-
)
301+
new Set(opts.algorithms.concat(algorithm ? [algorithm] : []))
284302
)
285303
const hashes = algorithms.map(crypto.createHash)
286304
let streamSize = 0
@@ -325,9 +343,9 @@ function integrityStream (opts) {
325343

326344
module.exports.create = createIntegrity
327345
function createIntegrity (opts) {
328-
opts = opts || {}
329-
const algorithms = opts.algorithms || ['sha512']
330-
const optString = opts.options && opts.options.length
346+
opts = SsriOpts(opts)
347+
const algorithms = opts.algorithms
348+
const optString = opts.options.length
331349
? `?${opts.options.join('?')}`
332350
: ''
333351

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
"twitter": "maybekatz"
3737
},
3838
"license": "ISC",
39-
"dependencies": {},
39+
"dependencies": {
40+
"figgy-pudding": "^3.5.1"
41+
},
4042
"devDependencies": {
4143
"nyc": "^11.4.1",
4244
"standard": "^10.0.3",

0 commit comments

Comments
 (0)