Skip to content

The state parameter is never validated #133

Closed as not planned
Closed as not planned

Description

Describe the bug

The Keycloak client uses the state OAuth 2.0 parameter, which is not mandatory but strongly recommended 👍.

I see in the client code that, when parsing the callback URL, it checks that the state parameter exists, but never checks if its value matches the one it generated at the beginning of the flow. I think this could lead to security vulnerability.

Could you please tell me if I'm right, and if so, fix this?

Thanks a lot!

Version

26.0.6

Expected behavior

If the client gets a response with a different state token than the one it initially generated, it should stop the authentication flow.

Actual behavior

If the client gets a response with a different state token than the one it initially generated, it continues with the authentication flow.

How to Reproduce?

Edit the state parameter that is returned by the server.

Anything else?

No response

Activity

  1. added theissue type on Jul 2, 2025
  2. jonkoops commented on Jul 8, 2025

    @jonkoops
    Contributor

    This parameter is actually validated, although the code could be a bit clearer on the matter. The state parameter is used as a key for state stored locally in the browser, if the stored state cannot be found then the callback never becomes valid when parsed:

    keycloak-js/lib/keycloak.js

    Lines 1020 to 1029 in bb0ad94

    var oauthState = this.#callbackStorage.get(oauth.state);
    if (oauthState) {
    oauth.valid = true;
    oauth.redirectUri = oauthState.redirectUri;
    oauth.storedNonce = oauthState.nonce;
    oauth.prompt = oauthState.prompt;
    oauth.pkceCodeVerifier = oauthState.pkceCodeVerifier;
    oauth.loginOptions = oauthState.loginOptions;
    }

    This means that later on, the callback will be determined invalid, and is never processed:

    keycloak-js/lib/keycloak.js

    Lines 811 to 815 in bb0ad94

    if (callback && callback.valid) {
    await this.#setupCheckLoginIframe();
    await this.#processCallback(callback);
    return;
    }

    This basically means that Keycloak JS will consider this a whole new flow, and it will do all the checks from scratch again. This is working as intended, and is secure behavior.

  3. jonkoops commented on Jul 8, 2025

    @jonkoops
    Contributor

    No problem, thanks for reporting this, I had to do some digging as well to understand if we were doing the right thing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    No fields configured for bug.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions