Skip to main content
Menu
Advisories

Improper Session Invalidation - JWT Access Tokens Survive Logout and Password Change

GHSA-M8G9-WXHX-6F86Published Updated GitHub

Description

JWT access tokens issued by the API plugin lack a jti (JWT ID) claim. Refresh tokens include a jti and are revocable via a server-side revocation list stored at cache://api/revoked_tokens.json. Access tokens have no equivalent mechanism - once issued, they remain valid for their full lifetime (default 1 hour) regardless of logout, password change, account state change, or issuance of new tokens. A stolen access token cannot be invalidated; the victim can log out, change their password, or even have their account disabled, and the stolen token continues to grant full API access until it naturally expires.

---

Details

Vulnerable code - user/plugins/api/classes/Api/Auth/JwtAuthenticator.php:46-61:

public function generateAccessToken(UserInterface $user): string
{
    $secret = $this->getSecret();
    $algorithm = $this->config->get('plugins.api.auth.jwt_algorithm', 'HS256');
    $expiry = (int) $this->config->get('plugins.api.auth.jwt_expiry', 3600);

    $payload = [
        'iss' => 'grav-api',
        'sub' => $user->username,
        'iat' => time(),
        'exp' => time() + $expiry,
        'type' => 'access',
        //  NO 'jti' claim - token cannot be revoked
    ];

    return JWT::encode($payload, $secret, $algorithm);
}

Compare with refresh tokens (lines 66-82) which DO include jti:

$payload = [
    'iss' => 'grav-api',
    'sub' => $user->username,
    'iat' => time(),
    'exp' => time() + $expiry,
    'type' => 'refresh',
    'jti' => bin2hex(random_bytes(16)),  // ← revocable!
];

The revoke flow (lines 170-188) only works with tokens that have a JTI:

public function revokeToken(string $token): bool
{
    $decoded = JWT::decode($token, new Key($secret, $algorithm));
    $jti = $decoded->jti ?? null;
    if (!$jti) {
        return false;  // ← access tokens always hit this path
    }
    $this->addRevokedToken($jti, $decoded->exp ?? time() + 604800);
    return true;
}

The validate flow (lines 250-271) never checks revocation for access tokens because there's nothing to check:

protected function validateToken(string $token): ?UserInterface
{
    $decoded = JWT::decode($token, new Key($secret, $algorithm));
    
    if (($decoded->type ?? null) !== 'access') {
        return null;
    }
    
    // No revocation check — no jti to look up
    
    $user = $accounts->load($decoded->sub);
    return $user->exists() ? $user : null;
}

---

Proof of Concept

Step 1 - Login and obtain token

TOKEN1=$(curl -s http://127.0.0.1/grav/api/v1/auth/token \
  -X POST -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"password"}' \
  | python3 -c "import json,sys; print(json.load(sys.stdin)['data']['access_token'])")

# Verify token works
curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1/grav/api/v1/pages \
  -H "Authorization: Bearer $TOKEN1"
# Output: 200  ← token is valid

Step 2 - Logout (revoke refresh token only)

REFRESH=$(curl -s http://127.0.0.1/grav/api/v1/auth/token \
  -X POST -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"password"}' \
  | python3 -c "import json,sys; print(json.load(sys.stdin)['data']['refresh_token'])")

curl -s http://127.0.0.1/grav/api/v1/auth/revoke -X POST \
  -H "Authorization: Bearer $TOKEN1" -H "Content-Type: application/json" \
  -d "{\"refresh_token\":\"$REFRESH\"}"

Step 3 - Old access token STILL works after logout

curl -s -o /dev/null -w "%{http_code}" http://TARGET/grav/api/v1/pages \
  -H "Authorization: Bearer $TOKEN1"
# Output: 200  ← token STILL valid after logout!

Step 4 - Login again, old token STILL works

TOKEN2=$(curl -s http://127.0.0.1/grav/api/v1/auth/token \
  -X POST -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"password"}' \
  | python3 -c "import json,sys; print(json.load(sys.stdin)['data']['access_token'])")

# Both tokens are valid simultaneously:
curl -s -o /dev/null -w "Token1: %{http_code}" http://127.0.0.1/grav/api/v1/pages \
  -H "Authorization: Bearer $TOKEN1"
echo " (old, should be dead)"

curl -s -o /dev/null -w "Token2: %{http_code}" http://127.0.0.1/grav/api/v1/pages \
  -H "Authorization: Bearer $TOKEN2"
echo " (new)"

Step 5 - Password change doesn't invalidate old tokens

curl -s http://127.0.0.1/grav/api/v1/users/admin -X PATCH \
  -H "Authorization: Bearer $TOKEN1" -H "Content-Type: application/json" \
  -d '{"password":"NewPass@123"}'

# Old token still works after password change:
curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1/grav/api/v1/pages \
  -H "Authorization: Bearer $TOKEN1"
# Output: 200  ← should be 401

<img width="1853" height="1011" alt="image" src="github.com/user-attachments/assets/eba4c36a-fcbc-4ab8-a909-bf6b852c5dbc" />

---

Impact

A JWT access token, once issued, cannot be revoked by any server-side action. This creates the following attack scenarios:

ScenarioImpact
Token stolen via XSSAttacker has 1-hour window of unrestricted API access; logout/password change does nothing
Token leaked via logs?token= in URL (download/thumbnail routes) exposes token in server logs; cannot be revoked
Account compromisedEven after password reset, the attacker's existing access token remains valid for 1 hour
Insider threatA revoked employee's tokens survive for 1 hour after account disable/removal
Token accumulationEach login creates a new valid token; old tokens pile up with no server-side cleanup

Unlike refresh tokens (which have jti and can be revoked), access tokens are bearer instruments with no kill switch.

---

Remediation

Option A - Add JTI to access tokens (preferred)

Add a jti claim to access tokens and check it against the revocation list in validateToken():

public function generateAccessToken(UserInterface $user): string
{
    $payload = [
        'iss' => 'grav-api',
        'sub' => $user->username,
        'iat' => time(),
        'exp' => time() + $expiry,
        'type' => 'access',
        'jti' => bin2hex(random_bytes(16)),  // ← ADD
    ];
    return JWT::encode($payload, $secret, $algorithm);
}

protected function validateToken(string $token): ?UserInterface
{
    $decoded = JWT::decode($token, new Key($secret, $algorithm));
    
    if (($decoded->type ?? null) !== 'access') {
        return null;
    }

    // Check revocation list
    if ($this->isTokenRevoked($decoded->jti ?? '')) {
        return null;
    }

    $user = $accounts->load($decoded->sub);
    return $user->exists() ? $user : null;
}

This also enables logout to properly revoke access tokens. On logout, iterate the user's active tokens and revoke each one.

Option B - Reduce token lifetime

Lower the default jwt_expiry from 3600 (1 hour) to a shorter window (e.g., 900 seconds / 15 minutes). This doesn't solve the problem but limits the blast radius of a leaked token.

Option C - Bind tokens to session state

Store a token version counter per user. On password change or logout, increment the counter. Validate that the token's version matches the current counter.

---

Affected versions

References

GitHub · getgrav/grav