Skip to content

SAML2 Exception when clicking browser's back button after successful login #2689

Open

Description

@DSK-IFJ

We're having a bit of an unexcpected behaviour with SimpleSAMLphp running as SP. After successful login, if the user clicks on the back button in the browser, saml2-acs.php throws an unhandled exception:

Unsolicited Response MUST NOT contain InResponseTo. Got: SOME_ID

As far as I can tell, the issue lies with how SimpleSAMLphp handles states. Before user is redirected to IdP's login page, a state is created by SimpleSAMLphp. ID of that state is sent in POST to the IdP during authentication process. It then deletes that state, after saving all the necessary data to the store defined in the config (in our case - Memcache). It also saves the Response ID to a prevAuth array, so it can be identified if, for whatever reason, it is resent.

When user presses back button in their browser, they resend the authentication request. I assume because IdP has no way of knowing that the old state doesn't exist in SimpleSAMLphp anymore, it treats the request as a new one, and assigns a new Response ID. This means, the mechanism in assertionConsumerService function, which is supposed to catch such an event, doesn't trigger ($prevAuth['ID'] !== $response->getId()), and response is processesed as Unsolicited. However, because it's not really Unsolicited, it has an InResponseTo value set, causing the exception to be thrown.

Specifics of your environment

  1. Are you acting as SP/IdP/proxy?
  • SimpleSAMLphp acts as SP
  1. SimpleSAMLphp: What version are you using?
  • 2.4.10
  1. PHP: What version are you using?
  • 8.2
  1. Platform: unix or Windows?
  • Linux (Debian 12)
  1. Webserver: Apache/Nginx/ISS?
  • Apache2 2.4.68

Describe the bug
Pressing "Back" button in the browser right after logging in throws an unhandled exception:
Unsolicited Response MUST NOT contain InResponseTo. Got: SOME_ID

To Reproduce
Steps to reproduce the behavior:

  1. Log into your remote IdP
  2. Click on "Back" button in the browser
  3. See error

Expected behavior
User is moved to a page before triggering the IdP logging behaviour, or, if impossible, redirected back on the same page they clicked "Back" button from.

Screenshots or logs
Start:

Oct 14 08:26:24 simplesamlphp DEBUG [2830826d0a] Session: 'IdP_Name' not valid because we are not authenticated.
Oct 14 08:26:24 simplesamlphp DEBUG [2830826d0a] saving key .session.SESSION-KEY-ID to memcache

IdP Login page:

Oct 14 08:28:40 simplesamlphp DEBUG [2830826d0a] loading key .session.SESSION-KEY-ID from memcache
Oct 14 08:28:40 simplesamlphp DEBUG [2830826d0a] Saved state: '_STATE-ID' // _acfc0415b18578645cb631d7818df892192fab06a7
Oct 14 08:28:40 simplesamlphp DEBUG [2830826d0a] Sending SAML 2 AuthnRequest to 'https://My-Keycloak-Instance.com/realms/My-Realm
Oct 14 08:28:40 simplesamlphp DEBUG [2830826d0a] Redirect to 1252 byte URL: https://My-Keycloak-Instance.com/realms/My-Realm/protocol/saml?(...)
Oct 14 08:28:40 simplesamlphp DEBUG [2830826d0a] saving key .session.SESSION-KEY-ID to memcache

IdP Post-Login Redirect:

Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] loading key .session.SESSION-KEY-ID from memcache
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] Loading state: '_STATE-ID'
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] Received SAML2 Response from 'https://My-Keycloak-Instance.com/realms/My-Realm'.
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] Has 1 candidate keys for validation.
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] Validation with key #0 succeeded.
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] Decryption with key #0 succeeded.
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] Has 1 candidate keys for validation.
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] Validation with key #0 failed without exception.
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] loading key .saml.AssertionReceived.ID_ASSERTION-ID from memcache
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] key .saml.AssertionReceived.ID_ASSERTION-ID not found in memcache
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] saving key .saml.AssertionReceived.ID_ASSERTION-ID to memcache
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] saving key .saml.LogoutStore.LOGOUTSTORE-ID to memcache
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] Filter config for https://My-Keycloak-Instance.com/realms/My-Realm->My-Realm>: Array(    [0] => SimpleSAML\Module\core\Auth\Process\LanguageAdaptor Object        (            [priority] => 90            [precondition] => return true;            [langattr:SimpleSAML\Module\core\Auth\Process\LanguageAdaptor:private] => preferredLanguage        ))
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] Deleting state: '_STATE-ID'
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] Session: doLogin("My-IDP")
Oct 14 08:31:41 simplesamlphp DEBUG [2830826d0a] saving key .session.SESSION-KEY-ID to memcache
Oct 14 08:31:42 simplesamlphp DEBUG [2830826d0a] loading key .session.SESSION-KEY-ID from memcache
Oct 14 08:31:42 simplesamlphp DEBUG [2830826d0a] Localization: load domain 'messages' at '/var/simplesamlphp/locales'
Oct 14 08:31:42 simplesamlphp DEBUG [2830826d0a] Trying langpath for 'pl' as '/var/simplesamlphp/locales/pl/LC_MESSAGES/'
Oct 14 08:31:42 simplesamlphp DEBUG [2830826d0a] Localization: load domain 'core' at '/var/simplesamlphp/modules/core/locales'
Oct 14 08:31:42 simplesamlphp DEBUG [2830826d0a] Trying langpath for 'pl' as '/var/simplesamlphp/modules/core/locales/pl/LC_MESSAGES/'
Oct 14 08:31:42 simplesamlphp DEBUG [2830826d0a] Localization: load domain 'attributes' at '/var/simplesamlphp/locales'
Oct 14 08:31:42 simplesamlphp DEBUG [2830826d0a] Trying langpath for 'pl' as '/var/simplesamlphp/locales/pl/LC_MESSAGES/'
Oct 14 08:31:42 simplesamlphp DEBUG [2830826d0a] saving key .session.SESSION-KEY-ID to memcache
Oct 14 08:31:42 simplesamlphp DEBUG [2830826d0a] loading key .session.SESSION-KEY-ID from memcache
Oct 14 08:31:42 simplesamlphp DEBUG [2830826d0a] Session: Valid session found with 'My-IDP'.
Oct 14 08:31:42 simplesamlphp DEBUG [2830826d0a] Session: Valid session found with 'My-IDP'.

Back Button clicked:

Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] loading key .session.SESSION-KEY-ID from memcache
Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] Loading state: '_STATE-ID'
Oct 14 08:37:27 simplesamlphp WARNING [2830826d0a] Could not load state specified by InResponseTo: NOSTATE Processing response as unsolicited.
Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] Received SAML2 Response from 'https://My-Keycloak-Instance.com/realms/My-Realm'.
Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] Has 1 candidate keys for validation.
Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] Validation with key #0 succeeded.
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] SimpleSAML\Error\Error: UNHANDLEDEXCEPTION
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] Backtrace:
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] 2 /var/simplesamlphp/src/SimpleSAML/Error/ExceptionHandler.php:36 (SimpleSAML\Error\ExceptionHandler::customExceptionHandler)
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] 1 /var/simplesamlphp/vendor/symfony/error-handler/ErrorHandler.php:538 (Symfony\Component\ErrorHandler\ErrorHandler::handleException)
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] 0 [builtin] (N/A)
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] Caused by: SimpleSAML\Error\Exception: Unsolicited Response MUST NOT contain InResponseTo. Got: '_acfc0415b18578645cb631d7818df892192fab06a7'
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] Backtrace:
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] 6 /var/simplesamlphp/modules/saml/src/Message.php:682 (SimpleSAML\Module\saml\Message::processResponse)
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] 5 /var/simplesamlphp/modules/saml/src/Controller/ServiceProvider.php:417 (SimpleSAML\Module\saml\Controller\ServiceProvider::assertionConsumerService)
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] 4 /var/simplesamlphp/vendor/symfony/http-kernel/HttpKernel.php:181 (Symfony\Component\HttpKernel\HttpKernel::handleRaw)
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] 3 /var/simplesamlphp/vendor/symfony/http-kernel/HttpKernel.php:76 (Symfony\Component\HttpKernel\HttpKernel::handle)
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] 2 /var/simplesamlphp/vendor/symfony/http-kernel/Kernel.php:208 (Symfony\Component\HttpKernel\Kernel::handle)
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] 1 /var/simplesamlphp/src/SimpleSAML/Module.php:234 (SimpleSAML\Module::process)
Oct 14 08:37:27 simplesamlphp ERROR [2830826d0a] 0 /var/simplesamlphp/public/module.php:17 (N/A)
Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] Session: Valid session found with 'My-IDP'.
Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] Localization: load domain 'messages' at '/var/simplesamlphp/locales'
Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] Trying langpath for 'pl' as '/var/simplesamlphp/locales/pl/LC_MESSAGES/'
Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] Localization: load domain 'core' at '/var/simplesamlphp/modules/core/locales'
Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] Trying langpath for 'pl' as '/var/simplesamlphp/modules/core/locales/pl/LC_MESSAGES/'
Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] Localization: load domain 'attributes' at '/var/simplesamlphp/locales'
Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] Trying langpath for 'pl' as '/var/simplesamlphp/locales/pl/LC_MESSAGES/'
Oct 14 08:37:27 simplesamlphp DEBUG [2830826d0a] saving key .session.SESSION-KEY-ID to memcache

Additional context
IdP: Keycloak 26.8.0
SimpleSAMLphp Store Type: Memcache

Activity

  1. tbone-tech commented on Oct 23, 2026

    @tbone-tech

    We are experiencing this same bug also, after uprading simple saml 2.5.3.1 the latest version, we experience the issue with multiple IDP's it. When I tested I did with entra, I logged in successfull then once I hit our app I hit back button and went back to microsoft page it had a error, but then if I hit back once again it would go to simple saml page and give the same error as explained above. Shows in browser but also will show in my simple saml logs .

  2. tbone-tech commented on Nov 2, 2026

    @tbone-tech

    I had our developer create a workaround for this is someone hits back button its corrects its self, we made change in the ServiceProvider.php

  3. DSK-IFJ commented on Nov 25, 2026

    @DSK-IFJ
    Author

    I had our developer create a workaround for this is someone hits back button its corrects its self, we made change in the ServiceProvider.php

    @tbone-tech Can you share the workaround you ended up using?

  4. tvdijen commented on Nov 25, 2026

    @tvdijen
    Member

    That would be very helpful indeed! Also, perhaps you could persuade your developer into opening a pull request?

  5. tbone-tech commented on Nov 25, 2026

    @tbone-tech

    ServiceProvider.php here is the file I have them modify. THis should be the extra code they added to fix this .

    +                // LOCAL WORKAROUND: discard a response to this session's completed request.
    +                // The incoming response is NOT accepted as authentication evidence.
    +                // Recover only from missing state, using an existing valid authentication
    +                // session and its saved redirect; never use the incoming RelayState here.
    +                if ($e instanceof Error\NoState) {
    +                    // LOCAL DIAGNOSTIC: record why the recovery below did or did not apply.
    +                    // Cookie names only, never values.
    +                    Logger::warning(sprintf(
    +                        'NoState diagnostics: InResponseTo=%s ResponseID=%s sessionValid=%s '
    +                        . 'prevAuthInResponseTo=%s prevAuthIssuerMatch=%s prevAuthHasRedirect=%s cookies=[%s]',
    +                        var_export($stateId, true),
    +                        var_export($response->getId(), true),
    +                        var_export($this->session->isValid($sourceId), true),
    +                        var_export(is_array($prevAuth) ? ($prevAuth['inResponseTo'] ?? null) : null, true),
    +                        var_export(is_array($prevAuth) && ($prevAuth['issuer'] ?? null) === $issuer, true),
    +                        var_export(is_array($prevAuth) && isset($prevAuth['redirect']), true),
    +                        implode(',', array_keys($_COOKIE)),
    +                    ));
    +                }
    +
    +                if (
    +                    $e instanceof Error\NoState
    +                    && $this->session->isValid($sourceId)
    +                    && is_array($prevAuth)
    +                    && isset($prevAuth['inResponseTo'], $prevAuth['issuer'], $prevAuth['redirect'])
    +                    && is_string($prevAuth['inResponseTo'])
    +                    && $prevAuth['inResponseTo'] !== ''
    +                    && $prevAuth['inResponseTo'] === $stateId
    +                    && $prevAuth['issuer'] === $issuer
    +                    && is_string($prevAuth['redirect'])
    +                    && $prevAuth['redirect'] !== ''
    +                ) {
    +                    // A disallowed redirect must not turn this into an error page;
    +                    // fall through to the default handling instead.
    +                    try {
    +                        $recoveryURL = $httpUtils->checkURLAllowed($prevAuth['redirect']);
    +                    } catch (Exception $urlException) {
    +                        Logger::warning(sprintf(
    +                            'Saved redirect not allowed for recovery: %s Falling back to default handling.',
    +                            $urlException->getMessage(),
    +                        ));
    +                        $recoveryURL = null;
    +                    }
    +                    if ($recoveryURL !== null && $recoveryURL !== $httpUtils->getSelfURLNoQuery()) {
    +                        Logger::notice(
    +                            'Discarding response to a completed authentication request; '
    +                            . 'resuming the existing authenticated session.',
    +                        );
    +
    +                        return new RunnableResponse(
    +                            [$httpUtils, 'redirectTrustedURL'],
    +                            [$recoveryURL],
    +                        );
    +                    }
    +                }
    
  6. tvdijen commented on Nov 25, 2026

    @tvdijen
    Member

    Thanks! This could be useful when doing some debugging.. It's probably not a patch that should ever see production

  7. tbone-tech commented on Nov 25, 2026

    @tbone-tech

    well we have that for now , as we had no other fix, hopefully you can come up with something that would work better

  8. DSK-IFJ commented on Dec 3, 2026

    @DSK-IFJ
    Author

    Unfortunately the temporary fix doesn't work for us on 2.4.10 . Now we're getting
    Exception: The POST data we should restore was lost.
    from the /var/simplesamlphp/modules/core/src/Controller/Redirection.php
    I dug a bit there and it seems that, with Keycloak, we already "use up" our one opportuninty to restore POST data by triggering the postredirect function after login. Data is deleted first time it is accessed with this function, so we cannot retrieve it again.

    $session = $this->session;
    $postData = $session->getData('core_postdatalink', $postId);
    $session->deleteData('core_postdatalink', $postId);

    Commenting out the deleteData line fixes it, but, correct me if I'm wrong, I believe it would compromize security even further, so I'm hesitant to apply such a "fix" in prod.

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

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions