Skip to content

Commit de30b36

Browse files
authored
fix: version and type are required by the message format (#217)
resolves #209 The version and type are required values. This error will help clarify malformed messages. Especially string messages that have been encoded and the proper encoding has not been passed. If the blob looks like it was base64 encoded, note this in the error message.
1 parent 1788d25 commit de30b36

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

modules/serialize/src/deserialize_factory.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ export function deserializeFactory<Suite extends AlgorithmSuite> (
7676

7777
const version = dataView.getUint8(0)
7878
const type = dataView.getUint8(1)
79+
/* Precondition: version and type must be the required values. */
80+
needs(version === 1 && type === 128,
81+
version === 65 && type === 89 ? 'Malformed Header: This blob may be base64 encoded.' : 'Malformed Header.')
7982

8083
const suiteId = <AlgorithmSuiteIdentifier>dataView.getUint16(2, false) // big endian
8184
/* Precondition: suiteId must match supported algorithm suite */

modules/serialize/test/deserialize_factory.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,13 @@ describe('deserializeFactory:deserializeMessageHeader', () => {
428428
.and.to.eql(4096)
429429
})
430430

431+
it('Precondition: version and type must be the required values.', () => {
432+
const { deserializeMessageHeader } = deserializeFactory(toUtf8, WebCryptoAlgorithmSuite)
433+
expect(() => deserializeMessageHeader(fixtures.versionNotValidMessageHeader())).to.throw('Malformed Header')
434+
expect(() => deserializeMessageHeader(fixtures.typeNotValidMessageHeader())).to.throw('Malformed Header')
435+
expect(() => deserializeMessageHeader(fixtures.base64MessageHeader())).to.throw('Malformed Header: This blob may be base64 encoded.')
436+
})
437+
431438
it('Precondition: suiteId must match supported algorithm suite', () => {
432439
const { deserializeMessageHeader } = deserializeFactory(toUtf8, WebCryptoAlgorithmSuite)
433440
const suiteIdNotValidMessageHeader = fixtures.suiteIdNotValidMessageHeader()

modules/serialize/test/fixtures.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ export function suiteIdNotValidMessageHeader () {
2929
return new Uint8Array([ 1, 128, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 43, 0, 2, 0, 11, 105, 110, 102, 111, 114, 109, 97, 116, 105, 111, 110, 0, 12, 194, 189, 32, 43, 32, 194, 188, 32, 61, 32, 194, 190, 0, 4, 115, 111, 109, 101, 0, 6, 112, 117, 98, 108, 105, 99, 0, 2, 0, 12, 194, 189, 32, 43, 32, 194, 188, 32, 61, 32, 194, 190, 0, 8, 102, 105, 114, 115, 116, 75, 101, 121, 0, 5, 1, 2, 3, 4, 5, 0, 12, 194, 189, 32, 43, 32, 194, 188, 32, 61, 32, 194, 190, 0, 9, 115, 101, 99, 111, 110, 100, 75, 101, 121, 0, 5, 6, 7, 8, 9, 0, 2, 0, 0, 0, 0, 12, 0, 0, 16, 0 ])
3030
}
3131

32+
export function versionNotValidMessageHeader () {
33+
return new Uint8Array([ 256, 128, 0, 20, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 43, 0, 2, 0, 11, 105, 110, 102, 111, 114, 109, 97, 116, 105, 111, 110, 0, 12, 194, 189, 32, 43, 32, 194, 188, 32, 61, 32, 194, 190, 0, 4, 115, 111, 109, 101, 0, 6, 112, 117, 98, 108, 105, 99, 0, 2, 0, 12, 194, 189, 32, 43, 32, 194, 188, 32, 61, 32, 194, 190, 0, 8, 102, 105, 114, 115, 116, 75, 101, 121, 0, 5, 1, 2, 3, 4, 5, 0, 12, 194, 189, 32, 43, 32, 194, 188, 32, 61, 32, 194, 190, 0, 9, 115, 101, 99, 111, 110, 100, 75, 101, 121, 0, 5, 6, 7, 8, 9, 0, 2, 0, 0, 0, 0, 12, 0, 0, 16, 0 ])
34+
}
35+
36+
export function typeNotValidMessageHeader () {
37+
return new Uint8Array([ 1, 256, 0, 20, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 43, 0, 2, 0, 11, 105, 110, 102, 111, 114, 109, 97, 116, 105, 111, 110, 0, 12, 194, 189, 32, 43, 32, 194, 188, 32, 61, 32, 194, 190, 0, 4, 115, 111, 109, 101, 0, 6, 112, 117, 98, 108, 105, 99, 0, 2, 0, 12, 194, 189, 32, 43, 32, 194, 188, 32, 61, 32, 194, 190, 0, 8, 102, 105, 114, 115, 116, 75, 101, 121, 0, 5, 1, 2, 3, 4, 5, 0, 12, 194, 189, 32, 43, 32, 194, 188, 32, 61, 32, 194, 190, 0, 9, 115, 101, 99, 111, 110, 100, 75, 101, 121, 0, 5, 6, 7, 8, 9, 0, 2, 0, 0, 0, 0, 12, 0, 0, 16, 0 ])
38+
}
39+
40+
export function base64MessageHeader () {
41+
return new Uint8Array([65, 89, 65, 65, 70, 65, 77, 68, 65, 119, 77, 68, 65, 119, 77, 68, 65, 119, 77, 68, 65, 119, 77, 68, 65, 119, 77, 65, 75, 119, 65, 67, 65, 65, 116, 112, 98, 109, 90, 118, 99, 109, 49, 104, 100, 71, 108, 118, 98, 103, 65, 77, 119, 114, 48, 103, 75, 121, 68, 67, 118, 67, 65, 57, 73, 77, 75, 43, 65, 65, 82, 122, 98, 50, 49, 108, 65, 65, 90, 119, 100, 87, 74, 115, 97, 87, 77, 65, 65, 103, 65, 77, 119, 114, 48, 103, 75, 121, 68, 67, 118, 67, 65, 57, 73, 77, 75, 43, 65, 65, 104, 109, 97, 88, 74, 122, 100, 69, 116, 108, 101, 81, 65, 70, 65, 81, 73, 68, 66, 65, 85, 65, 68, 77, 75, 57, 73, 67, 115, 103, 119, 114, 119, 103, 80, 83, 68, 67, 118, 103, 65, 74, 99, 50, 86, 106, 98, 50, 53, 107, 83, 50, 86, 53, 65, 65, 85, 71, 66, 119, 103, 74, 65, 65, 73, 65, 65, 65, 65, 65, 68, 65, 65, 65, 69, 65, 65, 61])
42+
}
43+
3244
export function reservedBytesNoZeroMessageHeader () {
3345
return new Uint8Array([ 1, 128, 0, 20, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 43, 0, 2, 0, 11, 105, 110, 102, 111, 114, 109, 97, 116, 105, 111, 110, 0, 12, 194, 189, 32, 43, 32, 194, 188, 32, 61, 32, 194, 190, 0, 4, 115, 111, 109, 101, 0, 6, 112, 117, 98, 108, 105, 99, 0, 2, 0, 12, 194, 189, 32, 43, 32, 194, 188, 32, 61, 32, 194, 190, 0, 8, 102, 105, 114, 115, 116, 75, 101, 121, 0, 5, 1, 2, 3, 4, 5, 0, 12, 194, 189, 32, 43, 32, 194, 188, 32, 61, 32, 194, 190, 0, 9, 115, 101, 99, 111, 110, 100, 75, 101, 121, 0, 5, 6, 7, 8, 9, 0, 2, 0, 1, 0, 0, 12, 0, 0, 16, 0 ])
3446
}

0 commit comments

Comments
 (0)