Secure token handling

From RedHatProductSecurity/prodsec-skills · View interactive version

Secure token handling is an AI agent skill from the GitHub repository RedHatProductSecurity/prodsec-skills. Enforce secure token validation and storage in MCP servers. Use when implementing or reviewing token handling, JWT validation, or token storage in MCP servers.

How to use this skill

Skill file

Secure Token Handling for MCP Servers

Security Requirement

MCP servers MUST implement best practices for token validation and secure token storage to prevent token-based attacks.

Token Validation Requirements

Every incoming token MUST be validated for:

| Check | Details |

|---|---|

| Signature | Verify JWT signature against the IdP's JWKS; reject unsigned or weakly signed tokens |

| Expiry (exp) | Reject expired tokens; do not allow clock skew greater than 30 seconds |

| Audience (aud) | Must match the MCP server's audience identifier |

| Issuer (iss) | Must match the trusted IdP issuer URL |

| Not-before (nbf) | If present, reject tokens used before this time |

| Token ID (jti) | Check against revocation list if token revocation is supported |

Secure Token Storage

If the MCP server needs to store tokens (e.g., cached access tokens or refresh tokens for downstream services):

| Requirement | Details |

|---|---|

| Encrypt at rest | Tokens stored on disk or in databases must be encrypted |

| Memory-only when possible | Prefer in-memory token storage; avoid persisting to disk |

| Short-lived cache | Cache tokens only for the duration of their validity |

| No logging | Never log token values; log token metadata (jti, sub, exp) instead |

| Secure deletion | Clear tokens from memory when no longer needed |

Anti-Patterns

# WRONG - logging the token
logger.info(f"Received token: {token}")

# WRONG - storing token in plaintext file
with open("tokens.txt", "a") as f:
    f.write(token)

# CORRECT - log metadata only
logger.info(f"Token validated: sub={claims['sub']}, exp={claims['exp']}")

Implementation Checklist

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.