Files
SluijsensandClaude Sonnet 5.5 ef7df53988 Gebruikersbeheer herkent de aanroeper aan het gebruikers-id van het token
Een echt access token heeft geen naam-claim, dus User.Identity.Name was null en elke aanroeper gold als
Beheerder: een Eigenaar kreeg 403 bij het toekennen van de Eigenaar-rol, en de controles voor 'jezelf'
(niet zelf verwijderen of deactiveren) sloegen nooit aan. Nu via het id van het token. Regressietest met
echte tokens en de echte pipeline.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-08 21:29:24 +01:00

48 lines
3.8 KiB
Markdown

# Bug Fix: an Owner gets 403 when promoting an Administrator to Owner (the caller was not recognised)
- **Slug**: owner-cannot-promote-requester-identity
- **Status**: Complete (pending ASF test deploy)
- **Issue**: None (reported by Bryan via "CMS & API" and the Pi-Main journal, 2026-11-08)
- **Branch**: feature/identity-email-notifications
- **Started**: 2026-11-08
- **Documentation Language**: English
- **Conversation Language**: Dutch (user language)
## Report
**Reproduction**: on ASF test, signed in as the owner, `PUT /api/v1/Users/{id}/role` with `newRole = Owner` for an Administrator. Six calls over three hours, two user ids, all 403; signing in again did not help. It worked earlier the same day.
**Expected**: an Owner may manage everyone and may hand out the Owner role. **Actual**: 403, as if the caller were an Administrator.
**Environment**: ASF test, release 142815a, which contains the role hierarchy of run 615.
## Root Cause
`UsersController` found out who is calling through `User.Identity.Name`. A real access token (issued by `AuthService`) carries `sub` (the user id), the e-mail address, the roles, `jti` and `stamp`, but **no name claim**, so `User.Identity.Name` is **null** for every real caller (confirmed with a probe that issued a real token and read the principal; only the unit tests supplied a name claim, so they never saw it). With the caller unresolved, `GetRequesterRoleAsync` returned its deliberately restrictive fallback, **Administrator**, so `CanAssign(Administrator, "Owner")` was false and every Owner who tried to hand out the Owner role got a 403 from the controller (`ForbidResult`). Before the hierarchy was added the same unresolved caller meant "no restrictions", which is why it worked earlier that day.
The same flaw made the "yourself" checks (cannot deactivate or delete yourself, an Administrator may step down themselves) compare against a null name, so they never matched.
Not related: the `admin_bypass_rejected ... (reason: Expired)` events. The availability middleware checks the bearer token of every request to decide whether an administrator may pass during maintenance; an access token older than its 60 minutes is logged as rejected there. That is normal noise from a tab that still holds an expired token before the silent refresh (the request then gets a 401 and the client refreshes), and an expired token would give a 401, not a 403.
**Confirmed with user**: relayed by "CMS & API" (Bryan asked for a cause first, then a fix if it is a bug).
## The Fix
- `UsersController`: the caller is found by the **user id of the token** (`NameIdentifier`/`sub`), through one helper (`CurrentUserId`, `GetRequesterAsync`); `IsSelf` compares ids. The name is only a fallback for callers that carry no id. The restrictive fallback for an unresolvable caller stays (secure by default).
- `SiteSettingsController`: the recorded `UpdatedBy` uses the e-mail claim (it was null for the same reason).
- Behaviour that now works as designed: an Owner can promote to Owner, keep their own role, demote and manage others; nobody can delete or deactivate themselves (it used to slip through); the last Owner cannot be demoted by themselves; an Administrator can still step down themselves and promote a User to Administrator, but never hands out Owner.
## Verification
**Path used**: Automated test framework. New `UsersApiWithRealTokensTests` (Identity.Tests): real login tokens from `AuthService`, the real bearer pipeline and policies, the real controller on a TestServer.
**Before the fix** (id lookup disabled): 5 of the 7 scenarios fail, including "an Owner can promote an Administrator to Owner" (403) and "nobody can delete or deactivate themselves".
**After the fix**: 7 of 7 pass; Identity.Tests 133, Core.Tests 547 green.
## Result
- **PR(s)**: None
- **Commit(s)**: see branch history
- **Completed**: 2026-11-08