Skip to content

Commit

Permalink
src,crypto: fix 0-length output crash in webcrypto
Browse files Browse the repository at this point in the history
  • Loading branch information
XadillaX committed Jun 3, 2021
1 parent 21f5a56 commit 490e2cf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/crypto/crypto_cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class CipherJob final : public CryptoJob<CipherTraits> {
v8::Local<v8::Value>* result) override {
Environment* env = AsyncWrap::env();
CryptoErrorStore* errors = CryptoJob<CipherTraits>::errors();
if (out_.size() > 0) {
if (out_.size() > 0 || (!out_.size() && errors->Empty())) {
CHECK(errors->Empty());
*err = v8::Undefined(env->isolate());
*result = out_.ToArrayBuffer(env);
Expand Down
38 changes: 38 additions & 0 deletions test/parallel/test-crypto-subtle-zero-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

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

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

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

crypto.subtle.importKey(
'raw',
new Uint8Array(32),
{ name: 'AES-GCM' },
false,
[ 'encrypt','decrypt' ]).then(k => {
assert(k instanceof crypto.CryptoKey);
return crypto.subtle.encrypt({
name: 'AES-GCM',
iv: new Uint8Array(12),
}, k, new Uint8Array(0)).then(e => {
assert(e instanceof ArrayBuffer);
assert.deepStrictEqual(
Buffer.from(e),
Buffer.from([
0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9,
0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b ]));
return crypto.subtle.decrypt({
name: 'AES-GCM',
iv: new Uint8Array(12) },
k,
e);
});
})
.then(v => {
assert(v instanceof ArrayBuffer);
assert.strictEqual(v.byteLength, 0);
});

0 comments on commit 490e2cf

Please sign in to comment.