Skip to content

fix: verify an auth module session with the module that established it - #41967

Open
oc-tmueller wants to merge 3 commits into
masterfrom
fix/verify-auth-headers-session-module
Open

oc-tmueller wants to merge 3 commits into
masterfrom
fix/verify-auth-headers-session-module

Conversation

@oc-tmueller

Copy link
Copy Markdown
Contributor

Description

Session::verifyAuthHeaders() hands the credential of an established session to every auth module on each request. A module that doesn't know the credential may check it remotely. openidconnect introspects every opaque bearer token at the identity provider. So a client that keeps its session cookie and authenticates with a local OAuth2 token (the desktop and mobile clients) cost two failing IdP round trips per OCS and web request: an introspection that answers active: false and a discovery fetch. The OAuth2 module authenticated the request anyway.

The DAV auth backends already avoid this. Once a session is DAV-authenticated, each backend re-checks the token only with its own module. This PR does the same in core for all requests:

  • tryAuthModuleLogin() records the module that established the session, together with the user.
  • verifyAuthHeaders() asks that module right after the built-in TokenAuthModule. Once it confirms the session user, the other app modules are skipped. The built-in TokenAuthModule and BasicAuthModule are local and still have to agree.
  • In every other case the module loop runs exactly as before: nothing recorded, another user, null, or an exception. The recorded module is also ignored once the session user has changed, and every login clears it.

No app has to know about another app, and openidconnect/oauth2 need no change.

Motivation and Context

A customer reported a large rise in requests to their SSO after enabling openidconnect next to oauth2 on 11.0.1. See also owncloud/openidconnect#338 and the analysis in owncloud/openidconnect#377.

How Has This Been Tested?

  • Bench: owncloud/server:11.0.1 (oauth2 0.7.2, openidconnect 2.4.2) with Keycloak 26 and redis, replaying a client: DAV, three OCS requests, a web request, DAV, all with the session cookie and bearer token. IdP requests (discovery + introspection): 8 → 0.
  • Identity checks on the same bench, with stock and patched core behaving the same:
    • the session cookie of one user plus another user's OAuth2 token switches to the other user
    • a random token on a session still goes to the IdP
    • an expired token still ends the session
  • Unit: tests/lib/User 284/284 on PHP 8.3. New tests for each guard: built-in modules still asked, token auth still first, module asked only once on fallback, uid binding, the key cleared on login and recorded on module login. Each was mutation-checked: removing the guard fails exactly its test.
  • php-cs-fixer clean.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • Code changes
  • Unit tests added
  • Changelog item

🤖 Generated with Claude Code

oc-tmueller and others added 3 commits November 4, 2026 15:27
verifyAuthHeaders() handed the request's credential to every auth module on
each request of an existing session. A module that does not know the
credential may check it remotely: openidconnect introspects every opaque
bearer token at the identity provider, so each OCS and web request of a
client using a local oauth2 token cost two failing IdP round trips.

tryAuthModuleLogin() now records the module that established the session,
and verifyAuthHeaders() asks that module first. If it confirms the session
user the request is verified, which is what the DAV auth backends already
do; in every other case all modules are asked exactly as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Thomas Müller <[email protected]>
Address review of the session auth module shortcut:

- the built-in TokenAuthModule and BasicAuthModule are still asked after
  the session module confirmed the user, so a conflicting app password or
  basic auth credential still ends the session; only the other app
  modules, which are the remote and expensive ones, are skipped
- the session module is asked first in the regular loop instead of in a
  separate pass, so it is not called twice when it does not confirm
- every login forgets the recorded module, so a session re-established by
  another path is not vouched for by the module of an earlier login

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Thomas Müller <[email protected]>
Address the second review round:

- record the user together with the module, and only trust the module
  while that user is still the session user; some paths switch the
  session user with setUser() alone and never reach loginInOwnCloud()
- put the session module behind the built-in TokenAuthModule, so a
  conflicting app password still ends the session before any app module
  is asked, as before
- let the test double honour $includeBuiltIn, so the tests fail when the
  built-in modules drop out of the check
- describe in the changelog that the built-in checks still apply

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Thomas Müller <[email protected]>
* @return IAuthModule[]
* @throws Exception
*/
private function getAuthModulesForSession($sessionModuleClass) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

private function getAuthModulesForSession($sessionModuleClass) {
    $modules = [null];
    foreach ($this->getAuthModules(true) as $mod) {
        if (get_class($mod) === $sessionModuleClass) {
            $r[0] = $mod;
        } else {
            $r[] = $mod;
        }
    }

    if ($modules[0] === null) {
        array_shift($modules);
    }
    return $modules;
}

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants