Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Custom JWT claims: user_id, user_roles, user_permissions, org_role, org_permissi

### 2.3 Impersonation

Owner organization only (Staff and Owner user roles carry `impersonate:users`). `POST /api/impersonate` mints a JWT with the target user's permissions, expiring with the remaining consent window (not a fixed hour). Requires the target to have opted-in via `POST /api/impersonate/consent` (1-168h, default 1h; revoked with DELETE). All sessions and actions are audited via `impersonation_audit` middleware. No self-impersonation, no chaining. An account holding the Owner user role is never a target. `DELETE /api/impersonate` revokes the impersonation token and re-mints the impersonator's session from its current state (a suspended impersonator gets 401); `GET /api/impersonate/status` re-checks the target's consent before handing out a token. The consent and sessions endpoints refuse impersonation tokens (`DisableOnImpersonate`): they belong to the real account only.
Owner organization only (Staff and Owner user roles carry `impersonate:users`). `POST /api/impersonate` mints a JWT with the target user's permissions, expiring after `IMPERSONATION_SESSION_DURATION` (default 1h, capped at 168h). The target's consent is not asked: any active user is a target, across the whole hierarchy. All sessions and actions are audited via `impersonation_audit` middleware. No self-impersonation, no chaining. An account holding the Owner user role is never a target. `POST /api/impersonate/extend` (impersonation token only, never an API key) restarts the session for a full duration with a new token and revokes the presented one, re-checking the impersonator's `impersonate:users` and the target from their current state; it is audited as `session_extended`. `DELETE /api/impersonate` revokes the impersonation token and re-mints the impersonator's session from its current state (a suspended impersonator gets 401); `GET /api/impersonate/status` hands out a token for the active session. The sessions endpoints refuse impersonation tokens (`DisableOnImpersonate`): they belong to the real account only.

---

Expand Down Expand Up @@ -226,6 +226,7 @@ Embedded in the custom JWT at exchange time. No external calls during request ha
- `middleware.RequireResourcePermission("systems")` β€” HTTP-verb-aware: `read:` on GET, `manage:` on **POST/PUT/PATCH/DELETE alike** (`middleware/rbac.go`). `destroy:` is never derived from the verb β€” an endpoint that needs it says so explicitly, e.g. `middleware.RequirePermission("destroy:systems")` on `/systems/:id/destroy`
- `middleware.RequireResourcePermissionOrSelf("resellers")` β€” as above, plus a GET on the caller's own organization is always allowed (org roles carry permissions for the levels *below*, so without this a reseller could not read itself)
- `middleware.PreventSelfModification()` β€” blocks a user from acting on their own account for dangerous verbs
- Soft-deleted objects belong to the Owner organization: outside it, a list or export with `status=deleted` gets 403 (`methods.rejectDeletedStatusOutsideOwnerOrg`) and the `/:id/restore` routes are gated by `middleware.RequireOrgRole(models.OwnerOrgRole)`. For everyone else, Delete is final
- Hierarchy check for cross-org reads/writes: `local.UserService.IsOrganizationInHierarchy(orgRole, userOrgID, targetOrgID)`
- RBAC filtering in SQL: `helpers.AppendOrgFilter(query, orgRole, orgID, tableAlias, args, nextArgIdx)` uses `GetAllowedOrgIDsForFilter` (cached org-ID set) for non-owner roles

Expand Down Expand Up @@ -270,7 +271,7 @@ Entities synced with Logto (orgs, users) carry both `id` (DB UUID) and `logto_id

### 6.2 Error messages

User-facing errors are **lowercase**. Example: `"the user has revoked consent for impersonation. please exit impersonation mode."`
User-facing errors are **lowercase**. Example: `"cannot impersonate while already impersonating another user. exit current impersonation first."`

Field validation errors (`response.ValidationError` with a 400/409/422 status) are not shown to users as written: the frontend turns each one into an i18n key, `<prefix>.<key>_<message>` (`getValidationIssues` in `frontend/src/lib/validation.ts`). The dots of a nested key become underscores (`custom_data.vat` gives `custom_data_vat`); the message is lowercased and its spaces become underscores, while any other character stays as it is. Therefore:

Expand Down
4 changes: 4 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ REDIS_URL=redis://localhost:6379
# changes trigger a run on their own; 0 disables the periodic loop.
#THIRD_PARTY_APPS_IDP_RECONCILE_INTERVAL=15m

# How long an impersonation session lasts before it expires (Go duration,
# default 1h, capped at 168h)
#IMPERSONATION_SESSION_DURATION=1h

# ===========================================
# MIMIR CONFIGURATION (Optional)
# ===========================================
Expand Down
5 changes: 4 additions & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ BACKUP_S3_USE_PATH_STYLE=false
# Lifetime of a presigned download URL (capped server-side at 15m).
#BACKUP_PRESIGN_TTL=5m

# Lifetime of an impersonation session (Go duration, capped server-side at 168h).
#IMPERSONATION_SESSION_DURATION=1h

# ===========================================
# SMTP EMAIL CONFIGURATION (Optional)
# ===========================================
Expand Down Expand Up @@ -137,7 +140,7 @@ Templates are in `services/email/templates/` and support Go template syntax.
Role pairing is enforced fail-closed by the API: inside the Owner organization only Staff is assignable, Staff is never assignable outside it, and the Owner user role is never assignable at all β€” it belongs solely to the bootstrap `owner` account seeded by `sync init` and is not returned by `GET /api/roles`. Staff holds all non-destructive technical scopes (including `impersonate:users` and `connect:systems`) but no `destroy:systems|users` and cannot manage the Owner organization's own membership; the Owner role adds those.

### User Impersonation
Requires the `impersonate:users` permission, held only by the Staff and Owner user roles: temporarily act as another user with 1-hour scoped JWT tokens. Consent of the target user is mandatory; reach is platform-wide by design. Prevents self-impersonation and token chaining. All actions are logged.
Requires the `impersonate:users` permission, held only by the Staff and Owner user roles: temporarily act as another user with scoped JWT tokens that expire after `IMPERSONATION_SESSION_DURATION` (default 1h). The target user's consent is not asked; reach is platform-wide by design. Prevents self-impersonation and token chaining. All actions are logged.

- `POST /api/auth/impersonate` - Start impersonation
- `POST /api/auth/exit-impersonation` - Exit impersonation
Expand Down
20 changes: 9 additions & 11 deletions backend/authz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,20 @@ and 3 below are **fixed**; finding 1 turned out not to be a defect.

### 1. Cross-hierarchy impersonation β€” INTENDED, spec states it

`POST /api/impersonate` checks the `impersonate:users` permission and the
target's consent but **not** whether the target is inside the caller's
hierarchy: an Owner-organization user reaches any consenting user of any
`POST /api/impersonate` checks the `impersonate:users` permission but **not**
whether the target is inside the caller's hierarchy, and it does not ask the
target's consent: an Owner-organization user reaches any active user of any
distributor, reseller or customer β€” all 200.

That is the design, not a defect. The Owner organization's staff must be able
to support users of every distributor and reseller. The feature therefore has
two barriers, and the hierarchy is deliberately not one of them:
one barrier, and the hierarchy is deliberately not it: the caller must hold
`impersonate:users`, which only the **Staff** and **Owner** user roles carry β€”
both confined to the Owner organization (Staff is assignable only there, by an
Owner-role caller; Owner is never assignable at all). An Owner-role account is
never a target, and every session is audited.

1. the target must have consented;
2. the caller must hold `impersonate:users`, which only the **Staff** and
**Owner** user roles carry β€” both confined to the Owner organization (Staff
is assignable only there, by an Owner-role caller; Owner is never
assignable at all).

**Barrier 2 is what protects the whole feature**, so that is where the suite
**That barrier is what protects the whole feature**, so that is where the suite
asserts. Verified: a distributor Admin, a reseller Admin, a customer Admin and
a Backoffice user are all refused when promoting a user to Staff *or* creating
one β€” including when the role id is supplied directly rather than picked from
Expand Down
53 changes: 35 additions & 18 deletions backend/authz/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,10 @@ routes:
note: revoking somebody else's key must fail on ownership, not on a permission

# ===========================================================================
# Impersonation. Consent is the caller's own business; impersonating somebody
# requires the dedicated impersonate:users permission, which only the Owner
# and Staff user roles carry
# holds in config.yml.
# Impersonation. Impersonating somebody requires the dedicated
# impersonate:users permission, which only the Owner and Staff user roles
# carry in config.yml; the target's consent is not asked.
# ===========================================================================
- method: GET
path: /api/impersonate/consent
intent: authenticated

- method: POST
path: /api/impersonate/consent
intent: authenticated
skip: mutates the caller's own consent state; exercised explicitly in the special layer

- method: DELETE
path: /api/impersonate/consent
intent: authenticated
skip: mutates the caller's own consent state; exercised explicitly in the special layer

- method: GET
path: /api/impersonate/status
intent: authenticated
Expand All @@ -184,6 +169,15 @@ routes:
intent: authenticated
note: exiting an impersonation the caller is not in is a no-op, not an authorization question

- method: POST
path: /api/impersonate/extend
intent: authenticated
note: |
Only an impersonation token has a session to extend: any other token gets
400. The handler re-reads the impersonator and requires impersonate:users
from its current state; API keys are refused by RejectAPIKey, asserted in
the special layer.

- method: GET
path: /api/impersonate/sessions
intent: authenticated
Expand Down Expand Up @@ -232,6 +226,8 @@ routes:
- method: PATCH
path: /api/systems/:id/restore
intent: manage:systems
org_role: owner
note: soft-deleted objects are seen and restored by the Owner organization only
- method: DELETE
path: /api/systems/:id/destroy
intent: destroy:systems
Expand All @@ -242,6 +238,19 @@ routes:
- method: PATCH
path: /api/systems/:id/reactivate
intent: manage:systems
- method: PUT
path: /api/systems/:id/favorite
intent: read:systems
note: |
Starring is a personal bookmark that changes nothing on the system, so it
needs only read access to it β€” a Reader may star what it can see. The
system must still be in the caller's scope.
- method: DELETE
path: /api/systems/:id/favorite
intent: read:systems
note: |
Removes only the caller's own star, so it may target a system that has
since left the caller's scope.
- method: GET
path: /api/systems/totals
intent: read:systems
Expand Down Expand Up @@ -651,6 +660,8 @@ routes:
- method: PATCH
path: /api/distributors/:id/restore
intent: manage:distributors
org_role: owner
note: soft-deleted objects are seen and restored by the Owner organization only
- method: DELETE
path: /api/distributors/:id/destroy
intent: destroy:distributors
Expand Down Expand Up @@ -709,6 +720,8 @@ routes:
- method: PATCH
path: /api/resellers/:id/restore
intent: manage:resellers
org_role: owner
note: soft-deleted objects are seen and restored by the Owner organization only
- method: DELETE
path: /api/resellers/:id/destroy
intent: destroy:resellers
Expand Down Expand Up @@ -797,6 +810,8 @@ routes:
- method: PATCH
path: /api/customers/:id/restore
intent: manage:customers
org_role: owner
note: soft-deleted objects are seen and restored by the Owner organization only
- method: DELETE
path: /api/customers/:id/destroy
intent: destroy:customers
Expand Down Expand Up @@ -875,6 +890,8 @@ routes:
- method: PATCH
path: /api/users/:id/restore
intent: manage:users
org_role: owner
note: soft-deleted objects are seen and restored by the Owner organization only
- method: DELETE
path: /api/users/:id/destroy
intent: destroy:users
Expand Down
67 changes: 67 additions & 0 deletions backend/authz/scenarios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,73 @@ scenarios:
path: /api/customers/{org:d1r2c1}/suspend
expect: denied

# ===========================================================================
# Soft-deleted objects belong to the Owner organization: it alone sees and
# restores them, and for everyone else a Delete is final. Outside it, a list
# or an export filtered on status=deleted is refused, and so is a restore,
# even of an object inside the caller's own hierarchy.
# ===========================================================================
- name: distributor cannot list deleted resellers
as: d1-admin
method: GET
path: /api/resellers?status=deleted
expect_status: [403]
- name: reseller cannot list deleted customers
as: d1r1-admin
method: GET
path: /api/customers?status=deleted
expect_status: [403]
- name: reseller cannot list deleted users
as: d1r1-admin
method: GET
path: /api/users?status=deleted
expect_status: [403]
- name: customer cannot list deleted systems
as: d1r1c1-admin
method: GET
path: /api/systems?status=deleted
expect_status: [403]
- name: reseller cannot list deleted systems among other statuses
as: d1r1-admin
method: GET
path: /api/systems?status=active&status=deleted
expect_status: [403]
- name: reseller cannot export deleted customers
as: d1r1-admin
method: GET
path: /api/customers/export?format=csv&status=deleted
expect_status: [403]
- name: distributor cannot export deleted systems
as: d1-admin
method: GET
path: /api/systems/export?format=csv&status=deleted
expect_status: [403]
- name: staff can list deleted customers
as: owner-staff
method: GET
path: /api/customers?status=deleted
expect: allowed
- name: staff can list deleted users
as: owner-staff
method: GET
path: /api/users?status=deleted
expect: allowed
- name: reseller cannot restore its own customer
as: d1r1-admin
method: PATCH
path: /api/customers/{org:d1r1c1}/restore
expect_status: [403]
- name: reseller cannot restore its own user
as: d1r1-admin
method: PATCH
path: /api/users/{user:d1r1c1-admin}/restore
expect_status: [403]
- name: customer cannot restore its own system
as: d1r1c1-admin
method: PATCH
path: /api/systems/{system:s-d1r1c1}/restore
expect_status: [403]

- name: reseller cannot restore an organization outside its hierarchy
as: d1r1-admin
method: PATCH
Expand Down
Loading
Loading