Skip to content
Closed
Changes from 1 commit
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
Next Next commit
test: fix flaky test-webcrypto-encrypt-decrypt-aes
* Use a copy of plaintext to prevent tampering of the original
* Since subtle.decrypt returns a Promise containing an ArrayBuffer and
  ArrayBuffers cannot be modified directly, create a Buffer from it
  right away so that the modification in the next line works as intended

Fixes: #35586
  • Loading branch information
RaisinTen committed Feb 17, 2021
commit c8b1459a06c5cf6f14dd88c92887b64942d16a7b
9 changes: 7 additions & 2 deletions test/parallel/test-webcrypto-encrypt-decrypt-aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const assert = require('assert');
const { getRandomValues, subtle } = require('crypto').webcrypto;

async function testEncrypt({ keyBuffer, algorithm, plaintext, result }) {
// using a copy of plaintext to prevent tampering of the original
plaintext = Buffer.from(plaintext);

const key = await subtle.importKey(
'raw',
keyBuffer,
Expand All @@ -23,8 +26,10 @@ async function testEncrypt({ keyBuffer, algorithm, plaintext, result }) {
Buffer.from(output).toString('hex'),
Buffer.from(result).toString('hex'));

const check = await subtle.decrypt(algorithm, key, output);
output[0] = 255 - output[0];
// converting the returned ArrayBuffer into a Buffer right away,
// so that the next line works
const check = Buffer.from(await subtle.decrypt(algorithm, key, output));
check[0] = 255 - check[0];

assert.strictEqual(
Buffer.from(check).toString('hex'),
Expand Down