diff --git a/modules/serialize/src/deserialize_factory.ts b/modules/serialize/src/deserialize_factory.ts index b4c29cf33..ae7c36ecd 100644 --- a/modules/serialize/src/deserialize_factory.ts +++ b/modules/serialize/src/deserialize_factory.ts @@ -76,6 +76,9 @@ export function deserializeFactory ( const version = dataView.getUint8(0) const type = dataView.getUint8(1) + /* Precondition: version and type must be the required values. */ + needs(version === 1 && type === 128, + version === 65 && type === 89 ? 'Malformed Header: This blob may be base64 encoded.' : 'Malformed Header.') const suiteId = dataView.getUint16(2, false) // big endian /* Precondition: suiteId must match supported algorithm suite */ diff --git a/modules/serialize/test/deserialize_factory.test.ts b/modules/serialize/test/deserialize_factory.test.ts index c0bf67e18..e53b411b8 100644 --- a/modules/serialize/test/deserialize_factory.test.ts +++ b/modules/serialize/test/deserialize_factory.test.ts @@ -428,6 +428,13 @@ describe('deserializeFactory:deserializeMessageHeader', () => { .and.to.eql(4096) }) + it('Precondition: version and type must be the required values.', () => { + const { deserializeMessageHeader } = deserializeFactory(toUtf8, WebCryptoAlgorithmSuite) + expect(() => deserializeMessageHeader(fixtures.versionNotValidMessageHeader())).to.throw('Malformed Header') + expect(() => deserializeMessageHeader(fixtures.typeNotValidMessageHeader())).to.throw('Malformed Header') + expect(() => deserializeMessageHeader(fixtures.base64MessageHeader())).to.throw('Malformed Header: This blob may be base64 encoded.') + }) + it('Precondition: suiteId must match supported algorithm suite', () => { const { deserializeMessageHeader } = deserializeFactory(toUtf8, WebCryptoAlgorithmSuite) const suiteIdNotValidMessageHeader = fixtures.suiteIdNotValidMessageHeader() diff --git a/modules/serialize/test/fixtures.ts b/modules/serialize/test/fixtures.ts index 7ce264ace..1430f5fb6 100644 --- a/modules/serialize/test/fixtures.ts +++ b/modules/serialize/test/fixtures.ts @@ -29,6 +29,18 @@ export function suiteIdNotValidMessageHeader () { 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 ]) } +export function versionNotValidMessageHeader () { + 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 ]) +} + +export function typeNotValidMessageHeader () { + 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 ]) +} + +export function base64MessageHeader () { + 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]) +} + export function reservedBytesNoZeroMessageHeader () { 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 ]) }