Skip to content

Commit 6945eef

Browse files
PetarMaxseebees
authored andcommitted
fix: prototype inheritance bug in decodeEncryptionContext (aws#216)
If the serialised encryption context contains a key-value pair where the key coincides with a property of Object.prototype, decodeEncryptionContext will incorrectly throw 'Error: Duplicate encryption context key value'. The fix is to create the encryptionContext using Object.create(null) instead of the standard object initialiser. This commit also adds a test vector in fixtures.ts, containing ‘hasOwnProperty’ as its only key. Nota bene: This fix disables the standard calling of Object.prototype functions on encryption contexts returned by decodeEncryptionContext. In particular, it is no longer possible to call encryptionContext.hasOwnProperty(prop). However, due to the fix, one can use the simpler encryptionContext[prop] instead.
1 parent b59855e commit 6945eef

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

modules/serialize/src/deserialize_factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export function deserializeFactory<Suite extends AlgorithmSuite> (
197197
* @param encodedEncryptionContext Uint8Array
198198
*/
199199
function decodeEncryptionContext (encodedEncryptionContext: Uint8Array) {
200-
const encryptionContext: EncryptionContext = {}
200+
const encryptionContext: EncryptionContext = Object.create(null)
201201
/* Check for early return (Postcondition): The case of 0 length is defined as an empty object. */
202202
if (!encodedEncryptionContext.byteLength) {
203203
return encryptionContext

modules/serialize/test/deserialize_factory.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ describe('deserializeFactory:decodeEncryptionContext', () => {
7878
expect(test).to.have.property('information')
7979
.and.to.eql('\u00bd + \u00bc = \u00be')
8080
})
81+
82+
it('Keys may be properties of Object.prototype, decodeEncryptionContext has to succeed', () => {
83+
const { decodeEncryptionContext } = deserializeFactory(toUtf8, WebCryptoAlgorithmSuite)
84+
85+
/* hasOwnProperty test vector */
86+
const encryptionContext = fixtures.hasOwnPropertyEncryptionContext().slice(2)
87+
88+
const test = decodeEncryptionContext(encryptionContext)
89+
expect(test).to.have.property('hasOwnProperty')
90+
.and.to.eql('arbitraryValue')
91+
})
8192
})
8293

8394
describe('deserializeFactory:deserializeEncryptedDataKeys', () => {

modules/serialize/test/fixtures.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ export function duplicateKeysEncryptionContext () {
7777
return new Uint8Array([ 0, 43, 0, 4, 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, 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, 4, 115, 111, 109, 101, 0, 6, 112, 117, 98, 108, 105, 99 ])
7878
}
7979

80+
export function hasOwnPropertyEncryptionContext () {
81+
return new Uint8Array([ 0, 34, 0, 1, 0, 14, 104, 97, 115, 79, 119, 110, 80, 114, 111, 112, 101, 114, 116, 121, 0, 14, 97, 114, 98, 105, 116, 114, 97, 114, 121, 86, 97, 108, 117, 101 ])
82+
}
83+
8084
export function basicFrameIV () {
8185
return new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
8286
}

0 commit comments

Comments
 (0)