# GHSA-PRRG-FP57-VCX4: OAuth 2.1 Temporary HA Credentials Keyed by client\_id Instead of Authorization Code Enables Cross-Session LLAT Swap / Race Condition

- Database: GitHub
- Severity: medium (stated by ghsa, no CVSS score)
- Published: 2026-10-24
- Modified: 2026-10-24

## Affected versions

- ha-mcp (PyPI): through 8.7.0
- ha-mcp-dev (PyPI): fixed in 9.0.0.dev3159

## Description

## Patches and workarounds

Fixed in homeassistant-ai/ha-mcp#2837: the token submitted on the consent form is bound to the authorization code it mints and consumed with it, and the consent POST enforces the transaction expiry.

- `ha-mcp` (stable, PyPI and the `ha-mcp` Docker image): no fixed release yet. The fix ships in the next stable release, 9.0.0.
- `ha-mcp-dev` (dev channel, PyPI and the dev Docker image): fixed in 9.0.0.dev3159.

Workaround for `ha-mcp` users running the `ha-mcp-oauth` entrypoint: switch to `ha-mcp-dev` 9.0.0.dev3159 or later, or wait for 9.0.0. The standard (`ha-mcp-web`), OIDC, app (add-on) and in-process component modes are not affected.

---

## Summary

In `ha-mcp-oauth` mode (`src/ha_mcp/auth/provider.py`), `HomeAssistantOAuthProvider` temporarily stores the user's submitted Home Assistant Long-Lived Access Token (`ha_token`) in `self.ha_credentials` between `POST /consent` (`_consent_post`) and `POST /token` (`exchange_authorization_code`).

However, `self.ha_credentials` is keyed by `client_id` (`self.ha_credentials[client_id]`) rather than by the one-time `auth_code_value` generated in `_consent_post` (`src/ha_mcp/auth/provider.py:171, 753, 830, 856`). Additionally, `_consent_post` does not validate the 300-second expiration window (`pending["created_at"]`) that `_consent_get` checks (`src/ha_mcp/auth/provider.py:678-686` vs `726-735`).

Because `ha_credentials` is bound to `client_id` instead of the specific `AuthorizationCode` (and its PKCE `code_challenge`), two concurrent or overlapping authorization flows sharing the same `client_id` overwrite `self.ha_credentials[client_id]`, causing `exchange_authorization_code()` to encode one session's `ha_token` into another session's OAuth token response.

---

## Affected Code (`src/ha_mcp/auth/provider.py`)

1. **Credential storage keyed by `client_id` in `_consent_post` (lines 752–778)**:
```python
        # Store credentials (no server-side validation - the token will be
        # validated on first actual API call to the configured HA instance)
        client_id = pending["client_id"]
        self.ha_credentials[client_id] = HomeAssistantCredentials(
            ha_token=str(ha_token),
        )
        logger.info(f"Stored HA credentials for client {client_id}")

        # Generate authorization code
        auth_code_value = f"ha_auth_code_{secrets.token_hex(16)}"
        ...
        self.auth_codes[auth_code_value] = auth_code
```

2. **Credential lookup and deletion by `client_id` in `exchange_authorization_code` (lines 829–856)**:
```python
        # Get HA credentials for this client to encode in token
        ha_credentials = self.ha_credentials.get(client.client_id)
        if not ha_credentials:
            raise TokenError(
                "invalid_client",
                f"No Home Assistant credentials found for client {client.client_id}",
            )
        ...
        access_token_value = self._encode_token(
            ha_credentials.ha_token,
            token_type="access",
            expires_at=access_token_expires_at,
        )
        ...
        # Clean up temporary credentials (no longer needed after token issued)
        self.ha_credentials.pop(client.client_id, None)
```

3. **Missing expiry check in `_consent_post` (lines 726–735)**:
Unlike `_consent_get` (which checks `if time.time() - pending["created_at"] > 300:`), `_consent_post` looks up `self.pending_authorizations.get(str(txn_id))` without checking `pending["created_at"]`.

---

## Reproduction & Impact Scenarios

### Scenario 1: Multi-User / Concurrent Consent Flows on a Shared OAuth Client
Per `SECURITY.md` (*"Per-user Home Assistant credentials exist only in OAuth mode"*), different users authenticating through the same registered OAuth client (same `client_id`) are intended to receive tokens bound to their own respective Home Assistant LLATs.

1. **Session A** initiates `GET /authorize` for `client_id` (with PKCE `challenge_A`) and submits `POST /consent` with `ha_token = "LLAT_USER_A"`.
   - Server sets `self.ha_credentials[client_id] = HomeAssistantCredentials("LLAT_USER_A")` and issues `code_A`.
2. Before `code_A` is exchanged at `POST /token` (or if Session A's redirect is slow/interrupted), **Session B** submits `POST /consent` for the same `client_id` (with PKCE `challenge_B`) and `ha_token = "LLAT_USER_B"`.
   - Server overwrites `self.ha_credentials[client_id] = HomeAssistantCredentials("LLAT_USER_B")` and issues `code_B`.
3. **Session A** completes `POST /token` presenting `code_A` and `verifier_A`.
   - `TokenHandler` verifies `code_A` against `challenge_A` and calls `exchange_authorization_code(client, code_A)`.
   - `exchange_authorization_code` reads `self.ha_credentials.get(client.client_id)` — which now holds `"LLAT_USER_B"` — and returns stateless `access_token` and `refresh_token` containing **`LLAT_USER_B`** to Session A.
   - `self.ha_credentials.pop(client.client_id, None)` removes the entry, so Session B's subsequent `POST /token` with `code_B` fails with `No Home Assistant credentials found for client ...`.

### Scenario 2: Unauthenticated Pre-Minted Authorization Code + Race Condition on Public Clients
For public PKCE clients (`token_endpoint_auth_method: "none"`, supported for connectors such as Claude.ai/ChatGPT):
1. Notice that `GET /authorize` redirects to `/consent?txn_id=...` on `ha-mcp` itself (not to `redirect_uri`), and `POST /consent` does not validate `ha_token` against Home Assistant before issuing `auth_code_value` and returning an HTTP `303` redirect (`Location: <redirect_uri>?code=ha_auth_code_...`).
2. Any party who knows a registered public `client_id` and its registered `redirect_uri` can call `GET /authorize` (with their own PKCE `code_challenge`) followed by `POST /consent` (with a dummy `ha_token`, without following redirects) and read a valid `code_attacker` directly from the `303` response's `Location` header — without needing to control `redirect_uri`.
3. Because `code_attacker` remains valid for 5 minutes (`AUTH_CODE_EXPIRY_SECONDS = 300`), if a victim subsequently submits `POST /consent` for the same `client_id` with their real `LLAT`, `self.ha_credentials[client_id]` is overwritten with the victim's `LLAT`. If the attacker redeems `code_attacker` (with their own `code_verifier`) at `POST /token` before the legitimate client redeems the victim's code (or if the victim's callback fails to complete), the attacker receives an `access_token` and `refresh_token` encoding the victim's `LLAT`.

---

## Severity Assessment

We assess this as **Medium** severity because:
- It only affects deployments explicitly running the beta `ha-mcp-oauth` entrypoint.
- Exploiting it maliciously requires knowing a registered public `client_id` (and its `redirect_uri`) and winning the race window between the victim's `POST /consent` and the legitimate client's `POST /token` call (or triggering a failed callback on the victim's redirect).

---

## Recommended Fix

1. **Bind `ha_credentials` to `auth_code_value` (or attach `ha_token` to `AuthorizationCode`) instead of `client_id`**:
   - In `_consent_post` (`src/ha_mcp/auth/provider.py:752-778`), generate `auth_code_value` first and store `self.ha_credentials[auth_code_value] = HomeAssistantCredentials(ha_token=str(ha_token))`.
   - In `exchange_authorization_code` (`src/ha_mcp/auth/provider.py:830-856`), retrieve and pop credentials by `authorization_code.code`: `ha_credentials = self.ha_credentials.pop(authorization_code.code, None)`.
   - In `load_authorization_code` (`src/ha_mcp/auth/provider.py:808-810`), also clean up `self.ha_credentials.pop(authorization_code, None)` when an expired code is evicted.
2. **Enforce `created_at` expiration in `_consent_post`**:
   - Mirror the `if time.time() - pending["created_at"] > 300:` check from `_consent_get` inside `_consent_post` before accepting the form submission.

## References

- https://github.com/homeassistant-ai/ha-mcp/security/advisories/GHSA-prrg-fp57-vcx4

Page: https://cve.now/GHSA-PRRG-FP57-VCX4

Wrong or missing data? Send feedback: POST https://cve.now/api/v1/feedback (see https://cve.now/agents#feedback).
