Skip to content

feat(broker): per-user credential-brokering audit records (spec 074 T10) - #694

Merged
Dumbris merged 2 commits into
mainfrom
074-t10-broker-audit
Jun 20, 2026
Merged

Dumbris merged 2 commits into
mainfrom
074-t10-broker-audit

Conversation

@Dumbris

@Dumbris Dumbris commented Jun 16, 2026

Copy link
Copy Markdown
Member

Spec 074 T10 — Audit integration: per-user brokering activity records

Closes the per-user attribution payoff (User Story 4). Emits a secret-free activity-log record on every per-user credential operation — acquisition, refresh, injection, connect — carrying attribution (user id, server, request id), the acquisition method (token_exchange / entra_obo / connect), and the outcome (success/failure + a coarse reason). No token/refresh/secret value is ever recorded (FR-028, FR-029, SC-006).

Design

  • broker.AuditEvent / broker.AuditSink (internal/serveredition/broker/audit.go): a structurally secret-free port — the event struct has no field able to hold a credential, so auditing cannot leak a secret. Default is a no-op sink.
  • CredentialResolver.Resolve emits exactly one classified event per resolution. The action (acquire / refresh / inject cache-hit / connect not-connected) is threaded out of the single-flight acquire so success and failure are attributed correctly. Exchanger errors are already sanitized upstream; NotConnectedError/PolicyDeniedError/sentinels are coarse and secret-free.
  • OAuthConnector.Complete / Deny emit the connect-flow event (the connect completion path is REST-driven and never flows through Resolve). Failure reasons are fixed coarse labels, never the raw upstream token-endpoint body. Refresh stays unaudited at the connector — the resolver owns refresh auditing (no double-count).
  • storage.ActivityTypeCredentialBroker + an activity-log-backed adapter (api.NewActivityAuditSink) using SaveActivityAsync (non-blocking on the request path). This reuses the existing activity log. Wired into the live connect flow via NewCredentialHandlers → connectorProvider → NewOAuthConnector.

Note: the broker's injection path (Resolve→InjectFor) is not yet wired into production (a later task). The audit port is in place on the resolver so injection/acquire/refresh auditing flows automatically once that wiring lands; it is unit-tested now with a fake sink. The connect flow is live today and audited end-to-end.

Tests (TDD)

  • audit_resolver_test.go — cache-hit→inject, fresh→acquire, near-expiry→refresh, entra_obo method mapping, exchange-failure (reason, no secret), policy-denied→inject failure, not-connected→connect failure, store-disabled, anonymous (no event).
  • audit_connector_test.go — connect Complete success/failure (coarse reason, no upstream body), invalid-state, Deny; asserts no upstream token / client secret in any event.
  • broker_audit_test.go — adapter persists a real credential_broker record with attribution + request_id and no secret; nil-storage → nil sink.

Verification

  • go build ./cmd/mcpproxy (personal) + go build -tags server ./cmd/mcpproxy ✅
  • go test -tags server ./internal/serveredition/... -race ✅
  • go test ./internal/storage/... ./internal/runtime/... -race -short ✅
  • golangci-lint v2.5.0 --config .github/.golangci.yml --new-from-rev=origin/main → 0 issues ✅

Docs

  • docs/features/activity-log.md and docs/cli/activity-commands.md updated with the new credential_broker activity type.

Related spec 074 (blockers MCP-1039/T6, MCP-1040/T7 merged in #691).

Emit a secret-free activity-log record on every per-user credential
operation — acquisition, refresh, injection, and connect — carrying
attribution (user id, server, request id), the acquisition method, and the
outcome (success/failure + coarse reason). No token/refresh/secret value is
ever recorded (FR-028, FR-029, SC-006; User Story 4 payoff).

- broker.AuditEvent/AuditSink: a structurally secret-free audit port (no
  field can hold a credential) with a default no-op sink.
- CredentialResolver.Resolve emits one classified event per resolution
  (acquire / refresh / inject / connect), threading the action out of the
  single-flight acquire so success and failure are attributed correctly.
- OAuthConnector.Complete/Deny emit the connect-flow event; failure reasons
  are fixed coarse labels, never the raw upstream token-endpoint body.
- storage.ActivityTypeCredentialBroker + an activity-log-backed AuditSink
  adapter (SaveActivityAsync, non-blocking) that reuses the existing
  activity log; wired into the live connect flow via NewCredentialHandlers.

TDD: resolver/connector emit correct attribution on success and failure and
never leak token material; the adapter persists records with no secret.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 16, 2026 •

Copy link
Copy Markdown

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: 074-t10-broker-audit

Available Artifacts

  • archive-darwin-amd64 (28 MB)
  • archive-darwin-arm64 (25 MB)
  • archive-linux-amd64 (16 MB)
  • archive-linux-arm64 (14 MB)
  • archive-windows-amd64 (28 MB)
  • archive-windows-arm64 (25 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (21 MB)
  • installer-dmg-darwin-arm64 (19 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 27738294419 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

The raw OAuth AS error= query parameter is now passed through
sanitizeCallbackError which maps known error codes to fixed labels
and falls back to 'authorization_denied' for anything else. This
prevents arbitrary upstream error text from reaching the audit log.

Regression tests:
- TestCredentialsCallback_DeniedByUpstream: known access_denied
  passes through the sanitizer unchanged in the audit record.
- TestCredentialsCallback_Denied_UnknownErrorCoerced: arbitrary
  upstream error strings are coerced to 'authorization_denied'.

Related MCP-2858

@mcpproxy-gatekeeper mcpproxy-gatekeeper 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.

APPROVE — Claude Code review (Spec 074 T10). Secret-free AuditEvent verified structurally (7 scalar fields, no token/map) + by tests; all 4 resolver lifecycle points + OAuth Complete/Deny audited via SaveActivityAsync; raw upstream body never reaches an audit record (fixed coarse labels, sanitizeCallbackError); go build -tags server + serveredition/storage tests pass; lint clean on changed files. Nits pre-existing. MCP-1043.

@Dumbris
Dumbris merged commit c959a3b into main Jun 20, 2026
47 checks passed
Dumbris added a commit that referenced this pull request Jun 21, 2026
…s (MCP-1045) (#739)

The T12 security review (Codex, request_changes) found two paths that leak
authorization-server-controlled strings into logs and responses, violating the
FR-029/SC-005 'no secret in logs/responses/audit' invariant:

1. The credential connect callback coerced the raw ?error= value only for the
   audit sink, but still logged it verbatim and reflected it into the browser
   redirect's credential_error param. Now the sanitized label is used on all
   three sinks (audit, log, redirect).

2. OAuthConnector.postToken returned the raw token-endpoint response body in its
   error, which is logged on the connect and refresh paths. A hostile/misconfigured
   AS could embed access/refresh tokens or echoed input there. postToken now
   surfaces only the HTTP status plus an allowlisted RFC 6749 §5.2 error code,
   mirroring the RFC 8693 exchanger's sanitizedError.

Adds regression tests covering the redirect/log leak and the token-body leak.

Related #694, Related MCP-1045
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.

2 participants