Skip to content

Commit

Permalink
fix: version and type are required by the message format
Browse files Browse the repository at this point in the history
resolves #209

The version and type are required values.
This error will help clarify malformed messages.
Especialy string messages that have been encoded
and the proper encoding has not been passed.
  • Loading branch information
seebees committed Sep 18, 2019
1 parent b59855e commit 8a91c04
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/serialize/src/deserialize_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export function deserializeFactory<Suite extends AlgorithmSuite> (

const version = dataView.getUint8(0)
const type = dataView.getUint8(1)
/* Precondition: version and type must be the required values. */
needs(version === 1 && type === 128, 'Malformed Header')

const suiteId = <AlgorithmSuiteIdentifier>dataView.getUint16(2, false) // big endian
/* Precondition: suiteId must match supported algorithm suite */
Expand Down
6 changes: 6 additions & 0 deletions modules/serialize/test/deserialize_factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,12 @@ 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')
})

it('Precondition: suiteId must match supported algorithm suite', () => {
const { deserializeMessageHeader } = deserializeFactory(toUtf8, WebCryptoAlgorithmSuite)
const suiteIdNotValidMessageHeader = fixtures.suiteIdNotValidMessageHeader()
Expand Down
8 changes: 8 additions & 0 deletions modules/serialize/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ 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 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 ])
}
Expand Down

0 comments on commit 8a91c04

Please sign in to comment.