Reject zero-length Buffer/KeyObject secrets for HS* algorithms, like empty strings
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
- Domain
- authentication, backend, security
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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from auth0/node-jsonwebtoken
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
auth0/node-jsonwebtoken#1042 · 2 comments ·
-
`jwt.sign()` callback is executed twice for "The payload already has an "..." property" errorsPossibly taken @cobyfrombrooklyn-bot claimed this 268 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
auth0/node-jsonwebtoken#1000 · 2 comments · 1 reaction ·
-
Difficulty 5/5 Over a week Newbie friendliness 15/100
auth0/node-jsonwebtoken#1054 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
auth0/node-jsonwebtoken#1048 · 1 comment ·
-
verify() resolves a string secret by attempting createPublicKey() first, costing 4x-52x on the HS* pathPossibly taken @Hashim1999164 claimed this 62 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 65/100
auth0/node-jsonwebtoken#1046 ·
All issues in auth0/node-jsonwebtoken
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
OWASP/CheatSheetSeries#3018 ·
Maintainers usually reply within 1 day
-
Radix number block generates NaN after switching a negative or decimal number to binary, octal or hexPossibly taken @matthewalberto claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
mit-cml/appinventor-sources#4246 · 1 comment ·
Maintainers usually reply within 1 day
-
marketing
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 83/100
GoogleChrome/modern-web-guidance-src#3675 · 2 comments ·
Maintainers usually reply within 1 day