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

Aperta
#1,052 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

@ricardoBLabs ci sta già lavorando.

Dal 29/10/2026.

  • #1053 di @ricardoBLabs — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, node.js

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
JavaScript
Stelle
18.2k
Fork
1.3k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di auth0/node-jsonwebtoken

Tutte le issue di auth0/node-jsonwebtoken

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.