Static analysis (SAST) check · TypeScript

JWT decode unverified (TypeScript)

A JSON Web Token is decoded with jwt.decode or jwt-decode, which does not check the signature, and the claims are used.

Why it matters

A caller can write any claims they like.

How to fix it

Verify with jwt.verify(token, key, { algorithms: [...] }) or jose's jwtVerify on the server before trusting any claim.

Example

The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.

Vulnerable: reported (TypeScript)
import jwt from "jsonwebtoken";

export function currentUser(token: string) {
  const claims = jwt.decode(token) as { sub: string };
  return claims.sub;
}
Fixed: not reported (TypeScript)
import jwt from "jsonwebtoken";

export function currentUser(token: string) {
  const claims = jwt.verify(token, process.env.JWT_KEY!, { algorithms: ["HS256"] }) as { sub: string };
  return claims.sub;
}

What TrustIvan reads

Runs on repository scans of TypeScript source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.

Weakness classes and references

Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10

All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation

See TrustIvan on your own estate.

Book thirty minutes with an engineer and bring an image, a repository or a cloud account, or start free on the hosted service.