Skip to content

fix: end a browser login that keycloak grants another access level than the chosen one - #100

Open
sttomm wants to merge 5 commits into
mainfrom
fix/oidc-full-login-scope-mismatch
Open

sttomm wants to merge 5 commits into
mainfrom
fix/oidc-full-login-scope-mismatch

Conversation

@sttomm

@sttomm sttomm commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Keycloak keeps one offline session per browser session and client and issues every token of it with the scopes of its first login. When that session started at Safe access (cli-access-write), every later Full access login came back with cli-access-write:

  • browser.Login took the missing cli-access-full scope for a meshStack without access levels and returned the level "".
  • resolveOidcLoginCredential stored "", and Previous.endIfReplaced reads "" as Full access, so the next Full access login did not end the session either.
  • Only meshstack auth status showed "with Full access, but the token grants Safe access". Building block orders failed with 403 Missing permissions (PLATFORMINSTANCE_DELETE, LANDINGZONE_DELETE, …).

Reproduced on a meshStack 2026.47.0 dev instance. The workaround was two logins, first Safe access, then Full access.

Fix

loginPages.grantedLevel reads the level from the token's scopes:

  • Chosen level granted: the CLI stores that level.
  • No access level scope (a meshStack without access levels): the CLI keeps today's behaviour. It stores "" for Full access and fails for any other level.
  • Another level's scope: the CLI revokes the new refresh token, which ends the stale offline session. The login then fails with "… granted the access level "Safe access" instead of "Full access", because it kept the access level of an earlier login; that login is now ended, so log in again and choose "Full access"". If the revoke fails too, the message names the two logins that end the session.

Changes after review

  • Previous level from the tokens. OidcLogin.GrantedLevel reads the level from the scope of the latest cached token, and falls back to the stored level. A profile that the bug stored as "" (Full access) now counts as Safe access. Its next Full access login ends the stuck session before the redirect and succeeds on the first try.
  • Tokens without any level are revoked too. A login that comes back with no access level scope for Safe access or Read-only now ends its session as well. Such a session can predate access levels on a meshStack that has them, and every retry would otherwise fail the same way.

How far the revoke reaches (Keycloak source)

  • Keycloak 26.2 and later, DefaultRefreshTokenProvider.revokeToken: revoking a refresh token removes the CLI client from that one offline session, plus the CLI's client session in the matching browser session. The browser session ends only if no other client is left in it. Other offline sessions and consent stay. That matches the doc comment on RevokeRefreshToken.
  • Keycloak 26.0, revokeClient: revoking a token also revoked the client's consent and every offline session of the user for the CLI, on every machine.
  • meshStack pins a floating keycloak:26 image, so revoking reaches that far only on a 26.0 deployment.
  • On either version, another profile that shares the same stuck session must log in again.

Verification

  • A unit test covers a matching level, a meshStack without access levels, the repro (Full access chosen, cli-access-write granted, so the new refresh token gets revoked), and a failed revoke.
  • go test ./internal/... and go tool golangci-lint run pass.
  • Not tested against a live Keycloak. The second login after the revoke should get Full access, but nobody has checked that.

Follow-up

The browser tab shows "You are logged in with Full access" before the token exchange runs. In the mismatch case it contradicts the error in the terminal. Fixing that means doing the exchange in the callback handler, so this PR leaves it out.

🤖 Generated with Claude Code

…an the chosen one

Keycloak keeps one offline session per browser session and client and issues
every token of it with the scopes of its first login. When that session began
at Safe access, a later Full access login came back with cli-access-write.
Login took the missing cli-access-full scope for a meshStack without access
levels and stored no level, which reads as Full access, so the next login did
not end the session either and every Full access login silently kept Safe
access.

A login that comes back with another level's scope now revokes the new refresh
token, which ends that offline session, and fails with a message that names
both levels and asks to log in again. If the revoke fails, the message names
the two logins that end the session. A level is stored as empty only when the
token carries no access level scope at all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@meshcloud-gh-actions

meshcloud-gh-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Coverage of the acceptance run against the meshStack backend, on f8352696ee10d0ddb16a483ef54e3fa58821b0e9.

Scope Coverage
Unit tests 78.6%
Acceptance tests 54.8%
Combined 81.1%
Uncovered functions
client/api_key.go:52: meshApiKeyClient.Create 0.0%
client/api_key.go:56: meshApiKeyClient.Read 0.0%
client/api_key.go:60: meshApiKeyClient.Update 0.0%
client/api_key.go:64: meshApiKeyClient.Delete 0.0%
client/api_key_permissions.go:20: ApiKeyPermissions.AllCodes 0.0%
client/api_key_permissions.go:33: ApiKeyPermissions.WorkspaceCodes 0.0%
client/api_key_permissions.go:263: AllApiKeyPermissions 0.0%
client/api_key_permissions.go:267: WorkspacePermissionCodes 0.0%
client/binding_status.go:18: *MeshGroupBindingStatus.WaitsForApproval 0.0%
client/binding_status.go:22: *MeshUserBindingStatus.WaitsForApproval 0.0%
client/building_block_definition.go:71: MeshBuildingBlockDefinitionApprovalPolicies.NothingRequiresApproval 0.0%
client/building_block_definition.go:82: DisabledSchedule 0.0%
client/building_block_definition.go:89: MeshBuildingBlockDefinitionSchedule.IsDisabled 0.0%
client/building_block_definition.go:93: MeshBuildingBlockDefinitionSpec.HasNeutralPolicies 0.0%
client/building_block_definition.go:97: MeshBuildingBlockDefinitionSpec.WithNeutralPolicies 0.0%
client/building_block_definition.go:166: meshBuildingBlockDefinitionClient.List 0.0%
client/building_block_definition.go:173: meshBuildingBlockDefinitionClient.Read 0.0%
client/building_block_definition.go:177: meshBuildingBlockDefinitionClient.Create 0.0%
client/building_block_definition.go:181: meshBuildingBlockDefinitionClient.Update 0.0%
client/building_block_definition.go:185: meshBuildingBlockDefinitionClient.Delete 0.0%
client/building_block_definition_version.go:102: MeshBuildingBlockType.TagInputTargets 0.0%
client/building_block_definition_version.go:246: meshBuildingBlockDefinitionVersionClient.Create 0.0%
client/building_block_definition_version.go:255: meshBuildingBlockDefinitionVersionClient.Update 0.0%
client/building_block_definition_version_implementation.go:81: MeshBuildingBlockDefinitionImplementation.InferType 0.0%
client/building_block_definition_version_implementation.go:95: MeshBuildingBlockDefinitionImplementation.MarshalJSONTo 0.0%
... and 127 more

covdata func names a method without its receiver, so an entry can belong to an implementation nothing selects rather than to a function the tests never reached. Open the file and line before reading one as a coverage gap.

A login that a CLI with the bug stored without a level read as Full access,
although its tokens grant Safe access. The next Full access login then
skipped ending the earlier session before the redirect and failed. The
previous level now comes from the scope of the login's latest cached token,
so that login ends the session first and succeeds. The stored level is the
fallback when there is no token, or none of them carries a level scope.

A login that comes back with no access level scope when Safe access or
Read-only was chosen now ends its session too. It could be a session from
before access levels rather than a meshStack without them, and without
ending it every retry would fail the same way.

Keycloak 26.2 and later end only the revoked token's own offline session and
the CLI's client session in the matching browser session. Keycloak 26.0
instead revoked the client's consent and every offline session of the user
for the client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sttomm
sttomm requested a review from grubmeshi November 19, 2026 13:28
assert.Empty(t, pages.chosenLevel())
}

func TestALoginThatKeycloakGrantsAnotherAccessLevelIsEnded(t *testing.T) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

d: Please make this an acc test case and really check it works? I've investigated this as well and I doubt it has a simple solution.

sttomm and others added 3 commits November 19, 2026 15:26
…s a safe access login

This is the case behind the fix: Keycloak grants a login the access level of
another profile's earlier login in the same browser session. The test checks
three things:

- that first login fails and ends the shared session, so the other profile's
  refresh token stops working too
- the next login gets the level it chose, both stored and in its token
- the logout test now uses the same refresh check

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…en response

The acceptance test showed what really happens: a Full access login in a
browser session that already held a Safe access login stored Full access, but
its access token carried cli-access-write. The scope field of Keycloak's token
response does not match the access token's own scope claim, and meshStack
authorizes by that claim, so the login has to check the claim.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The acceptance run on 80569ef shows a Full access login that shares its
browser session with a Safe access login of another profile. Its
code-exchange token passes the check: it carries either cli-access-full or
no level scope, and the run cannot tell which. The workspace token that the
login fetches next by a refresh grants Safe access. A refresh takes its
scopes from the offline session, which the two logins share.

80569ef put the cause in a mismatch between the token response and the
access token. The run does not show that. Reading the level from the access
token's claim is still right, since meshStack authorizes by that claim.

The login now refreshes its new token once, with openid and offline_access
so it inherits no workspace, and checks the level on the refreshed token. It
keeps that token, because Keycloak rotates the refresh token on every
refresh. On a mismatch it revokes the refreshed refresh token.

The acceptance test now names the stored level and the token's level when
the Full access login passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

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