Skip to content

fix: avoid local token introspection and handle rejected session refresh - #377

Closed
H-LK wants to merge 2 commits into
owncloud:masterfrom
H-LK:fix/oidc-local-tokens-session-refresh
Closed

H-LK wants to merge 2 commits into
owncloud:masterfrom
H-LK:fix/oidc-local-tokens-session-refresh

Conversation

@H-LK

@H-LK H-LK commented Oct 9, 2026 •

Copy link
Copy Markdown

Description

This PR addresses two separate OIDC authentication problems:

  • Redundant introspection of valid local OAuth2 tokens: Before processing a Bearer token, check whether the enabled OAuth2 app recognizes it as valid. If so, the OIDC module returns null, leaving authentication to OAuth2. Unknown tokens, absent/disabled OAuth2 and PoP retain the existing OIDC verification path. Core authentication and identity-consistency checks remain unchanged.
  • HTTP 500 after a rejected browser-session refresh: Handle HTTP 400 responses containing invalid_grant by ending the local session. Remove only this verifier's logout listener to avoid another IdP request with rejected credentials. Other logout listeners still run; unrelated HTTP and transport exceptions are rethrown. The corresponding decoded error response is handled as well.

The OAuth2 check uses actual token validation, not token length or shape. It adds a local token/user lookup for Bearer requests; database lookup errors are not swallowed. No new dependencies or core changes are introduced.

Related Issue

Motivation and Context

With ownCloud Server 11.0.1, OAuth2 0.7.2 and OpenID Connect 2.4.2, session-bearing requests authenticated with valid local OAuth2 tokens also entered OIDC verification and triggered unsuccessful IdP introspection.

Separately, a browser request failed with HTTP 500 after Keycloak 26.7.4 returned invalid_grant: Token is not active from its token endpoint. The trace passed through SessionVerifier::refreshToken() from the cached-expiry branch. The reason the IdP rejected that token was not established. This change handles the rejection, rather than changing token lifetime or IdP policy.

How Has This Been Tested?

  • Full app PHPUnit suite using the standard ownCloud test bootstrap, core 11.0.1, SQLite and PHP 8.3.6: 286 tests, 561 assertions passed.
  • PHPStan level 5: passed.
  • Phan with the polyfill parser: passed.
  • ownCloud coding-standard check for all four changed files: passed.
  • Separate isolated regression using real OAuth2 validation logic: 33 tests, 101 assertions passed.
  • Disposable HTTP tests with a loopback OIDC provider: cached and uncached rejected refreshes redirect to login (HTTP 303), invalidate the old session and permit a fresh OIDC login; baseline controls reproduce HTTP 500.
  • Session-bearing local-token probes: candidate 0 vs baseline 1 introspection; a bounded 24-request run produced 0 vs 24 introspections. These are functional checks, not production performance measurements.

The isolated and HTTP harnesses are external to this PR; the regression unit tests are included. This is not a real-Keycloak, LDAP, clustered-database or production-load acceptance test. The extended HTTP harness is not fully green: an independent check fails on both baseline and candidate and needs separate follow-up outside this PR. The full upstream database CI matrix was not run locally.

Types of changes

  • Bug fix
  • New feature
  • Breaking change
  • Technical debt
  • Tests

Checklist

  • Code changes
  • Unit tests added
  • Acceptance tests added to the upstream repository (external harness used)
  • Documentation ticket raised (none; no new configuration or user-facing feature)
  • GPG-signed commit with DCO sign-off

Open tasks

  • Upstream CI and maintainer review
  • Maintainer decision on backporting

Please apply the appropriate review/backport labels and milestone where repository permissions are required.

Delegate valid local OAuth2 bearer tokens without invoking OIDC verification. End only the local session when a refresh is rejected with invalid_grant, avoiding a second failing IdP logout request. Preserve other authentication errors and add regression coverage for cached and uncached sessions.

Signed-off-by: Hendrik Linka <[email protected]>
@H-LK
H-LK requested a review from a team as a code owner October 9, 2026 10:06
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@oc-tmueller

Copy link
Copy Markdown
Contributor

Thanks a lot @H-LK for the thorough analysis and the reproducer. We could reproduce it exactly: with a session cookie, every OCS and web request that carries a local OAuth2 token cost two requests to the identity provider (discovery plus an introspection answering active: false).

We'd like to fix that part in core instead of here, though. One design principle of these apps is that they don't interact with each other, so openidconnect shouldn't instantiate or query classes of the oauth2 app. The root cause is in Session::verifyAuthHeaders(), which hands the credential of an established session to every auth module. owncloud/core#41967 makes the session remember the module that established it, and lets that module verify the request before the other app modules are asked. The DAV auth backends already work this way. Everything else keeps the full identity checks, and on your scenario it brings the IdP requests down to zero.

Could you split the invalid_grant / #376 part (the SessionVerifier change) out into its own PR, without the OAuth2 check in OpenIdConnectAuthModule? That fix is independent, and we'd be happy to review it on its own.

@H-LK

H-LK commented Nov 16, 2026

Copy link
Copy Markdown
Author

Thanks a lot for reproducing that — it confirms the analysis and the reproducer.

As requested, I am splitting this PR into two independent, SessionVerifier-only PRs:

  1. invalid_grant / Browser session refresh returns HTTP 500 when the IdP rejects the refresh token with invalid_grant #376 — the SessionVerifier change that ends the local session without the second, failing sign-out request: fix: end local session on rejected refresh token #382
  2. refresh window — refresh with less than 60 s remaining instead of 5 min, so a standard 5-minute access token stops triggering a refresh on every request: fix: update session token refresh logic to trigger with less than 60 seconds remaining #383

I agree with your call on the remaining part: the OpenIdConnectAuthModule / local token verification belongs in core, so I am dropping it from this app in favor of owncloud/core#41967.

I am closing this PR in favor of #382 and #383 and will delete the fix/oidc-local-tokens-session-refresh branch afterwards.

@H-LK H-LK closed this Nov 16, 2026
@H-LK
H-LK deleted the fix/oidc-local-tokens-session-refresh branch November 16, 2026 13:16
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.

Browser session refresh returns HTTP 500 when the IdP rejects the refresh token with invalid_grant

3 participants