Skip to content

Commit

Permalink
feat(parse): allow parsing straight into a single IntegrityMetadata o…
Browse files Browse the repository at this point in the history
…bject
  • Loading branch information
zkat committed Apr 2, 2017
1 parent b97a796 commit c8ddf48
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ will be an `Integrity` instance that has this shape:
}
```

If `opts.single` is truthy, a single `IntegrityMetadata` object will be
returned. That is, a single object that looks like `{algorithm, digest,
options}`, as opposed to a larger object with multiple of these.

If `opts.strict` is truthy, the resulting object will be filtered such that
it strictly follows the Subresource Integrity spec, throwing away any entries
with any invalid components. This also means a restricted set of algorithms
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ function parse (sri, opts) {
function _parse (integrity, opts) {
// 3.4.3. Parse metadata
// https://w3c.github.io/webappsec-subresource-integrity/#parse-metadata
if (opts.single) {
return new IntegrityMetadata(integrity, opts)
}
return integrity.trim().split(/\s+/).reduce((acc, string) => {
const metadata = new IntegrityMetadata(string, opts)
if (metadata.algorithm && metadata.digest) {
Expand Down
14 changes: 13 additions & 1 deletion test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function hash (data, algorithm) {
return crypto.createHash(algorithm).update(data).digest('base64')
}

test('parses single-entry inegrity string', t => {
test('parses single-entry integrity string', t => {
const sha = hash(TEST_DATA, 'sha512')
const integrity = `sha512-${sha}`
t.deepEqual(ssri.parse(integrity), {
Expand All @@ -26,6 +26,18 @@ test('parses single-entry inegrity string', t => {
t.done()
})

test('can parse single-entry string directly into IntegrityMetadata', t => {
const sha = hash(TEST_DATA, 'sha512')
const integrity = `sha512-${sha}`
t.deepEqual(ssri.parse(integrity, {single: true}), {
source: integrity,
digest: sha,
algorithm: 'sha512',
options: []
}, 'single entry parsed into single IntegrityMetadata instance')
t.done()
})

test('accepts IntegrityMetadata-likes as input', t => {
const algorithm = 'sha512'
const digest = hash(TEST_DATA, 'sha512')
Expand Down

0 comments on commit c8ddf48

Please sign in to comment.