Skip to content
Closed
Show file tree
Hide file tree
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
crypto: fix aes crash when tag length too small
Refs: #38883
  • Loading branch information
XadillaX committed Jun 3, 2021
commit 0ce31fb8f3ef0cf8948db04e74235c395c38701d
5 changes: 5 additions & 0 deletions lib/internal/crypto/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ function asyncAesGcmCipher(
const slice = ArrayBufferIsView(data) ?
TypedArrayPrototypeSlice : ArrayBufferPrototypeSlice;
tag = slice(data, -tagByteLength);
if (tagByteLength > tag.byteLength) {
throw lazyDOMException(
'The provided data is too small.',
'OperationError');
}
data = slice(data, 0, -tagByteLength);
break;
case kWebCryptoCipherEncrypt:
Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-crypto-webcrypto-aes-decrypt-tag-too-small.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const common = require('../common');

if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const crypto = require('crypto').webcrypto;

crypto.subtle.importKey(
'raw',
new Uint8Array(32),
{
name: 'AES-GCM'
},
false,
[ 'encrypt', 'decrypt' ])
.then((k) => {
assert.rejects(async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function does not need the async keyword, and using it hides whether the exception is thrown synchronously or the Promise is actually rejected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmmm, Chrome rejects this situation in Promise. So shall we do reject or throw?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

window.crypto.subtle.decrypt({name: 'AES-GCM', iv: new Uint8Array(12)}, k, new Uint8Array(0));
> Promise {<pending>}
Uncaught (in promise) DOMException: The provided data is too small

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that the behavior is correct, and the test works, but it isn't as strict as it could be. assert.rejects is fine, but the async weakens the test:

function fnThatThrows() { throw new Error(); }
async function fnThatRejects() { throw new Error(); }

// With 'async':

assert.rejects(async () => fnThatRejects()); // passes
assert.rejects(async () => fnThatThrows()); // passes, but should not!

// Now remove the 'async' keyword:

assert.rejects(() => fnThatRejects()); // passes
assert.rejects(() => fnThatThrows()); // fails as it should

So I'd simply remove the async keyword from the function declaration :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've resolved it.

return crypto.subtle.decrypt({
name: 'AES-GCM',
iv: new Uint8Array(12),
},
k,
new Uint8Array(0));
}, {
name: 'OperationError',
message: /The provided data is too small/,
});
});