Skip to content

Commit 76e2233

Browse files
committed
fix: simplify regex for strict mode, add tests
1 parent 41b764f commit 76e2233

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const SPEC_ALGORITHMS = ['sha256', 'sha384', 'sha512']
99
// rather than [a-z0-9].
1010
const BASE64_REGEX = /^[a-z0-9+/]+(?:=?=?)$/i
1111
const SRI_REGEX = /^([a-z0-9]+)-([^?]+)([?\S*]*)$/
12-
const STRICT_SRI_REGEX = /^([a-z0-9]+)-([A-Za-z0-9+/=]{44,88})(\?[\x21-\x7E]*)*$/
12+
const STRICT_SRI_REGEX = /^([a-z0-9]+)-([A-Za-z0-9+/=]{44,88})(\?[\x21-\x7E]*)?$/
1313
const VCHAR_REGEX = /^[\x21-\x7E]+$/
1414

1515
const defaultOpts = {
@@ -24,7 +24,8 @@ const defaultOpts = {
2424

2525
const ssriOpts = (opts = {}) => ({ ...defaultOpts, ...opts })
2626

27-
const getOptString = options => !options || !options.length ? ''
27+
const getOptString = options => !options || !options.length
28+
? ''
2829
: `?${options.join('?')}`
2930

3031
const _onEnd = Symbol('_onEnd')

test/parse.js

+28
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,34 @@ test('parses single-entry integrity string', t => {
2626
t.done()
2727
})
2828

29+
test('parses options from integrity string', t => {
30+
const sha = hash(TEST_DATA, 'sha512')
31+
const integrity = `sha512-${sha}?one?two?three`
32+
t.deepEqual(ssri.parse(integrity), {
33+
sha512: [{
34+
source: integrity,
35+
digest: sha,
36+
algorithm: 'sha512',
37+
options: ['one', 'two', 'three']
38+
}]
39+
}, 'single entry parsed into full Integrity instance')
40+
t.done()
41+
})
42+
43+
test('parses options from integrity string in strict mode', t => {
44+
const sha = hash(TEST_DATA, 'sha512')
45+
const integrity = `sha512-${sha}?one?two?three`
46+
t.deepEqual(ssri.parse(integrity, { strict: true }), {
47+
sha512: [{
48+
source: integrity,
49+
digest: sha,
50+
algorithm: 'sha512',
51+
options: ['one', 'two', 'three']
52+
}]
53+
}, 'single entry parsed into full Integrity instance')
54+
t.done()
55+
})
56+
2957
test('can parse single-entry string directly into Hash', t => {
3058
const sha = hash(TEST_DATA, 'sha512')
3159
const integrity = `sha512-${sha}`

0 commit comments

Comments
 (0)