Asymmetric Digital Signatures (RS256/ES256) vs Shared Secrets (HS256) & Algorithm Confusion
HS256 uses a symmetric shared secret where every party that verifies a token can also forge one. RS256/ES256 use asymmetric cryptography where only the authorization server holds the private key to sign, while resource servers use the public key to verify. Defenses must prevent Algorithm Confusion (substituting public keys as HMAC secrets) and 'alg: none' attacks.
Step 1: Auth server generates an RSA-2048 or ECDSA P-256 key pair.
In a microservice ecosystem of 30 services, distributing a shared symmetric secret (HS256) means any compromised service can forge tokens for the entire company. Asymmetric keys decouple signing from verification. However, naive validation code can be tricked into verifying RS256 tokens as HS256 using the public key as the secret.
The Royal Signet Ring vs The Shared Passphrase: With HS256, all 100 border guards know the royal secret passphrase; any rogue guard can whisper it to forge entrance passes. With RS256, only the King holds the golden signet ring (private key), while every guard carries an open plaster imprint (public key) to check seals.
[ Attacker Payload: alg: "HS256" ]
│
▼ (Attacker signs token with IdP's PUBLIC RSA KEY as HMAC secret!)
[ Vulnerable Resource Server ]
│
├── Reads header: alg = "HS256"
├── Naive code: secret = getPublicKey()
├── Verifies HMAC(secret, Header.Payload) ──► FORGERY ACCEPTED! (CRITICAL BREACH)
│
[ Hardened CosmosApp Resource Server ]
│
├── Enforces STRICT whitelist: algorithms = ['RS256']
├── Header says "HS256" ──► REJECT IMMEDIATELY (Algorithm Mismatch!)CosmosApp migrates its microservices to RS256. To defend against the famous 'alg: none' and Algorithm Confusion attacks, CosmosApp hardcodes an algorithm whitelist on every API gateway. If an attacker submits a token signed with HS256 using CosmosApp's public certificate as an HMAC key, the gateway rejects the token before executing any verification math.
Step 1: Auth server generates an RSA-2048 or ECDSA P-256 key pair.
Step 2: Private key is stored strictly inside AWS KMS or HashiCorp Vault; public key is published on JWKS.
Step 3: Auth server signs JWTs using the private key, setting header alg='RS256' and kid='key_1'.
Step 4: Resource server receives token and checks that alg exactly matches the expected algorithm in its whitelist.
Step 5: Resource server fetches the public key corresponding to kid and verifies the signature.
Step 6: If alg does not match the whitelist or signature verification fails, returns HTTP 401.
import jwt from 'jsonwebtoken';
// CosmosApp RS256 Token Verification (Immunized against Algorithm Confusion):
export function verifyRs256Token(token: string, rsaPublicKey: string) {
return jwt.verify(token, rsaPublicKey, {
// CRITICAL: Force RS256! Never allow dynamic alg selection from header!
algorithms: ['RS256'],
issuer: 'https://auth.cosmos.app',
audience: 'https://api.cosmos.app',
});
}- ✕Trusting the 'alg' header in the token to decide which verification algorithm to use (The token's header is controlled by the untrusted caller; the server must dictate allowed algorithms).
- ✕HS256 is obsolete for all use cases (HS256 is appropriate for single monolith architectures where only one server ever creates and verifies tokens).
Key Architectural Takeaways
- ✓Always use asymmetric algorithms (RS256, ES256) for multi-service or third-party token verification.
- ✓Never allow the token's header 'alg' parameter to dictate the cryptographic algorithm used by the server.
- ✓Explicitly reject tokens specifying 'alg: none' or unexpected symmetric algorithms.
Accepting whatever algorithm is specified in the JWT header without an explicit whitelist, enabling Algorithm Confusion attacks.
Always pass an explicit algorithms: ['RS256'] option to the verification function.
How does an Algorithm Confusion attack exploit an asymmetric JWT implementation?