Skip to content

fix: KMS client plaintext byteOffset (generate too) #47

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 1 commit into from
Apr 16, 2019
Merged
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
12 changes: 12 additions & 0 deletions modules/kms-keyring/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export async function generateDataKey<Client extends KMS> (

/* Postcondition: KMS must return serializable generate data key. */
if (!isRequiredGenerateDataKeyOutput<typeof dataKey>(dataKey)) throw new Error('Malformed KMS response.')

/* 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.
* This means that this function will *always* zero out the value returned to it from the KMS client.
* While this is safe to do here, copying this code somewhere else may produce unexpected results.
*/
const {Plaintext} = dataKey
dataKey.Plaintext = new Uint8Array(Plaintext)
Plaintext.fill(0)
return dataKey
}

Expand Down