Skip to content

mcpx: principal per surface + permission per operation, declared permissively (#1968) - #2649

Merged
dezren39 merged 3 commits into
mainfrom
queue/3264-1968-principal-permission
Oct 30, 2026
Merged

dezren39 merged 3 commits into
mainfrom
queue/3264-1968-principal-permission

Conversation

@dezren39

@dezren39 dezren39 commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

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 — Permission tiers read / run / admin; DerivePermission (Admin → admin, Mutating/Destructive → run, else read); Op.EffectivePermission(); ByRoute for 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 every Decision in a bounded in-memory log readable via Decisions().
  • Every one of the 81 operations now declares 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.Server ConnContext, wired on all three server constructions) builds the principal while the connection's surface is still visible; authorize middleware (wrapping the mux in both startHTTP and Handler()) 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.
  • The three places that state the unauthenticated model (server.go Listen 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

  • Tiers vs per-operation permissions: tiers (the issue's lean). Three names, declared on the table, pinned against the existing flags by test.
  • Tool calls: out of scope here, as the packet directs — Design: mcpx write policy — one gate for writes (ask/allow/deny, argument rules, unknown-outcome ledger) #2126 owns the write-policy gate for tool calls; this vocabulary covers mcpx's own operations.
  • Principal in request context vs session: request context. A principal is not a session; they travel separately, and the context is where the funnel already is.
  • mcpx doctor reporting "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.
  • Shared machinery with Phase 5: who may clear a tag claim #185's clamping: noted, not shared yet — the shapes rhyme but the clamp is settings plumbing; merging them now would couple two permissive-stage vocabularies before either has a policy.
  • Peer uid usefulness on a single-user laptop: rarely distinguishes anything there, as the issue says — it matters on shared hosts and costs one getsockopt.

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; Authorize answers yes and records the decision; ByRoute finds declared ops only.
  • internal/daemon: unix principal carries the real peer uid (socket pair), TCP principal is its address, connPrincipal lands the principal in the context.
  • Local (laptop): go test -count=1 ./internal/api/ ./internal/daemon/ ./internal/mcpserver/ ./internal/cli/ — all pass; go vet clean; gofmt clean. Full suite is CI's job on this PR.

Refs #1968 (close-out evidence follows on merge).


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

…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.
@dezren39
dezren39 requested a review from a team as a code owner October 30, 2026 09:10
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bb542145-435d-40f4-ae64-86a26c2f0f11
Commits

Reviewing files that changed from the base of the PR and between d37f225 and 582efac.

Files selected for processing: 26
  • pkgs/mcpx/go.mod
  • pkgs/mcpx/internal/api/authz_test.go
  • pkgs/mcpx/internal/api/openapi.go
  • pkgs/mcpx/internal/api/ops.go
  • pkgs/mcpx/internal/api/ops_consumer.go
  • pkgs/mcpx/internal/api/ops_exec.go
  • pkgs/mcpx/internal/api/ops_proto.go
  • pkgs/mcpx/internal/api/ops_resolve.go
  • pkgs/mcpx/internal/api/ops_secrets.go
  • pkgs/mcpx/internal/api/ops_settings.go
  • pkgs/mcpx/internal/api/parity_test.go
  • pkgs/mcpx/internal/api/permission.go
  • pkgs/mcpx/internal/api/principal.go
  • pkgs/mcpx/internal/daemon/authz.go
  • pkgs/mcpx/internal/daemon/authz_test.go
  • pkgs/mcpx/internal/daemon/ops.go
  • pkgs/mcpx/internal/daemon/origin.go
  • pkgs/mcpx/internal/daemon/peercred_linux.go
  • pkgs/mcpx/internal/daemon/peercred_other.go
  • pkgs/mcpx/internal/daemon/routes_consumer.go
  • pkgs/mcpx/internal/daemon/routes_exec.go
  • pkgs/mcpx/internal/daemon/routes_proto.go
  • pkgs/mcpx/internal/daemon/routes_resolve.go
  • pkgs/mcpx/internal/daemon/routes_secrets.go
  • pkgs/mcpx/internal/daemon/routes_settings.go
  • pkgs/mcpx/internal/daemon/server.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T11:45:08.223495Z 582efac New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread pkgs/mcpx/internal/daemon/authz_test.go Outdated
Comment on lines +37 to +39
want := api.UnixPeerPrincipal(uint32(os.Getuid()))
if p != want {
t.Errorf("principal = %+v, want %+v (peer credentials for our own uid)", p, want)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Comment on lines +93 to +95
next.ServeHTTP(w, r)
if op, ok := api.ByRoute(r.Pattern); ok {
api.Authorize(p, op)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Comment on lines +75 to +78
if r.RemoteAddr == "" {
return api.InProcessPrincipal()
}
return api.TCPPrincipal(r.RemoteAddr)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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.
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.

1 participant