Repository navigation
mcpx: principal per surface + permission per operation, declared permissively (#1968) - #2649
Conversation
…1968) Approach C from #1968, built permissively: every surface constructs a principal (unix peer credentials read for the first time, TCP by address, in-process as self), every one of the 81 operations declares a permission tier (read/run/admin) on its Op entry, and the daemon funnel (ConnContext + authorize middleware) puts the principal in the request context and records each decision via api.Authorize. Every principal resolves to the owner and every check answers yes: behaviour is unchanged, and the OpenAPI text keeps saying the API is unauthenticated while documenting the new vocabulary.
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 27 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 41366cbd35
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| want := api.UnixPeerPrincipal(uint32(os.Getuid())) | ||
| if p != want { | ||
| t.Errorf("principal = %+v, want %+v (peer credentials for our own uid)", p, want) |
There was a problem hiding this comment.
Make the peer-UID test platform-aware
On macOS and every other non-Linux platform, peercred_other.go deliberately returns no UID, so principalForConn returns api.UnixPrincipal(). This unconditional comparison with UnixPeerPrincipal(os.Getuid()) therefore fails even when the implementation behaves correctly. The package enables tests in its Nix build (package.nix), so this also breaks non-Linux package builds. Restrict the UID assertion to Linux or assert the documented fallback on other platforms.
Useful? React with 👍 / 👎.
| next.ServeHTTP(w, r) | ||
| if op, ok := api.ByRoute(r.Pattern); ok { | ||
| api.Authorize(p, op) |
There was a problem hiding this comment.
Record decisions before entering long-lived handlers
For GET /v1/events, handleEvents flushes the response and then remains in its subscription loop until the client disconnects. Because Authorize runs only after ServeHTTP returns, an active event subscription has no recorded permission decision for its entire lifetime; if the daemon terminates first, it is never recorded. Long-running calls have the same delay. This leaves ongoing traffic out of the decision history that this change introduces. Resolve the matched operation and record its decision before invoking the handler.
Useful? React with 👍 / 👎.
| if r.RemoteAddr == "" { | ||
| return api.InProcessPrincipal() | ||
| } | ||
| return api.TCPPrincipal(r.RemoteAddr) |
There was a problem hiding this comment.
Preserve the in-process principal for inline CLI requests
In CLI inline mode, inlineDaemon passes srv.InProcessHandler(life) to newInProcessTransport (internal/cli/connect.go:266). That transport serves requests through a private http.Server over net.Pipe, without a ConnContext hook, so incoming requests have RemoteAddr == "pipe", not an empty address. Consequently, actual inline CLI operations are recorded as TCP callers with ID pipe rather than in-process callers with ID self, making the new principal history misidentify this surface. Attach InProcessPrincipal explicitly in the inline serving path instead of relying on an empty remote address.
Useful? React with 👍 / 👎.
- peer-UID test asserts per-platform: peer uid on Linux, surface-only fallback elsewhere (peercred_other.go) - permission decision recorded by route before the handler runs, so long-lived handlers (GET /v1/events) are recorded for their lifetime; all registrations funnel through s.route - InProcessHandler stamps InProcessPrincipal explicitly instead of inferring from RemoteAddr (net.Pipe reads as tcp pipe) - parity test reads both registration idioms Tests: api package green; 3 new funnel-pinning tests pass; daemon balance identical to clean tree (20 pre-existing AF_UNIX sandbox failures, proven by stash compare)
Main added POST /v1/adapters and DELETE /v1/adapters/{name} with
handlers but no declared Permission; the funnel invariant (every op
declares the tier its flags derive) gains PermissionAdmin on both.
Conflict in routes_settings.go resolved toward the s.route funnel.
Implements #1968 (Approach C) — queue task 3264: a principal per surface and a permission per operation, declared permissively. No enforcement, no policy format, no behaviour change.
What changed
Vocabulary (
internal/api)permission.go—Permissiontiersread/run/admin;DerivePermission(Admin → admin, Mutating/Destructive → run, else read);Op.EffectivePermission();ByRoutefor the funnel.principal.go—Principal{Surface, ID, Owner}with constructors per surface (UnixPeerPrincipal,UnixPrincipal,TCPPrincipal,InProcessPrincipal,AnonymousPrincipal); context plumbing (WithPrincipal/PrincipalFrom);Authorize— the one check, which records everyDecisionin a bounded in-memory log readable viaDecisions().Permission:on its Op entry (20 admin, 15 run, 46 read), consistent with the flags it already carried.Funnel (
internal/daemon)authz.go—connPrincipal(http.ServerConnContext, wired on all three server constructions) builds the principal while the connection's surface is still visible;authorizemiddleware (wrapping the mux in bothstartHTTPandHandler()) ensures the request context carries a principal and, after dispatch, records the decision for the operation the matched route declares.peercred_linux.go/peercred_other.go— SO_PEERCRED peer-uid reading on Linux (the peer uid was always available and read nowhere); other platforms construct the unix principal without a uid.server.goListen comment,origin.go, the OpenAPI description) now document the vocabulary while keeping the admission — the OpenAPI description still says "unauthenticated" (the advisory test pins that word, and still passes).Open questions from #1968 — answers
mcpx doctorreporting "authorisation: permissive": not in this PR (the daemon's status/health surface is the natural home); recommended as a small follow-up so nobody deploys believing otherwise. The OpenAPI description and the three comments already say it loudly.Tests
internal/api: every op declares a permission and it equals its flag-derived tier; every constructible principal resolves to owner and allows every tier;Authorizeanswers yes and records the decision;ByRoutefinds declared ops only.internal/daemon: unix principal carries the real peer uid (socket pair), TCP principal is its address,connPrincipallands the principal in the context.go test -count=1 ./internal/api/ ./internal/daemon/ ./internal/mcpserver/ ./internal/cli/— all pass;go vetclean; gofmt clean. Full suite is CI's job on this PR.Refs #1968 (close-out evidence follows on merge).
Need help on this PR? Tag
@codesmith-botwith what you need.