SKY-D246 · Security in code
JWT read with decode() instead of verify()
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
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
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
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
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
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
- Runs with
--dangeron TypeScript and JavaScript files. Finds every call of the form<object>.decode(...). - Reports HIGH when the object, lower-cased, is exactly
jwt,jsonwebtokenorjwtlib. The arguments are not read, sojwt.decode(token, { complete: true })counts too. - The import is not checked: a different module imported under the name
jwtis reported, andjsonwebtokenimported under another name (import tokens from "jsonwebtoken") is not. Python'sjwt.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.
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 thekidheader to choose a key, followed byjwt.verify(), is still reported. So is browser code that only displays a name. - Missed:
jsonwebtokenimported under another name, a destructureddecode(const { decode } = require("jsonwebtoken")), and other libraries such asjose'sdecodeJwt().
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--baselineaccept the existing calls and report only new ones.