Skip to content

Commit 08a2356

Browse files
committed
updates
Pass the error through the node stream better. add test for browsers.
1 parent 6f94c29 commit 08a2356

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

modules/decrypt-browser/test/decrypt.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,15 @@ describe('decrypt', () => {
3232
expect(messageHeader.encryptionContext).to.deep.equal(fixtures.encryptionContext())
3333
expect(test).to.deep.equal(fixtures.plaintext())
3434
})
35+
36+
it('Precondition: The sequence number is required to monotonically increase, starting from 1.', async () => {
37+
return decrypt(
38+
fixtures.decryptKeyring(),
39+
fixtures.frameSequenceOutOfOrder()
40+
).then(() => {
41+
throw new Error('should not succeed')
42+
}, err => {
43+
expect(err).to.be.instanceOf(Error)
44+
})
45+
})
3546
})

modules/decrypt-browser/test/fixtures.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ export function frameSequenceOutOfOrder () {
4343
* The data key is all zeros.
4444
* The frames have been reordered in order to test decryption failure.
4545
*/
46-
return [
46+
return new Uint8Array([
4747
1, 128, 0, 20, 111, 198, 208, 129, 64, 41, 115, 91, 247, 27, 148, 148, 102, 70, 149, 210, 0, 0, 0, 1, 0, 1, 107, 0, 1, 107, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 12, 0, 0, 0, 1, // header
4848
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 178, 85, 20, 93, 96, 34, 206, 116, 216, 115, 183, 217, 192, 84, 155, 15, // header auth
4949
0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71, 217, 75, 144, 128, 252, 7, 157, 174, 2, 89, 248, 206, 24, 122, 69, 226, 95, // f
5050
0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 114, 251, 4, 157, 182, 94, 8, 230, 234, 185, 222, 120, 209, 235, 186, 207, 112, // d
5151
0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 44, 171, 202, 172, 191, 0, 232, 145, 31, 77, 90, 8, 233, 45, 106, 60, 176, // s
52-
0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 248, 112, 28, 63, 229, 61, 238, 146, 255, 228, 38, 170, 100, 42, 251, 21, 208] // a
52+
0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 248, 112, 28, 63, 229, 61, 238, 146, 255, 228, 38, 170, 100, 42, 251, 21, 208 // a
53+
])
5354
}
5455

5556
class TestKeyring extends KeyringWebCrypto {

modules/decrypt-node/src/decrypt_stream.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Duplexify from 'duplexify'
2525
import { Duplex } from 'stream' // eslint-disable-line no-unused-vars
2626

2727
// @ts-ignore
28-
import { pipeline } from 'readable-stream'
28+
import { pipeline, PassThrough } from 'readable-stream'
2929

3030
export interface DecryptStreamOptions {
3131
maxBodySize?: number
@@ -44,7 +44,13 @@ export function decryptStream (
4444
const verifyStream = new VerifyStream({ maxBodySize })
4545
const decipherStream = getDecipherStream()
4646

47-
pipeline(parseHeaderStream, verifyStream, decipherStream)
47+
/* pipeline will _either_ stream.destroy or the callback.
48+
* decipherStream uses destroy to dispose the material.
49+
* So I tack a pass though stream onto the end.
50+
*/
51+
pipeline(parseHeaderStream, verifyStream, decipherStream, new PassThrough(), (err: Error) => {
52+
if (err) stream.emit('error', err)
53+
})
4854

4955
const stream = new Duplexify(parseHeaderStream, decipherStream)
5056

modules/decrypt-node/src/parse_header_stream.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,17 @@ export class ParseHeaderStream extends PortableTransformWithType {
108108
// The header is parsed, pass control
109109
const readPos = rawHeader.byteLength + headerIv.byteLength + headerAuthTag.byteLength
110110
const tail = headerBuffer.slice(readPos)
111-
this._transform = (chunk: any, _enc: string, cb: Function) => cb(null, chunk)
111+
/* needs calls in downstream _transform streams will throw.
112+
* But streams are async.
113+
* So this error should be turned into an `.emit('error', ex)`.
114+
*/
115+
this._transform = (chunk: any, _enc: string, cb: Function) => {
116+
try {
117+
cb(null, chunk)
118+
} catch (ex) {
119+
this.emit('error', ex)
120+
}
121+
}
112122
// flush the tail. Stream control is now in the verify and decrypt streams
113123
return setImmediate(() => this._transform(tail, encoding, callback))
114124
})

modules/decrypt-node/test/decrypt.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ describe('decrypt', () => {
6969
})
7070

7171
it('Precondition: The sequence number is required to monotonically increase, starting from 1.', async () => {
72-
expect(decrypt(
72+
return expect(decrypt(
7373
fixtures.decryptKeyring(),
7474
fixtures.frameSequenceOutOfOrder(),
7575
{ encoding: 'base64' }
76-
)).to.rejectedWith(Error)
76+
)).to.rejectedWith(Error, 'Encrypted body sequence out of order.')
7777
})
7878
})

0 commit comments

Comments
 (0)