Skip to content
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

fix(ext/node): resource leak in createHmac #18229

Merged
merged 3 commits into from Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions cli/tests/unit_node/crypto_hash.ts
@@ -0,0 +1,24 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { createHash, createHmac } from "node:crypto";
import { assertEquals } from "../../../test_util/std/testing/asserts.ts";

// https://github.com/denoland/deno/issues/18140
Deno.test({
name: "createHmac digest",
fn() {
assertEquals(
createHmac("sha256", "secret").update("hello").digest("hex"),
"88aab3ede8d3adf94d26ab90d3bafd4a2083070c3bcce9c014ee04a443847c0b",
);
},
});

Deno.test({
name: "createHash digest",
fn() {
assertEquals(
createHash("sha256").update("hello").digest("hex"),
"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
);
},
});
4 changes: 2 additions & 2 deletions ext/node/polyfills/internal/crypto/hash.ts
Expand Up @@ -178,15 +178,15 @@ class HmacImpl extends Transform {
const u8Key = prepareSecretKey(key, options?.encoding) as Buffer;

const alg = hmac.toLowerCase();
this.#hash = new Hash(alg, options);
this.#algorithm = alg;
const blockSize = (alg === "sha512" || alg === "sha384") ? 128 : 64;
const keySize = u8Key.length;

let bufKey: Buffer;

if (keySize > blockSize) {
bufKey = this.#hash.update(u8Key).digest() as Buffer;
const hash = new Hash(alg, options);
bufKey = hash.update(u8Key).digest() as Buffer;
} else {
bufKey = Buffer.concat([u8Key, this.#ZEROES], blockSize);
}
Expand Down