← Blog

Claude skill JWT algorithm confusion security — SA-SKL-JWTALG

JWT algorithm confusion is a class of vulnerabilities where an attacker exploits the gap between the algorithm the server expects to verify and the algorithm specified in the token header. The most critical variant — using an RS256 public key as an HS256 HMAC secret — has been known since 2015 and continues to appear in MCP skill audits. SkillAudit finds four patterns beyond the basic alg:none bypass: accepting both RS256 and HS256 so the public key becomes the HMAC secret, an algorithm array that enables header-driven downgrade, JWT decoded before verified so routing decisions use unverified claims, and the JWT kid header used in a database query without sanitization enabling SQL injection. SA-SKL-JWTALG-001 through JWTALG-004 cover each with the attack vector and jsonwebtoken fix.

By SkillAudit  |  5 Nov 2026

Algorithm confusion vs. the alg:none bypass

The alg:none bypass (a token with no signature that passes verification when none is in the allowed algorithm list) is the most widely documented JWT vulnerability and is covered separately as SA-SKL-JWT. Algorithm confusion attacks are distinct: they exploit the algorithm selection logic when multiple legitimate algorithms are permitted. The classic confusion attack takes an RS256-signed token system (asymmetric: private key signs, public key verifies) and exploits a server that accepts both RS256 and HS256. The attacker crafts a token with the header alg:HS256 and signs it with the RS256 public key as the HMAC-SHA256 secret. The server retrieves the public key (which is intended for RS256 verification), treats it as an HMAC secret for HS256 verification, and verifies the token successfully. The attack requires only the public key — which is typically distributed openly in a JWKS endpoint or embedded in client documentation.

SA-SKL-JWTALG in scope: JWTALG-001 (RS256/HS256 public key confusion), JWTALG-002 (algorithm array enables downgrade), JWTALG-003 (JWT decoded before verified for routing), JWTALG-004 (kid header SQL injection in key lookup). These are distinct from SA-SKL-JWT which covers alg:none bypass, weak HMAC secret brute force, decode-without-verify, and kid path traversal.

JWTALG-001 — RS256/HS256 algorithm confusion

When a server accepts both RS256 (asymmetric) and HS256 (symmetric) for JWT verification and uses the same key material for both, an attacker can forge tokens by signing with the HS256 algorithm using the RS256 public key as the HMAC secret. The public key is not secret — it is the public half of an asymmetric key pair and is typically published at a JWKS endpoint. The attack is feasible whenever: (1) the server's algorithms array includes both RS256 and HS256, and (2) the server uses the RS256 public key as the secret for HS256 verification (which happens implicitly when the server passes the public key PEM as the secretOrPublicKey argument to jwt.verify() without enforcing the algorithm).

// VULNERABLE: accepts both RS256 and HS256 with the same key
const jwt = require('jsonwebtoken');
const fs = require('fs');

const publicKey = fs.readFileSync('./keys/public.pem'); // RS256 public key — not secret

app.use('/api', (req, res, next) => {
  const token = req.headers.authorization?.split(' ')[1];

  try {
    // VULNERABLE: algorithms array includes HS256 alongside RS256
    // An attacker who has the public key (from /jwks.json or docs) can:
    // 1. Create a token with header alg:HS256
    // 2. Sign it with the RS256 public key as the HMAC secret
    // 3. jwt.verify() accepts it because publicKey is used as HMAC secret for HS256
    const payload = jwt.verify(token, publicKey, {
      algorithms: ['RS256', 'HS256'], // ← VULNERABLE: dual algorithm acceptance
    });
    req.user = payload;
    next();
  } catch (err) {
    res.status(401).json({ error: 'Unauthorized' });
  }
});
// FIXED: single algorithm enforced — RS256 only
const jwt = require('jsonwebtoken');
const fs = require('fs');

const publicKey = fs.readFileSync('./keys/public.pem');

app.use('/api', (req, res, next) => {
  const token = req.headers.authorization?.split(' ')[1];

  try {
    // FIXED: exactly one algorithm — RS256 only
    // An attacker cannot change the header alg to HS256 because
    // jwt.verify() will reject any token not signed with RS256
    const payload = jwt.verify(token, publicKey, {
      algorithms: ['RS256'], // ← Single algorithm: no confusion possible
      issuer: process.env.JWT_ISSUER,
      audience: process.env.JWT_AUDIENCE,
    });
    req.user = payload;
    next();
  } catch (err) {
    res.status(401).json({ error: 'Unauthorized' });
  }
});
CRITICAL
SA-SKL-JWTALG-001 — MCP skill JWT middleware passes an RSA public key to jwt.verify() with an algorithms array that includes both RS256 and HS256; an attacker who obtains the public key (from JWKS endpoint, documentation, or source code) can forge a token by setting alg:HS256 in the header and signing with the public key as the HMAC secret — jwt.verify() treats the public key PEM as the HMAC secret and verifies successfully. Fix: enforce exactly one algorithm — algorithms: ['RS256'] — never mix asymmetric and symmetric algorithms in the same middleware. Detection: SkillAudit flags jwt.verify() calls where algorithms includes both RS256/ES256 (asymmetric) and HS256/HS384/HS512 (symmetric) variants.

JWTALG-002 — Algorithm array enables header-driven downgrade

Even without the RS256/HS256 confusion scenario, an algorithms array with multiple entries allows the token's header to select which algorithm the server uses for verification. If a server lists ['RS256', 'RS384', 'RS512'], an attacker cannot forge tokens (all three are asymmetric and require the private key). But if the array includes a weaker algorithm — HS256 alongside a stronger HS512 — tokens with the weaker algorithm are accepted even if the server intended to standardize on the stronger one. The correct practice is to enforce exactly one algorithm at all times; the algorithm should be a configuration value, not a runtime choice.

// VULNERABLE: multiple HS algorithms — attacker downgrades to weakest
const SECRET = process.env.JWT_SECRET; // 32-char secret

jwt.verify(token, SECRET, {
  algorithms: ['HS512', 'HS384', 'HS256'], // ← HS256 accepted even if HS512 intended
  // A token signed with HS256 (easier to brute-force) passes verification
});
// FIXED: single algorithm constant — no downgrade surface
const JWT_ALGORITHM = 'HS256'; // Or 'RS256' for asymmetric — single choice, documented

jwt.verify(token, SECRET, {
  algorithms: [JWT_ALGORITHM], // Single element: no downgrade possible
  issuer: process.env.JWT_ISSUER,
  clockTolerance: 30, // 30s clock skew tolerance
});
HIGH
SA-SKL-JWTALG-002 — MCP skill jwt.verify() call specifies an algorithms array with more than one entry; the JWT header's alg field selects which algorithm is used, enabling an attacker to craft tokens that use a weaker algorithm from the permitted list (e.g., HS256 instead of HS512), or to mix symmetric and asymmetric algorithms if both appear in the list. Fix: enforce a single algorithm as a named constant; algorithms array must contain exactly one element. Detection: SkillAudit flags jwt.verify() calls where the algorithms option is an array with more than one element.

JWTALG-003 — JWT decoded before verified for routing

jwt.decode() returns the token payload without verifying the signature — it is a base64url decode operation, not a security check. When MCP skills use jwt.decode() to extract a claim (such as role, tier, or userId) before calling jwt.verify() — typically to route the request to different logic based on the claim — the routing decision is based on unverified data. An attacker can craft a token with any payload, changing role: 'admin' or tier: 'team', and the decode-based routing will treat the token as an admin or team user before the verification step has a chance to reject it. The vulnerability is especially severe when the routing branch itself performs privileged operations before the verification result is checked.

// VULNERABLE: jwt.decode() used before jwt.verify() for routing
app.use('/api', (req, res, next) => {
  const token = req.headers.authorization?.split(' ')[1];

  // VULNERABLE: unverified decode used to route the request
  const decoded = jwt.decode(token); // No signature check — attacker controls payload

  if (decoded?.role === 'admin') {
    // Routes to admin logic BEFORE the token is verified
    // An attacker sets role:'admin' in a forged (unsigned) token
    req.isAdmin = true;
  }

  // Verification happens AFTER routing decision — too late
  try {
    const payload = jwt.verify(token, PUBLIC_KEY, { algorithms: ['RS256'] });
    req.user = payload;
    next();
  } catch (err) {
    res.status(401).json({ error: 'Unauthorized' });
  }
});
// FIXED: jwt.verify() first — claims only used from verified payload
app.use('/api', (req, res, next) => {
  const token = req.headers.authorization?.split(' ')[1];
  if (!token) return res.status(401).json({ error: 'Missing token' });

  try {
    // FIXED: verify first — only use claims from the verified payload
    const payload = jwt.verify(token, PUBLIC_KEY, {
      algorithms: ['RS256'],
      issuer: process.env.JWT_ISSUER,
    });

    // Claims from verified payload only
    req.user = payload;
    req.isAdmin = payload.role === 'admin'; // Safe: payload is verified

    next();
  } catch (err) {
    res.status(401).json({ error: 'Unauthorized' });
  }
});
HIGH
SA-SKL-JWTALG-003 — MCP skill calls jwt.decode() to extract claims for routing decisions before calling jwt.verify(); since jwt.decode() does not verify the signature, an attacker can craft a token with any payload to influence routing — including setting role:'admin', tier:'team', or any claim that gates access to privileged logic. Fix: always call jwt.verify() first; use claims only from the verified return value; never call jwt.decode() in authentication or authorization middleware. Detection: SkillAudit flags route files and middleware that call jwt.decode() before jwt.verify() in the same request handler.

JWTALG-004 — JWT kid header SQL injection

The JWT kid (key ID) header field is used in multi-key environments to indicate which signing key the verifier should use. A common pattern is to look up the key in a database using the kid value as the query parameter. If the kid value is used in a database query without sanitization, an attacker who crafts a token with a malicious kid can inject SQL into the key lookup query. The injection target is the key retrieval step — before verification has occurred — so an attacker who controls the SQL query can retrieve any row from the keys table, return a key they control, and use it to sign a valid token. The result is a full authentication bypass: the attacker controls both the query that retrieves the key and the key itself.

// VULNERABLE: kid header used in DB query without sanitization
app.use('/api', async (req, res, next) => {
  const token = req.headers.authorization?.split(' ')[1];
  if (!token) return res.status(401).json({ error: 'Missing token' });

  // Decode header to extract kid (before verification — necessary for key lookup)
  const header = jwt.decode(token, { complete: true })?.header;
  const kid = header?.kid;

  // VULNERABLE: kid is attacker-controlled string — SQL injection
  // Crafted: kid = "' OR '1'='1" retrieves the first row of the signing_keys table
  // Crafted: kid = "' UNION SELECT attacker_public_key--" returns attacker key
  const signingKey = await db.raw(
    `SELECT public_key FROM signing_keys WHERE id = '${kid}'` // ← String interpolation
  );

  if (!signingKey.rows[0]) return res.status(401).json({ error: 'Unknown key' });

  try {
    const payload = jwt.verify(token, signingKey.rows[0].public_key, {
      algorithms: ['RS256'],
    });
    req.user = payload;
    next();
  } catch (err) {
    res.status(401).json({ error: 'Unauthorized' });
  }
});
// FIXED: kid validated with strict regex before DB query; parameterized query
const KID_REGEX = /^[a-zA-Z0-9_-]{1,64}$/; // Allow only safe characters

app.use('/api', async (req, res, next) => {
  const token = req.headers.authorization?.split(' ')[1];
  if (!token) return res.status(401).json({ error: 'Missing token' });

  const header = jwt.decode(token, { complete: true })?.header;
  const kid = header?.kid;

  // FIXED: reject kid values that don't match the expected format
  if (!kid || !KID_REGEX.test(kid)) {
    return res.status(401).json({ error: 'Invalid key ID format' });
  }

  // FIXED: parameterized query — kid is passed as a bound parameter, not interpolated
  const result = await db.query(
    'SELECT public_key FROM signing_keys WHERE id = $1 AND active = true',
    [kid] // Parameterized — SQL injection impossible
  );

  if (!result.rows[0]) return res.status(401).json({ error: 'Unknown key' });

  try {
    const payload = jwt.verify(token, result.rows[0].public_key, {
      algorithms: ['RS256'],
      issuer: process.env.JWT_ISSUER,
    });
    req.user = payload;
    next();
  } catch (err) {
    res.status(401).json({ error: 'Unauthorized' });
  }
});
CRITICAL
SA-SKL-JWTALG-004 — MCP skill uses the JWT kid header field as a string-interpolated parameter in a database query to retrieve the signing key; since kid is extracted from the unverified token header, an attacker can inject SQL that retrieves a key they control, then sign a token with that key to bypass authentication entirely. Fix: validate kid with a strict regex (/^[a-zA-Z0-9_-]{1,64}$/) before using it in any query; use a parameterized query (db.query('... WHERE id = $1', [kid])); alternatively, use a local key map (Map<string, string>) instead of a database query. Detection: SkillAudit flags database queries inside JWT middleware that interpolate header.kid or decoded JWT header fields without prior regex validation.

SkillAudit detection summary — SA-SKL-JWTALG

Pattern IDSeverityWhat SkillAudit flagsFix
JWTALG-001CRITICALjwt.verify() with algorithms array containing both asymmetric (RS256/ES256) and symmetric (HS256/HS512) entriesSingle algorithm: algorithms: ['RS256'] — never mix asymmetric and symmetric
JWTALG-002HIGHjwt.verify() with algorithms array containing more than one elementSingle-element array: algorithms: [JWT_ALGORITHM] constant
JWTALG-003HIGHjwt.decode() called before jwt.verify() in the same request handler with claims used for routingCall jwt.verify() first; use claims only from verified return value
JWTALG-004CRITICALDatabase queries inside JWT middleware that interpolate header.kid without prior regex validationRegex validation of kid format; parameterized query or local key map

Related patterns: For the alg:none bypass, weak HMAC secret brute force, and jwt.decode() used instead of jwt.verify(), see SA-SKL-JWT. For SQL injection via other unvalidated request parameters in MCP skill database queries, see SA-SKL-SQLI.