Claude skill JWT none algorithm and JWK injection via kid header — SA-SKL-JWT5
JWT implementation mistakes remain among the highest-impact authentication vulnerabilities in MCP skill servers because a single flaw typically grants the attacker the ability to forge an arbitrary identity — including the platform's own service accounts. SA-SKL-JWT5 covers four critical and high severity patterns found across MCP skill audit engagements: accepting the alg:none algorithm due to a missing algorithms option in jsonwebtoken.verify; fetching the JWKS signing key from a URL constructed using the token's own kid header field, enabling an attacker-controlled key injection; using the kid header value as an unsanitized SQL parameter, turning JWT verification into a SQL injection vector; and trusting claims decoded from an unverified JWT before the signature check completes, enabling sub-claim forgery for identity impersonation.
JWT algorithm confusion and header injection in MCP skill deployments
MCP skill servers that implement their own JWT verification middleware rather than delegating to a fully managed identity provider are responsible for every detail of the verification algorithm. The JWT specification was designed with flexibility in mind — the alg and kid header fields are attacker-controlled inputs that most libraries will process before they can be validated against an expected value. The result is that the attack surface for JWT bypasses lives almost entirely in the gap between what the library accepts by default and what the application enforces explicitly. All four SA-SKL-JWT5 patterns follow the same root cause: the application trusts a header field from the token without validating it against a server-side constraint first.
jsonwebtoken.verify(token, secret) without algorithms option; attacker sets alg:none and omits signature; jsonwebtoken versions < 9 accept alg:none without explicit opt-inkid header URL; attacker supplies kid: "https://attacker.com/jwks.json" with attacker-controlled public key; server verifies signature against attacker keykid used in raw SQL query SELECT secret FROM keys WHERE kid='${header.kid}'; attacker injects ' OR '1'='1 returning any/all keyssub used for DB lookup before signature check; attacker forges sub to impersonate any userJWT5-001 — jsonwebtoken.verify without algorithms option accepts alg:none
The jsonwebtoken npm package's verify function accepts an optional algorithms array that restricts which signing algorithms are permitted. Without this option, versions of jsonwebtoken prior to 9.0.0 would accept a token with alg: "none" in its header and an empty signature segment — interpreting it as an unsigned token that is valid by definition. An attacker can take any legitimate JWT, decode it, modify the payload (setting role: "admin" or sub: "service-account"`), re-encode it with alg: "none" in the header, and strip the signature. If the server calls jwt.verify(token, secret) without specifying { algorithms: ['RS256'] }, the modified token passes verification. Even on jsonwebtoken >= 9, the explicit algorithms option is still required best practice to prevent algorithm confusion attacks between RS256 and HS256.
// VULNERABLE — jwt.verify without algorithms option; alg:none accepted in jsonwebtoken < 9
import jwt from 'jsonwebtoken';
app.use((req, res, next) => {
const token = req.headers.authorization?.replace('Bearer ', '');
if (!token) return res.status(401).json({ error: 'Missing token' });
try {
// VULNERABLE: no algorithms option — attacker sets alg:none in token header;
// jsonwebtoken < 9 accepts the unsigned token as valid
const payload = jwt.verify(token, process.env.JWT_SECRET);
req.user = payload;
next();
} catch (err) {
res.status(401).json({ error: 'Invalid token' });
}
});
// FIXED — explicit algorithms array; rejects alg:none and prevents algorithm confusion
import jwt from 'jsonwebtoken';
app.use((req, res, next) => {
const token = req.headers.authorization?.replace('Bearer ', '');
if (!token) return res.status(401).json({ error: 'Missing token' });
try {
const payload = jwt.verify(token, process.env.JWT_SECRET, {
algorithms: ['RS256'], // only accept RS256 — rejects alg:none, HS256, RS384, etc.
issuer: 'https://auth.skillaudit.dev',
audience: 'skillaudit-api',
});
req.user = payload;
next();
} catch (err) {
// jwt.JsonWebTokenError covers alg:none rejection, invalid signature, expired, etc.
res.status(401).json({ error: 'Invalid token' });
}
});
JWT5-002 — JWKS endpoint fetched from kid header URL
When a skill server supports RS256 tokens and needs to rotate signing keys, it may implement a JWKS endpoint lookup using the token's kid header to identify which public key to use for verification. A dangerous implementation constructs the JWKS fetch URL by concatenating a base URL with the kid value, or uses the kid value directly as the fetch URL. An attacker crafts a token with kid: "https://attacker.com/jwks.json" pointing to a JWKS document the attacker controls — containing the attacker's own RSA public key. The server fetches the attacker-controlled JWKS, extracts the public key, and verifies the token's signature — which the attacker signed with their corresponding private key. Verification succeeds, granting the attacker full control over all token claims. The fix is to store the JWKS URI exclusively in a server-side environment variable and never read it from any token field.
// VULNERABLE — JWKS URI constructed from kid header value; attacker controls key source
import jwksClient from 'jwks-rsa';
async function getSigningKey(header) {
// VULNERABLE: kid header used directly as the JWKS URI — or used to construct it:
// const jwksUri = `https://auth.skillaudit.dev/.well-known/jwks/${header.kid}`;
// Attacker sets kid: "https://evil.com/jwks.json" pointing to their own JWKS
const client = jwksClient({ jwksUri: header.kid }); // kid IS the URI — fully attacker controlled
return new Promise((resolve, reject) => {
client.getSigningKey(header.kid, (err, key) => {
if (err) reject(err);
else resolve(key.getPublicKey());
});
});
}
app.use(async (req, res, next) => {
const token = req.headers.authorization?.replace('Bearer ', '');
const decoded = jwt.decode(token, { complete: true });
const signingKey = await getSigningKey(decoded.header); // fetches from attacker URL
const payload = jwt.verify(token, signingKey, { algorithms: ['RS256'] });
req.user = payload;
next();
});
// FIXED — JWKS URI hardcoded in server config; kid used only as key identifier lookup
import jwksClient from 'jwks-rsa';
// JWKS URI comes from server-side environment — never from the token
const client = jwksClient({
jwksUri: process.env.JWKS_URI, // e.g. "https://auth.skillaudit.dev/.well-known/jwks.json"
cache: true,
cacheMaxEntries: 5,
cacheMaxAge: 600_000, // 10 minutes
rateLimit: true,
});
function getSigningKey(kid) {
// kid is used only to select among the keys already fetched from the hardcoded URI
return new Promise((resolve, reject) => {
client.getSigningKey(kid, (err, key) => {
if (err) reject(err);
else resolve(key.getPublicKey());
});
});
}
app.use(async (req, res, next) => {
const token = req.headers.authorization?.replace('Bearer ', '');
if (!token) return res.status(401).json({ error: 'Missing token' });
try {
const { header } = jwt.decode(token, { complete: true }) || {};
if (!header?.kid) return res.status(401).json({ error: 'Missing kid' });
const signingKey = await getSigningKey(header.kid); // fetches from server-configured URI only
const payload = jwt.verify(token, signingKey, { algorithms: ['RS256'] });
req.user = payload;
next();
} catch (err) {
res.status(401).json({ error: 'Invalid token' });
}
});
JWT5-003 — kid header value used in raw SQL query
When a skill server stores signing secrets in a database keyed by a kid identifier, it must retrieve the correct secret to verify each token. A common implementation reads the kid from the decoded token header and interpolates it directly into a SQL query string. Because the kid is attacker-controlled — it is part of the JWT header, which is base64-decoded before signature verification — an attacker can set kid: "' OR '1'='1' --" to cause the query to return all rows, or use a UNION SELECT to return an arbitrary string as the secret. If the attacker knows the returned value, they can then sign a token with it and verify successfully. The fix is a parameterized query plus UUID format validation on the kid value before it is used in any database operation.
// VULNERABLE — kid from JWT header interpolated directly into SQL query
import jwt from 'jsonwebtoken';
import { db } from './db.js';
app.use(async (req, res, next) => {
const token = req.headers.authorization?.replace('Bearer ', '');
const decoded = jwt.decode(token, { complete: true });
const kid = decoded?.header?.kid;
// VULNERABLE: kid is attacker-controlled; interpolated into SQL without parameterization
// Attacker sets kid: "' OR '1'='1' --" → returns all secrets; first row used
// Attacker sets kid: "x' UNION SELECT 'attacker_known_secret' --" → custom secret
const row = await db.query(`SELECT secret FROM signing_keys WHERE kid='${kid}'`);
if (!row) return res.status(401).json({ error: 'Unknown kid' });
const payload = jwt.verify(token, row.secret, { algorithms: ['HS256'] });
req.user = payload;
next();
});
// FIXED — parameterized query; UUID format validation on kid before DB access
import jwt from 'jsonwebtoken';
import { db } from './db.js';
// kid values are UUIDs in this deployment — validate format before any DB use
const KID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
app.use(async (req, res, next) => {
const token = req.headers.authorization?.replace('Bearer ', '');
if (!token) return res.status(401).json({ error: 'Missing token' });
let decoded;
try {
decoded = jwt.decode(token, { complete: true });
} catch {
return res.status(401).json({ error: 'Malformed token' });
}
const kid = decoded?.header?.kid;
// Validate kid format before it touches the database
if (!kid || !KID_PATTERN.test(kid)) {
return res.status(401).json({ error: 'Invalid kid format' });
}
// Parameterized query — kid is a bound parameter, not string interpolation
const row = await db.query('SELECT secret FROM signing_keys WHERE kid = ?', [kid]);
if (!row) return res.status(401).json({ error: 'Unknown kid' });
try {
const payload = jwt.verify(token, row.secret, { algorithms: ['HS256'] });
req.user = payload;
next();
} catch {
res.status(401).json({ error: 'Invalid token' });
}
});
JWT5-004 — JWT decoded before verification for routing decisions
It is tempting to call jwt.decode() at the start of a request to read the sub or org claim and route the request to the appropriate database shard or tenant namespace before the full async verification step completes. jwt.decode() does no signature verification — it simply base64-decodes the payload section of the JWT string. Any claims in the decoded output are entirely attacker-controlled at this point. If the application uses the decoded sub claim to look up a user record and attaches that record to req.user before jwt.verify completes, an attacker can forge any sub value and impersonate any user. The verified payload returned by jwt.verify must be the only source of claims used for authentication or authorization decisions.
// VULNERABLE — jwt.decode used for routing before jwt.verify completes
import jwt from 'jsonwebtoken';
import { getUserById } from './users.js';
app.use(async (req, res, next) => {
const token = req.headers.authorization?.replace('Bearer ', '');
if (!token) return res.status(401).json({ error: 'Missing token' });
// VULNERABLE: jwt.decode does NO signature verification
// attacker forges sub: "00000000-0000-0000-0000-000000000001" (admin user ID)
const unverified = jwt.decode(token); // ← attacker controls all fields in this object
const userId = unverified.sub; // ← forged sub; used before verify completes
// DB lookup with forged userId happens before signature is verified
const user = await getUserById(userId);
req.user = user; // ← attacker has now impersonated the target user
// jwt.verify called too late — req.user already set from forged claim
jwt.verify(token, process.env.JWT_SECRET, { algorithms: ['RS256'] }, (err) => {
if (err) return res.status(401).json({ error: 'Invalid token' });
next(); // by here req.user is already the impersonated user
});
});
// FIXED — jwt.verify first; only use verified payload for all auth decisions
import jwt from 'jsonwebtoken';
import { getUserById } from './users.js';
app.use(async (req, res, next) => {
const token = req.headers.authorization?.replace('Bearer ', '');
if (!token) return res.status(401).json({ error: 'Missing token' });
let verifiedPayload;
try {
// jwt.verify throws if signature is invalid, expired, wrong algorithm, etc.
// The return value is the VERIFIED payload — safe to use for auth decisions
verifiedPayload = jwt.verify(token, process.env.JWT_SECRET, {
algorithms: ['RS256'],
issuer: 'https://auth.skillaudit.dev',
audience: 'skillaudit-api',
});
} catch (err) {
return res.status(401).json({ error: 'Invalid token' });
}
// Only after successful verify do we trust any claim in the payload
const userId = verifiedPayload.sub;
if (!userId) return res.status(401).json({ error: 'Missing sub claim' });
const user = await getUserById(userId);
if (!user) return res.status(401).json({ error: 'User not found' });
req.user = user; // set only from verified identity
next();
});
Related patterns: Earlier JWT algorithm confusion patterns (RS256/HS256 key confusion and embedded JWK in header) are covered in SA-SKL-JWT4. For SQL injection via other attacker-controlled header fields beyond kid, see SA-SKL-INJECTION5. The SA-SKL-SSRF10 page covers SSRF that can be chained with the JWKS URI injection pattern to reach internal token signing services.
| Finding | Severity | Vulnerable pattern | Fix |
|---|---|---|---|
JWT5-001 | Critical | jwt.verify(token, secret) without algorithms option; attacker sets alg:none; unsigned token accepted in jsonwebtoken < 9 | jwt.verify(token, secret, { algorithms: ['RS256'] }) — always specify algorithms array explicitly |
JWT5-002 | Critical | JWKS URI fetched from kid header URL; attacker-controlled public key used for verification; full token forgery | Hardcode JWKS URI in process.env.JWKS_URI; never construct JWKS URL from token header fields |
JWT5-003 | Critical | kid in raw SQL WHERE kid='${header.kid}'; SQL injection returns any secret; attacker signs forged token | Parameterized query WHERE kid = ? with [kid]; UUID format validation on kid before query |
JWT5-004 | High | jwt.decode() output used for DB lookup before jwt.verify(); forged sub impersonates any user | Always call jwt.verify() first; use only the verified payload return value for all auth decisions |