1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 08:33:43 -05:00

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

This commit fixes https://github.com/denoland/deno/issues/18140.
Verified that test fails on `main`.
This commit is contained in:
Divy Srivastava 2023-03-17 02:55:12 +05:30 committed by GitHub
parent 2c7174a5a2
commit 1300d6178e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View file

@ -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",
);
},
});

View file

@ -178,7 +178,6 @@ class HmacImpl extends Transform {
const u8Key = prepareSecretKey(key, options?.encoding) as Buffer; const u8Key = prepareSecretKey(key, options?.encoding) as Buffer;
const alg = hmac.toLowerCase(); const alg = hmac.toLowerCase();
this.#hash = new Hash(alg, options);
this.#algorithm = alg; this.#algorithm = alg;
const blockSize = (alg === "sha512" || alg === "sha384") ? 128 : 64; const blockSize = (alg === "sha512" || alg === "sha384") ? 128 : 64;
const keySize = u8Key.length; const keySize = u8Key.length;
@ -186,7 +185,8 @@ class HmacImpl extends Transform {
let bufKey: Buffer; let bufKey: Buffer;
if (keySize > blockSize) { 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 { } else {
bufKey = Buffer.concat([u8Key, this.#ZEROES], blockSize); bufKey = Buffer.concat([u8Key, this.#ZEROES], blockSize);
} }