File tree 3 files changed +20
-1
lines changed
3 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -92,6 +92,10 @@ will be an `Integrity` instance that has this shape:
92
92
}
93
93
```
94
94
95
+ If ` opts.single ` is truthy, a single ` IntegrityMetadata ` object will be
96
+ returned. That is, a single object that looks like `{algorithm, digest,
97
+ options}`, as opposed to a larger object with multiple of these.
98
+
95
99
If ` opts.strict ` is truthy, the resulting object will be filtered such that
96
100
it strictly follows the Subresource Integrity spec, throwing away any entries
97
101
with any invalid components. This also means a restricted set of algorithms
Original file line number Diff line number Diff line change @@ -102,6 +102,9 @@ function parse (sri, opts) {
102
102
function _parse ( integrity , opts ) {
103
103
// 3.4.3. Parse metadata
104
104
// https://w3c.github.io/webappsec-subresource-integrity/#parse-metadata
105
+ if ( opts . single ) {
106
+ return new IntegrityMetadata ( integrity , opts )
107
+ }
105
108
return integrity . trim ( ) . split ( / \s + / ) . reduce ( ( acc , string ) => {
106
109
const metadata = new IntegrityMetadata ( string , opts )
107
110
if ( metadata . algorithm && metadata . digest ) {
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ function hash (data, algorithm) {
12
12
return crypto . createHash ( algorithm ) . update ( data ) . digest ( 'base64' )
13
13
}
14
14
15
- test ( 'parses single-entry inegrity string' , t => {
15
+ test ( 'parses single-entry integrity string' , t => {
16
16
const sha = hash ( TEST_DATA , 'sha512' )
17
17
const integrity = `sha512-${ sha } `
18
18
t . deepEqual ( ssri . parse ( integrity ) , {
@@ -26,6 +26,18 @@ test('parses single-entry inegrity string', t => {
26
26
t . done ( )
27
27
} )
28
28
29
+ test ( 'can parse single-entry string directly into IntegrityMetadata' , t => {
30
+ const sha = hash ( TEST_DATA , 'sha512' )
31
+ const integrity = `sha512-${ sha } `
32
+ t . deepEqual ( ssri . parse ( integrity , { single : true } ) , {
33
+ source : integrity ,
34
+ digest : sha ,
35
+ algorithm : 'sha512' ,
36
+ options : [ ]
37
+ } , 'single entry parsed into single IntegrityMetadata instance' )
38
+ t . done ( )
39
+ } )
40
+
29
41
test ( 'accepts IntegrityMetadata-likes as input' , t => {
30
42
const algorithm = 'sha512'
31
43
const digest = hash ( TEST_DATA , 'sha512' )
You can’t perform that action at this time.
0 commit comments