Mitigation Strategy: Strong Secret and Explicit Algorithm Configuration
-
Description:
- Strong Secret: Use the
php artisan jwt:secret
command 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 thealgo
key 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:secret
is used, but key length/randomness verification is inApp\Providers\AppServiceProvider
. - Explicit Algorithm Configuration: Implemented -
algo
is 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_ttl
inconfig/jwt.php
).
- Using
- Short
-
Threats Mitigated:
- Token Replay Attacks: (Severity: High) - Short
ttl
reduces 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 -ttl
is 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:
ttl
Optimization: Requires careful consideration of user experience and security trade-offs.
Mitigation Strategy: JWT ID (jti) Claim and Library's Blacklist (Cache-Based)
-
Description:
jti
Claim: Ensure that each issued JWT includes a uniquejti
(JWT ID) claim.tymondesigns/jwt-auth
does 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 thejti
to 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:
jti
Claim: 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
JWTAuth
methods (e.g.,parseToken
,authenticate
,refresh
) intry-catch
blocks. Specifically, catch exceptions of typeTymon\JWTAuth\Exceptions\JWTException
and its subclasses (e.g.,TokenExpiredException
,TokenInvalidException
). - Generic Responses: In the
catch
block, 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
JWTException
types. - 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-auth
automatically includes and validatesiat
.exp
(Expiration Time) Claim:tymondesigns/jwt-auth
automatically includes and validatesexp
based on the configuredttl
.nbf
(Not Before) Claim: If you usenbf
,tymondesigns/jwt-auth
will validate it. You would set this when creating the token if needed.aud
(Audience) Claim: While the library doesn't enforceaud
validation, you can easily add it.- When creating the token, include the
aud
claim:$token = JWTAuth::claims(['aud' => 'your-audience'])->attempt($credentials);
- After retrieving the payload (but before full authentication), check the
aud
claim: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) -
aud
prevents tokens intended for one application from being used in another. - Token Replay (with
iat
,exp
andnbf
): (Severity: Medium)
- Token Misuse: (Severity: Medium) -
-
Impact:
- Token Misuse: Risk significantly reduced if
aud
is properly implemented. - Token Replay: Risk slightly reduced.
- Token Misuse: Risk significantly reduced if
-
Currently Implemented:
iat
Claim: Implemented (automatic).exp
Claim: Implemented (automatic).nbf
Claim: Not implemented (but supported if used).aud
Claim: Not implemented.
-
Missing Implementation:
aud
Claim: Requires adding the claim when creating tokens and adding the validation check shown above.nbf
Claim: Only if you need to issue tokens that are not valid until a future time.