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

src: fix abort in pbkdf2 #38354

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 3 additions & 9 deletions src/crypto/crypto_pbkdf2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,20 @@ Maybe<bool> PBKDF2Traits::AdditionalConfig(

params->iterations = args[offset + 2].As<Int32>()->Value();
if (params->iterations < 0) {
char msg[1024];
snprintf(msg, sizeof(msg), "iterations must be <= %d", INT_MAX);
THROW_ERR_OUT_OF_RANGE(env, msg);
THROW_ERR_OUT_OF_RANGE(env, "iterations must be <= %d", INT_MAX);
return Nothing<bool>();
}

params->length = args[offset + 3].As<Int32>()->Value();
if (params->length < 0) {
char msg[1024];
snprintf(msg, sizeof(msg), "length must be <= %d", INT_MAX);
THROW_ERR_OUT_OF_RANGE(env, msg);
THROW_ERR_OUT_OF_RANGE(env, "length must be <= %d", INT_MAX);
return Nothing<bool>();
}

Utf8Value name(args.GetIsolate(), args[offset + 4]);
params->digest = EVP_get_digestbyname(*name);
if (params->digest == nullptr) {
char errmsg[1024];
snprintf(errmsg, sizeof(errmsg), "Invalid digest: %s", *name);
THROW_ERR_CRYPTO_INVALID_DIGEST(env, errmsg);
THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *name);
return Nothing<bool>();
}

Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-crypto-pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,15 @@ if (!common.hasOpenSSL3) {
runPBKDF2(new Uint8Array(10), 'salt', 8, 8, hash);
});
}

{
// This should not crash.
assert.throws(
() => crypto.pbkdf2Sync('1', '2', 1, 1, '%'),
{
code: 'ERR_CRYPTO_INVALID_DIGEST',
name: 'TypeError',
message: 'Invalid digest: %'
}
);
}