Skip to content

Commit 8c75cfb

Browse files
authored
fix: KMS client plaintext byteOffset (#46)
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 0575a2e commit 8c75cfb

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
@@ -80,6 +80,18 @@ export async function decrypt<Client extends KMS> (
8080

8181
/* Postcondition: KMS must return usable decrypted key. */
8282
if (!isRequiredDecryptOutput(dataKey)) throw new Error('Malformed KMS response.')
83+
84+
/* The KMS Client *may* return a Buffer that is not isolated.
85+
* i.e. the byteOffset !== 0.
86+
* This means that the unencrypted data key is possibly accessible to someone else.
87+
* If this is the node shared Buffer, then other code within this process _could_ find this secret.
88+
* Copy Plaintext to an isolated ArrayBuffer and zero the Plaintext.
89+
* This means that this function will *always* zero out the value returned to it from the KMS client.
90+
* While this is safe to do here, copying this code somewhere else may produce unexpected results.
91+
*/
92+
const {Plaintext} = dataKey
93+
dataKey.Plaintext = new Uint8Array(Plaintext)
94+
Plaintext.fill(0)
8395
return dataKey
8496
}
8597

0 commit comments

Comments
 (0)