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

Offen
#1,052 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

@ricardoBLabs arbeitet bereits daran.

Seit 29.10.2026.

  • #1053 von @ricardoBLabs — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
javascript, node.js

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
JavaScript
Sterne
18.2k
Forks
1.3k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus auth0/node-jsonwebtoken

Alle Issues in auth0/node-jsonwebtoken

Ähnliche Issues

Weitere Issues zu JavaScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.