Skip to content

feat(studio): allow admin owner scope for project endpoints - #13404

Merged
mudler merged 9 commits into
mudler:masterfrom
rossigee:feat/project-admin-owner-scope
Nov 23, 2026
Merged

mudler merged 9 commits into
mudler:masterfrom
rossigee:feat/project-admin-owner-scope

Conversation

@rossigee

@rossigee rossigee commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes #13406.

Add optional ?owner=<principal> handling on the project endpoints so an administrator can manage projects on behalf of another user. Normal callers keep existing behavior; non-admin callers passing owner get 403.

  • ChatsProject responses expose owner (additive).
  • Covered by studio_projects_test.go: admin create/list/get/delete on behalf of a user; a non-admin cannot use owner.

go test ./core/http/routes -run Project passes.

Add optional ?owner=<principal> handling on project endpoints so an
administrator can manage projects on behalf of another user. Normal
callers keep existing behavior.

Expose project owner in ChatsProject responses as an additive field.
The owner query parameter is used as the principal that names the user's
project folder, so an administrator-supplied value must not be trusted as
is. Reject a value that is not a well-formed user id (letters, digits,
'-' and '_', at most 36 characters) with 400, and a value that names no
user with 404. A caller who is not an administrator still gets 403 first,
so the answer does not reveal which users exist.

The user check goes through a new WithUserLookup handler option, wired to
the auth database in the server. Without it, or without an auth database,
no owner is known, so the request fails closed.

Assisted-by: Claude:claude-sonnet-5-5 go
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Write one info line per request that uses ?owner= to act on behalf of
another user, with the caller, the target owner, the HTTP method and the
route pattern. The request body and the project id stay out of the log.

Assisted-by: Claude:claude-sonnet-5-5 go
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
The comment on ChatsProject.Owner said the field is set only when an
administrator acts for someone else, but the handlers fill it on every
response, with the caller's own id by default. A project always has an
owner, so drop omitempty: the field is part of the contract and a client
can rely on it being there.

Assisted-by: Claude:claude-sonnet-5-5 go
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Run one table over the routes that take ?owner=: update, archive,
restore, read and write the memory, list the chats, put a chat in a
project, pull projects into a chat, and list the agents, connections,
triggers and loops. For each, a user who is not an administrator gets
403 for another owner (even an unknown one), an administrator gets 404
for an unknown owner and 400 for a malformed one (a slash, a dot
segment, a space, too long), and the owner's own id works. Also check
that the administrator reads what the owner wrote and that their own
projects stay apart.

Assisted-by: Claude:claude-sonnet-5-5 go
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Add the owner query parameter and its 400, 403 and 404 answers to the
swagger annotations of the project routes and regenerate the swagger
files, and describe the administrator's owner scope, its limits and the
audit line in the projects reference.

Assisted-by: Claude:claude-sonnet-5-5 go
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
@mudler-agent

Copy link
Copy Markdown
Collaborator

Thanks for this, and for the reference implementation in the issue. The design is a good fit for provisioning controllers, and I kept the scope as you wrote it: every project handler goes through projectOwner(), so a project opened with ?owner= shows its agents, connections, triggers, loops and chats consistently.

I pushed five commits on top of your branch (your commit is untouched) so this can merge as one change. Impersonation is a security surface, so I wanted the hardening in before it lands instead of in a follow-up:

  • Validate owner. The value is used as a per-user storage key, so it must now match the user id format (400 otherwise) and exist in the auth users table (404 otherwise). A non-admin gets 403 first, so the response does not reveal which users exist. With auth disabled it still fails closed.
  • Audit log. One info line per impersonated request with the admin, the target owner, the method and the route pattern (no ids, no bodies).
  • Owner field comment. It said the field is only set when an admin acts for someone, but responses always carry it. The comment now says so, and omitempty is gone.
  • Tests. Table-driven coverage of every project route: admin with ?owner=, non-admin 403, owner=<self>, unknown owner 404, malformed owner 400.
  • Docs and swagger. ?owner= is documented in the projects reference, and the swagger files are regenerated.

One thing is left on your side: the DCO check is red because your original commit has no Signed-off-by line. git rebase --signoff on your commit fixes it. Alternatively a maintainer can override the check at merge.

A UI for this (picking a user to act as, with a visible banner) is a separate follow-up.

Move the rule behind ?user_id= (admins only, one "User impersonation"
audit line, everyone else gets their own id) from the localai endpoints
into the auth package as EffectiveUserID, CanImpersonate and
LogImpersonation, so routes outside that package can use the same
convention. The unexported localai helpers keep their names and
behavior and now call the shared ones.

Assisted-by: Claude:claude-sonnet-5-5 go
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
The project routes took ?owner= to let an administrator act on the
projects of another user. The rest of the API uses ?user_id= for that,
so use the same parameter and the shared auth helpers (CanImpersonate,
LogImpersonation), and write the same "User impersonation" audit line.

One difference stays on purpose: a caller who is not an administrator
and names another user gets 403 here, where the other routes quietly
use the caller's own account. A client that creates projects for a user
must not create them under the wrong account without noticing. Naming
your own id works. The order is unchanged: 403, then 400 for an id that
is not well formed, then 404 for an unknown user. The owner field of a
project in the answers keeps its name.

Assisted-by: Claude:claude-sonnet-5-5 go
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Rename the query parameter in the swagger annotations, regenerate the
swagger files, and update the projects reference: the parameter, the
403 for a user who is not an administrator, and the audit line.

Assisted-by: Claude:claude-sonnet-5-5 go
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
@mudler-agent

Copy link
Copy Markdown
Collaborator

Follow-up on my earlier comment: I renamed the parameter from ?owner= to ?user_id=, in three more commits on top.

Reason: LocalAI already has an admin impersonation convention. effectiveUserID() in core/http/endpoints/localai/user_scope.go takes ?user_id=<id> from admins, and the UI already sends it (for example the collections page). A second spelling for the same idea would mean two parameters and two code paths to keep secure, so the project routes now use the existing one.

  • The admin rule and the audit line (User impersonation, with caller, target and path) now live in one place, core/http/auth/impersonation.go. The existing handlers call it too, with their behavior and tests unchanged.
  • One difference on purpose: on the project routes a non-admin who passes another user's user_id gets 403. The existing handlers ignore it silently and act as the caller. For a provisioning client, silently creating projects under the wrong account is worse than a clear error, so I kept the 403. Passing your own id still works. Validation order is unchanged: 403, then 400 for a malformed id, then 404 for an unknown user.
  • The owner field in project responses stays, since it says who owns the project.
  • Docs, swagger and tests are updated to ?user_id=.

The DCO check is still red because of the first commit's missing sign-off (see above).

@mudler
mudler merged commit 8728ff1 into mudler:master Nov 23, 2026
4 of 25 checks passed
@rossigee

Copy link
Copy Markdown
Contributor Author

Thanks! FTR, this will help me finish up my implementation of this crossplane provider...

https://github.com/rossigee/provider-localai

@rossigee
rossigee deleted the feat/project-admin-owner-scope branch November 24, 2026 00:23
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.

feat(studio): allow admins to act on projects on behalf of another user

3 participants