Authn sessions and tokens

From makifbaysal/tasktrooper · View interactive version

Authn sessions and tokens is an AI agent skill from the GitHub repository makifbaysal/tasktrooper. Use when the diff touches login, password storage, sessions, cookies, JWT verification, OAuth/OIDC or SAML flows, MFA and step-up, passkeys/WebAuthn, account linking and recovery, password reset, webhook signatures, API keys, mTLS identity or headers used to identify a caller

How to use this skill

Skill file

Authentication, Sessions and Tokens

Overview

Authentication bugs are rarely missing code; they are code that verifies the wrong thing, verifies with the wrong key, or trusts a value the attacker sends. Read every check as "what exactly is compared to what, and who chose each side?"

Core principle: the server decides the algorithm, the key, the audience and the identity — never the token, the header or the client.

1. Password Storage (CWE-916)

# ❌
user.password = hashlib.sha256(pw.encode()).hexdigest()
# ✅
user.password = argon2.PasswordHasher().hash(pw)   # or Django's make_password

2. JWT Verification (CWE-347)

// ❌ decode() does not verify; the header's alg is trusted
const claims = jwt.decode(token);
// ❌ no algorithm pinned: an RS256 public key can be used as an HS256 secret
const claims = jwt.verify(token, publicKey);
// ✅
const claims = jwt.verify(token, publicKey, { algorithms: ["RS256"], audience: "api", issuer: ISS });
# ❌
jwt.decode(token, options={"verify_signature": False})
# ✅
jwt.decode(token, key, algorithms=["RS256"], audience="api", issuer=ISS)

Also: alg: none accepted; exp/aud/iss not checked; the key chosen by a kid header used in a file path or SQL query; keys fetched from a token-supplied jku/x5u URL (SSRF and forged keys); a token accepted for a different audience than this service.

3. Sessions and Cookies

4. OAuth 2 and OIDC

5. Identity from Headers (CWE-290, CWE-348)

// ❌ any client sets this header
const userId = req.header("X-User-Id");
// ❌ Express trusts every hop's X-Forwarded-For for IP allowlists
app.set("trust proxy", true);

Headers like X-User-Id, X-Remote-User, X-Forwarded-For, X-Real-IP identify a caller only when an authenticating proxy sets them and strips client copies — confirm that in the repo's deployment config, not by assumption. X-Forwarded-Host/Host used to build a password-reset or magic-link URL sends the token to the attacker's domain:

# ❌ host header injection: the reset link points wherever the request's Host said
link = f"https://{request.headers['Host']}/reset?token={token}"
# ✅
link = f"{settings.PUBLIC_BASE_URL}/reset?token={token}"

6. Reset Tokens, API Keys and Webhooks

7. MFA and Step-Up

8. Passkeys and WebAuthn

Use the library's verify call and read what it is given: the challenge must be the one issued to this session and consumed once; the RP ID and expected origin must be fixed server-side values; at authentication, the credential must belong to the account being signed in, and the userHandle → account mapping must come from the server's own record. Require user verification when the policy says so. A signature counter matters only where the product treats a regression as a clone signal.

9. SAML

10. Account Linking and Recovery

Draw the account before and after: each new edge — an IdP, a passkey, an email, a phone, a device — needs a current authenticated session, proof of ownership of the new identity, the step-up the policy requires, and callback state bound to the account that started it.

11. API Keys, Service Credentials and mTLS

Severity Guide

Authentication bypass, forged tokens or SAML assertions accepted, or account takeover → CRITICAL. Reset-link host injection, unsigned state-changing webhooks, OAuth state/redirect_uri flaws, MFA enrollment without the existing factor, account linking without ownership proof, an API key reaching another tenant → HIGH. Fixation, logout that does not revoke → MEDIUM.

Common Mistakes

Red Flags

References (names and links only)

OWASP Top 10:2025 A07 Authentication Failures · ASVS 5.0 V6 Authentication, V7 Session Management, V9 Self-contained Tokens, V10 OAuth and OIDC · CWE-208, 290, 304, 347, 348, 384, 613, 640, 916 · W3C WebAuthn Level 3 · OWASP SAML Security Cheat Sheet · RFC 9700 OAuth 2.0 Security Best Current Practice

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.