Skip to content

Commit 1532b9e

Browse files
authored
fix: KMS client plaintext byteOffset (generate too) (#47)
The KMS Client *may* return a Buffer that is not isolated. i.e. the byteOffset !== 0. This means that the unencrypted data key is possibly accessible to someone else. If this is the node shared Buffer, then other code within this process _could_ find this secret. Copy Plaintext to an isolated ArrayBuffer and zero the Plaintext.
1 parent 8c75cfb commit 1532b9e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

modules/kms-keyring/src/helpers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ export async function generateDataKey<Client extends KMS> (
4040

4141
/* Postcondition: KMS must return serializable generate data key. */
4242
if (!isRequiredGenerateDataKeyOutput<typeof dataKey>(dataKey)) throw new Error('Malformed KMS response.')
43+
44+
/* The KMS Client *may* return a Buffer that is not isolated.
45+
* i.e. the byteOffset !== 0.
46+
* This means that the unencrypted data key is possibly accessible to someone else.
47+
* If this is the node shared Buffer, then other code within this process _could_ find this secret.
48+
* Copy Plaintext to an isolated ArrayBuffer and zero the Plaintext.
49+
* This means that this function will *always* zero out the value returned to it from the KMS client.
50+
* While this is safe to do here, copying this code somewhere else may produce unexpected results.
51+
*/
52+
const {Plaintext} = dataKey
53+
dataKey.Plaintext = new Uint8Array(Plaintext)
54+
Plaintext.fill(0)
4355
return dataKey
4456
}
4557

0 commit comments

Comments
 (0)