Reject zero-length Buffer/KeyObject secrets for HS* algorithms, like empty strings

Open
#1,052 1 comment 0 reactions 0 assignees View on GitHub

@ricardoBLabs is already working on this.

Since Oct 29, 2026.

  • #1053 by @ricardoBLabs — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, node.js

Research direction

Start by reading the sign and verify key validation paths and the tests for empty string secrets; the issue names no file paths. Check how keys become KeyObjects and how HS* algorithms handle a zero-length symmetric key. Done means zero-length Buffer and secret KeyObject inputs are rejected in sign and verify with the existing errors, while algorithm: 'none' behavior remains unchanged. The issue says a PR is ready, so check the linked work before starting.

Written by the indexing model from the issue text.

Description

Describe the problem you'd like to have solved

jwt.sign() and jwt.verify() reject an empty string secret (secretOrPrivateKey must have a value / secret or public key must be provided). A zero-length Buffer, or a secret KeyObject created from one, is truthy, so it gets past that check. HS256/HS384/HS512 then sign and verify with an empty HMAC key.

In practice this comes from configuration, not intent. For example, the secret is read from a file that exists but is empty (fs.readFileSync(path)), or it is decoded from an unset variable (Buffer.from(process.env.JWT_SECRET ?? '', 'base64')). The application keeps working, because it signs and verifies its own tokens, but anyone can compute valid tokens for an empty key. With '' the same mistake fails loudly.

This has been visible since auth0/node-jws#51. It is related to #286 and #335, which cover missing or empty string secrets.

Describe the ideal solution

Treat a zero-length symmetric key the same way as an empty string for HS* algorithms. After the key has been converted to a KeyObject, reject symmetricKeySize === 0 in sign and verify, using the existing error messages. algorithm: 'none' behaviour stays the same (#386).

Alternatives and current work-arounds

Applications can check the key length themselves before calling the library.

Additional context

I have a small PR ready with tests and will link it here.

Dominant language
JavaScript
Stars
18.2k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from auth0/node-jsonwebtoken

All issues in auth0/node-jsonwebtoken

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.