-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Do not blow up if the opts object is mutated
Pacote has a use case where the integrity value may not be known at the outset, but is later established, either via the dist.integrity in a packument, or by the x-local-hash header value when make-fetch-happen loads a response from the cache. In these cases, we have already started an integrity stream at the beginning of the request, and don't get the expected integrity until _after_ the integrity stream is created, resulting in a spurious EINTEGRITY error. This patch makes ssri responsive to (and resilient against) updates to the integrity and size options after the stream has started.
- Loading branch information
Showing
2 changed files
with
94 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const ssri = require('../') | ||
const t = require('tap') | ||
|
||
const data = 'hello world' | ||
const expectIntegrity = ssri.fromData(data, { algorithms: ['sha512'] }) | ||
const expectSize = data.length | ||
|
||
t.test('support adding bad integrity later', t => { | ||
const opts = {} | ||
const stream = ssri.integrityStream(opts) | ||
opts.integrity = ssri.parse('sha512-deepbeets') | ||
return t.rejects(stream.end(data).collect(), { | ||
code: 'EINTEGRITY' | ||
}) | ||
}) | ||
|
||
t.test('support adding bad integrity string later', t => { | ||
const opts = {} | ||
const stream = ssri.integrityStream(opts) | ||
opts.integrity = 'sha512-deepbeets' | ||
return t.rejects(stream.end(data).collect(), { | ||
code: 'EINTEGRITY' | ||
}) | ||
}) | ||
|
||
t.test('support adding bad size later', t => { | ||
const opts = {} | ||
const stream = ssri.integrityStream(opts) | ||
opts.size = 2 | ||
return t.rejects(stream.end(data).collect(), { | ||
code: 'EBADSIZE' | ||
}) | ||
}) | ||
|
||
t.test('support adding good integrity later', t => { | ||
const opts = {} | ||
const stream = ssri.integrityStream(opts) | ||
opts.integrity = expectIntegrity | ||
return stream.end(data).on('verified', match => { | ||
t.same(match, expectIntegrity.sha512[0]) | ||
}).collect() | ||
}) | ||
|
||
t.test('support adding good integrity string later', t => { | ||
const opts = {} | ||
const stream = ssri.integrityStream(opts) | ||
opts.integrity = String(expectIntegrity) | ||
return stream.end(data).on('verified', match => { | ||
t.same(match, expectIntegrity.sha512[0]) | ||
}).collect() | ||
}) | ||
|
||
t.test('support adding good size later', t => { | ||
const opts = {} | ||
const stream = ssri.integrityStream(opts) | ||
opts.size = expectSize | ||
return stream.end(data).on('size', size => { | ||
t.same(size, expectSize) | ||
}).collect() | ||
}) |