Mitigation Strategy: Strong Secret and Explicit Algorithm Configuration
-
Description:
- Strong Secret: Use the
php artisan jwt:secretcommand to generate a cryptographically secure randomJWT_SECRET. Verify the generated key's length (at least 64 characters) and randomness. - Explicit Algorithm: In
config/jwt.php, explicitly set thealgokey to the desired signing algorithm (e.g.,'RS256'or'HS256'). Do not rely on the library's default.
- Strong Secret: Use the
-
Threats Mitigated:
- JWT Secret Key Compromise: (Severity: Critical) - A strong secret makes it computationally infeasible to forge signatures.
- Algorithm Confusion/Downgrade Attacks: (Severity: High) - Explicitly setting the algorithm prevents attackers from forcing the use of a weaker algorithm.
-
Impact:
- JWT Secret Key Compromise: Risk significantly reduced (though secure storage and rotation are still crucial).
- Algorithm Confusion/Downgrade Attacks: Risk significantly reduced.
-
Currently Implemented:
- Strong Secret Generation: Partially implemented -
php artisan jwt:secretis used, but key length/randomness verification is inApp\Providers\AppServiceProvider. - Explicit Algorithm Configuration: Implemented -
algois set inconfig/jwt.php.
- Strong Secret Generation: Partially implemented -
-
Missing Implementation:
- None, assuming the secret is strong and the algorithm is correctly configured.
Mitigation Strategy: Short-Lived Tokens and Refresh Token Usage
-
Description:
- Short
ttl: Inconfig/jwt.php, set thettl(time to live) to a short value (e.g., 15-60 minutes). This limits the lifespan of access tokens. - Refresh Token Implementation: Utilize
tymondesigns/jwt-auth's built-in refresh token functionality. This involves:- Using
JWTAuth::refresh()to obtain a new access token using a valid refresh token. - Configuring the refresh token's time-to-live (
refresh_ttlinconfig/jwt.php).
- Using
- Short
-
Threats Mitigated:
- Token Replay Attacks: (Severity: High) - Short
ttlreduces the window for replay. - Token Compromise: (Severity: High) - Limits the damage if an access token is stolen.
- Token Replay Attacks: (Severity: High) - Short
-
Impact:
- Token Replay Attacks: Risk significantly reduced.
- Token Compromise: Impact significantly reduced.
-
Currently Implemented:
- Short
ttl: Partially implemented -ttlis set, but could be shorter. - Refresh Token Implementation: Partially implemented - Refresh tokens are used, but not one-time use (one-time use is outside the direct scope of the library).
- Short
-
Missing Implementation:
ttlOptimization: Requires careful consideration of user experience and security trade-offs.
Mitigation Strategy: JWT ID (jti) Claim and Library's Blacklist (Cache-Based)
-
Description:
jtiClaim: Ensure that each issued JWT includes a uniquejti(JWT ID) claim.tymondesigns/jwt-authdoes this automatically.- Library's Blacklist (Cache): Utilize the library's built-in, cache-based blacklist. This is the default behavior. When you invalidate a token (e.g., on logout), use
JWTAuth::invalidate($token). This adds thejtito the cache. The library automatically checks the cache during token validation.
-
Threats Mitigated:
- Token Replay Attacks: (Severity: High) - Prevents reuse of invalidated tokens (within the cache's limitations).
- Token Compromise: (Severity: High) - Allows invalidation of compromised tokens (within the cache's limitations).
-
Impact:
- Token Replay Attacks: Risk reduced, but the cache-based blacklist has limitations (see below).
- Token Compromise: Impact reduced, but the cache-based blacklist has limitations.
-
Currently Implemented:
jtiClaim: Implemented - Automatic by the library.- Library's Blacklist (Cache): Likely implemented (default behavior), but needs verification.
-
Missing Implementation:
- Verification of Blacklist Usage: Ensure
JWTAuth::invalidate($token)is being called correctly on logout and other invalidation events. The limitations of the cache-based blacklist (not persistent across server restarts or multiple instances) are important to understand. A database-backed blacklist is better but outside the scope of this focused list.
- Verification of Blacklist Usage: Ensure
Mitigation Strategy: Secure Payload Handling (within Library Usage)
-
Description:
- Signature Verification: Always use
JWTAuth::parseToken()->authenticate()(or equivalent methods likeJWTAuth::attempt()) to validate the token's signature before accessing any data from the payload. This is the intended and correct way to use the library. - Avoid Sensitive Data: Do not store sensitive data directly in the JWT payload.
- Signature Verification: Always use
-
Threats Mitigated:
- Token Tampering (Payload Modification): (Severity: High) - Signature verification prevents unauthorized modification.
-
Impact:
- Token Tampering: Risk reduced to near zero with correct library usage.
-
Currently Implemented:
- Signature Verification: Implemented - Standard library usage.
- Avoid Sensitive Data: Partially Implemented - Review payload.
-
Missing Implementation:
- Payload Review: Requires a thorough review of the data currently included in the JWT payload.
Mitigation Strategy: Generic Error Messages (with Library Exceptions)
-
Description:
- Custom Exception Handling: Wrap calls to
JWTAuthmethods (e.g.,parseToken,authenticate,refresh) intry-catchblocks. Specifically, catch exceptions of typeTymon\JWTAuth\Exceptions\JWTExceptionand its subclasses (e.g.,TokenExpiredException,TokenInvalidException). - Generic Responses: In the
catchblock, do not return the specific exception message from the library to the client. Return a generic error message (e.g., "Unauthorized", "Invalid token") and an appropriate HTTP status code (e.g., 401).
- Custom Exception Handling: Wrap calls to
-
Threats Mitigated:
- Information Disclosure: (Severity: Medium) - Prevents attackers from gaining insights into the JWT validation process.
-
Impact:
- Information Disclosure: Risk significantly reduced.
-
Currently Implemented:
- Custom Exception Handling: Partially implemented - Some exception handling exists, but needs review for consistency and specific
JWTExceptiontypes. - Generic Responses: Partially implemented - Needs consistent application.
- Custom Exception Handling: Partially implemented - Some exception handling exists, but needs review for consistency and specific
-
Missing Implementation:
- Consistent Exception Handling and Generic Responses: Requires a review of all JWT-related code.
Mitigation Strategy: Standard Claims Validation (within Library Usage)
-
Description:
iat(Issued At) Claim:tymondesigns/jwt-authautomatically includes and validatesiat.exp(Expiration Time) Claim:tymondesigns/jwt-authautomatically includes and validatesexpbased on the configuredttl.nbf(Not Before) Claim: If you usenbf,tymondesigns/jwt-authwill validate it. You would set this when creating the token if needed.aud(Audience) Claim: While the library doesn't enforceaudvalidation, you can easily add it.- When creating the token, include the
audclaim:$token = JWTAuth::claims(['aud' => 'your-audience'])->attempt($credentials); - After retrieving the payload (but before full authentication), check the
audclaim:try { $payload = JWTAuth::getPayload($token); // Get payload *without* full validation if ($payload['aud'] !== 'your-audience') { // Handle invalid audience return response()->json(['error' => 'Invalid audience'], 401); } $user = JWTAuth::parseToken()->authenticate(); // Now do full validation } catch (JWTException $e) { // Handle other JWT exceptions return response()->json(['error' => 'Invalid token'], 401); }
- When creating the token, include the
-
Threats Mitigated:
- Token Misuse: (Severity: Medium) -
audprevents tokens intended for one application from being used in another. - Token Replay (with
iat,expandnbf): (Severity: Medium)
- Token Misuse: (Severity: Medium) -
-
Impact:
- Token Misuse: Risk significantly reduced if
audis properly implemented. - Token Replay: Risk slightly reduced.
- Token Misuse: Risk significantly reduced if
-
Currently Implemented:
iatClaim: Implemented (automatic).expClaim: Implemented (automatic).nbfClaim: Not implemented (but supported if used).audClaim: Not implemented.
-
Missing Implementation:
audClaim: Requires adding the claim when creating tokens and adding the validation check shown above.nbfClaim: Only if you need to issue tokens that are not valid until a future time.