Skip to main content

SKY-D246 · Security in code

JWT read with decode() instead of verify()

High

A JWT (JSON Web Token) is a signed piece of text that says who a user is. In the jsonwebtoken package, jwt.decode() only unpacks the token and never checks the signature; jwt.verify() does both. SKY-D246 reports every jwt.decode() call in TypeScript and JavaScript.

Languages
TypeScript/JavaScript
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Catalog name
JWT decode without verification
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

Anyone can build a token that says "sub": "admin". Server code that trusts what jwt.decode() returns lets an attacker act as any user, including administrators, with a token they wrote themselves.

What Skylos flags

src/middleware/auth.ts · TypeScriptReported on line 6
import jwt from "jsonwebtoken";import type { NextFunction, Request, Response } from "express"; export function requireUser(req: Request, res: Response, next: NextFunction) {  const token = req.headers.authorization?.replace("Bearer ", "") ?? "";  const payload = jwt.decode(token) as { sub?: string } | null;  if (!payload?.sub) {    return res.status(401).end();  }  res.locals.userId = payload.sub;  next();}

What Skylos leaves alone

src/middleware/auth.ts · TypeScript
import jwt, { type JwtPayload } from "jsonwebtoken";import type { NextFunction, Request, Response } from "express"; const secret = process.env.JWT_SECRET ?? ""; export function requireUser(req: Request, res: Response, next: NextFunction) {  const token = req.headers.authorization?.replace("Bearer ", "") ?? "";  try {    const payload = jwt.verify(token, secret, { algorithms: ["HS256"] });    res.locals.userId = (payload as JwtPayload).sub;    next();  } catch {    res.status(401).end();  }}

Why Skylos leaves it alone

jwt.verify() checks the signature with the server's secret and allows only HS256 before any claim is used. The rule looks only for calls to a method named decode.

More languages

JavaScript

Flagged

lib/session.js · JavaScriptReported on line 4
const jsonwebtoken = require("jsonwebtoken"); function sessionUser(cookieToken) {  const claims = jsonwebtoken.decode(cookieToken);  return claims ? { id: claims.sub, role: claims.role } : null;} module.exports = { sessionUser };

Not flagged

web/header.js · JavaScript
import { jwtDecode } from "jwt-decode"; export function showSignedInName(idToken) {  const { name } = jwtDecode(idToken);  document.querySelector("#user-name").textContent = name;}

Why Skylos leaves it alone

This is browser code that only shows the user's name; the server still verifies the token on every request, so reading it unverified here grants nothing. The jwt-decode helper is not a .decode() call on jwt, jsonwebtoken or jwtlib, so it is not reported.

How Skylos decides

  1. Runs with --danger on TypeScript and JavaScript files. Finds every call of the form <object>.decode(...).
  2. Reports HIGH when the object, lower-cased, is exactly jwt, jsonwebtoken or jwtlib. The arguments are not read, so jwt.decode(token, { complete: true }) counts too.
  3. The import is not checked: a different module imported under the name jwt is reported, and jsonwebtoken imported under another name (import tokens from "jsonwebtoken") is not. Python's jwt.decode() with verification turned off is SKY-D232.

How to fix it

Use jwt.verify() with the secret or public key and a fixed algorithms list, and reject the request when it throws. Never base an access decision on what jwt.decode() returns.

src/middleware/auth.ts · TypeScript · the fix, against the flagged file
Removed: import jwt from "jsonwebtoken";Added: import jwt, { type JwtPayload } from "jsonwebtoken";import type { NextFunction, Request, Response } from "express"; Added: const secret = process.env.JWT_SECRET ?? "";Added:  export function requireUser(req: Request, res: Response, next: NextFunction) {  const token = req.headers.authorization?.replace("Bearer ", "") ?? "";Removed:   const payload = jwt.decode(token) as { sub?: string } | null;Removed:   if (!payload?.sub) {Added:   let payload: JwtPayload;Added:   try {Added:     payload = jwt.verify(token, secret, { algorithms: ["HS256"] }) as JwtPayload;Added:   } catch {    return res.status(401).end();  }Added:   if (!payload.sub) {Added:     return res.status(401).end();Added:   }  res.locals.userId = payload.sub;  next();}

When it can be wrong

  • A jwt.decode(token, { complete: true }) that only reads the kid header to choose a key, followed by jwt.verify(), is still reported. So is browser code that only displays a name.
  • Missed: jsonwebtoken imported under another name, a destructured decode (const { decode } = require("jsonwebtoken")), and other libraries such as jose's decodeJwt().

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Mark the reviewed line with a comment that says why the result is not trusted.

    TypeScript
    const shown = jwt.decode(token); // skylos: ignore[SKY-D246] display only, not trusted
  • [tool.skylos] ignore = ["SKY-D246"] turns the rule off for the whole project.

  • skylos baseline . and then --baseline accept the existing calls and report only new ones.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.