SQL Injection Prevention: Escaping vs Parameterization
Learn the critical differences between escaping and parameterized queries for SQL injection prevention. Understand why parameterization is the gold standard for database security.
Learn how JWT claims work, explore registered, public, and private claims, and discover security best practices for implementing JSON Web Tokens in your applications.
JSON Web Tokens (JWTs) are a compact, URL-safe means of representing claims between two parties. Claims are statements about an entity (typically the user) and additional metadata. They form the payload of a JWT and contain the actual information being transmitted.
Understanding JWT claims is essential for implementing secure authentication and authorization in modern web applications. This guide covers everything from basic claim types to advanced security practices.
Before diving into claims, it helps to understand the JWT structure. A JWT consists of three parts separated by dots: header, payload, and signature.
Registered claims are predefined claims recommended by the JWT specification. They are not mandatory but provide a standardized set of useful, interoperable claims.
Public claims are custom claims that can be defined by anyone using JWTs. To avoid collisions, they should be defined in the IANA JSON Web Token Registry or use collision-resistant names like URIs.
Private claims are custom claims created for sharing information between parties that agree on their use. They are neither registered nor public claims.
Time-based claims are critical for token security. They control when tokens become valid and when they expire.
Here are typical claim configurations for different use cases.
Proper claim handling is essential for JWT security. Follow these practices to protect your applications.
The algorithm you choose affects both security and architecture. Understanding the options helps you make the right choice.
Choosing the right expiration time involves balancing security and user experience.
JWTs are stateless by design, making revocation challenging. Here are common approaches.
Always validate claims server-side. Never trust claims without verification.
Avoid these frequently seen JWT implementation errors.
Where you store JWTs affects security. Choose based on your application type.
When JWTs fail validation, systematic debugging helps identify the problem.
Registered claims are predefined by the JWT specification (like exp, iss, sub) and have standard meanings. Private claims are custom claims you define for your application (like user_id or role). Registered claims ensure interoperability, while private claims let you include application-specific data.
JWTs are signed, not encrypted by default. Anyone can decode and read the payload. If you need to hide claim values, use JWE (JSON Web Encryption) or avoid putting sensitive data in claims entirely. For most applications, signing alone is sufficient when combined with HTTPS.
Access tokens should be short-lived, typically 15-60 minutes. Refresh tokens can be longer (days to weeks) but require secure storage and revocation capability. Shorter expiration times limit the damage if a token is compromised.
A stolen JWT can be used by an attacker until it expires. This is why short expiration times are important. Implement token revocation through blacklists or refresh token rotation. Also use secure storage (HttpOnly cookies) to prevent theft through XSS.
No. JWTs are immutable once signed. Any modification to the header or payload invalidates the signature. To change claims, you must issue a new token with the updated values.
The sub (subject) claim is the registered claim for identifying the principal. Using it ensures interoperability with libraries and services that expect standard claims. You can still use custom claims like user_id alongside sub if needed.
Use refresh tokens to obtain new access tokens before expiration. Implement token refresh logic that detects near-expiration and refreshes proactively. Handle 401 responses by attempting refresh before prompting for re-login.
There is no hard limit in the specification, but JWTs are sent with every request (often in headers). Keep the total JWT size under 8KB to avoid issues with header size limits in servers and proxies. Minimize claims to essential data only.
Learn the critical differences between escaping and parameterized queries for SQL injection prevention. Understand why parameterization is the gold standard for database security.
Decode JWT headers and payloads, inspect claims, and verify expiration quickly.
Create hashes for checksums, file validation, and data integrity in seconds.