A JSON Web Token is signed or checked with a key written in the source

tx0/ts:hardcoded-jwt-secretrecommendedwarningsecurity/secretsdepth fastsecurity, cwe-798

What it finds

jwt.sign(payload, "s3cret"): with a symmetric algorithm the same key signs and verifies tokens, so a key in the source lets everyone who can read it mint tokens that pass, for any user (CWE-798). A fallback such as process.env.JWT_SECRET || "dev-secret" does the same wherever the variable is missing, which is how it reaches production.

Reported are the keys of jwt.sign and jwt.verify (also through jsonwebtoken) that are a non-empty string literal, a constant initialized with one, or || or ?? with one on the right. Test files are left alone.

From the rule's source, hardcoded_jwt_secret.rs.

Example from the rule's tests
import jwt from "jsonwebtoken";const SECRET = "change-this-secret-key";const FROM_ENV = process.env.JWT_SECRET;export function tokens(payload: object, token: string) {  const a = jwt.sign(payload, "s3cret");tx0 reports: this token key is written in the source  const b = jwt.verify(token, SECRET);tx0 reports: this token key is written in the source  const c = jwt.sign(payload, process.env.JWT_SECRET || "dev-secret", { expiresIn: "1h" });tx0 reports: this token key falls back to a value written in the source  const d = jwt.sign(payload, process.env.JWT_SECRET ?? "", { expiresIn: "1h" });  const e = jwt.verify(token, FROM_ENV as string);  const f = jwt.sign(payload, readKey(), { algorithm: "RS256" });  return [a, b, c, d, e, f];}function readKey() {  return String(process.env.KEY);}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:hardcoded-jwt-secret" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:hardcoded-jwt-secret] reason="..."

tx0 explain tx0/ts:hardcoded-jwt-secret shows the same in the terminal. Found a false positive? Report it.