Skip to content

fix: version and type are required by the message format #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions modules/serialize/src/deserialize_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ 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,
version === 65 && type === 89 ? 'Malformed Header: This blob may be base64 encoded.' : 'Malformed Header.')

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