Skip to content

fix(hub): harden canvas and agent authorization - #470

Open
alliscode wants to merge 8 commits into
build/hub/newfrom
alliscode-refactored-meme
Open

alliscode wants to merge 8 commits into
build/hub/newfrom
alliscode-refactored-meme

Conversation

@alliscode

@alliscode alliscode commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 595379ed947ae9cb3c5422db17f8bf5040bc7d0b

Dimension Assessment Rationale
Change confidence 8.5/10 ↑ Focused unit, database, migration and extension tests cover each fix, including retried renewals, the pending-token cap, disconnect with a retired token and legacy extensions.
↑ All review threads are addressed, including stack-frame-only error logging; migration SQL matches Prisma's diff.
↑ VS Code and Copilot CLI redirect registrations were observed and pass validation.
↓ The Copilot app's registration was inferred from its published client metadata, not observed.
↓ Migration compatibility tests time out under parallel load locally and pass serially; CI passed at the previous head; this head changes only error-summary logging.
Diagnosis confidence 9/10 ↑ Each finding was checked in code before fixing.
↓ Finding 6 was only partly correct.
Evidence fit 9/10 ↑ Each fix targets the exact lines named in the build/hub/new review and its follow-up review threads.

Why

A review of build/hub/new found authorization, session and audit gaps in Hub before it merges to main. The gaps include canvas pairing revealing tracked repository names, canvas refresh tokens that never rotate, loose agent OAuth checks, and noisy or misleading audit records. This PR fixes them.

What changes

Canvas pairing: Starting a canvas pairing now always succeeds, so it no longer reveals whether Hub tracks a private repository. Hub resolves the repository when the signed-in user opens the approval. An untracked repository and one the user can't read show the same not-available page. A migration stores the requested repository name on pending pairings.

Canvas refresh tokens: Each renewal now issues a new refresh token, and the extension stores it.

  • The current token stays valid until the extension presents a new one. Retried renewals each add a pending token, and all of them stay usable until one is presented, so a lost or late response can't lock the extension out.
  • Pending and replaced token digests live in a new canvas_refresh_tokens table. Presenting a replaced token again, to renew or to disconnect, revokes the connection and records refresh_token_reused in the audit context.
  • A connection holds at most 10 pending tokens. Replaced digests older than 30 days are pruned on renewal, and all of a connection's rows are deleted when it is revoked. Connections that go inactive without being revoked keep their few rows; there is no separate cleanup job.
  • Hub issues renewable connections only to extensions that send X-Replica-Canvas-Refresh-Rotation: 1. Older extensions can't renew and pair again instead.

Unknown credentials: Repeated canvas requests with unknown credentials now write at most one audit row per client and operation each hour.

Agent connections:

  • Code exchange requires redirect_uri and it must match exactly. This is stricter than RFC 6749 when a client omits it from both requests.
  • Refresh takes the client lifecycle lock and the provider, revocation and membership locks, then checks the viewer before rotating.
  • Dynamic client registration accepts only https, loopback http, cursor: and github-app: redirect URIs, without fragments. I checked what clients register:
    • VS Code registers https://vscode.dev/redirect, https://insiders.vscode.dev/redirect, http://127.0.0.1/ and http://127.0.0.1:33418/.
    • The Copilot CLI, probed against a local server, registers http://127.0.0.1:<port>/.
    • The Copilot app's published client metadata lists github-app://oauth/callback and loopback URIs, so github-app: is allowed. I did not observe the app's registration directly.
  • Route errors are logged as name, code and stack only.

Sign-in and audit:

  • Sign-in ends the browser's previous Hub session in its own transaction before replacing the cookie. If ending it is briefly blocked, the callback returns 503 with Retry-After, keeps the old cookie and ends the new session it couldn't deliver.
  • After sign-in, Hub revokes the GitHub user token on a best-effort basis.
  • Authentication proof failures are audited with their real reason.
  • The effective-administrator SQL check is shared from one fragment.
  • hub/docs/authentication.md now limits the "no refresh tokens" statement to browser sessions and describes provider re-sign-in as step-up authentication.

Finding 6 is partly skipped. The bootstrap_attempt_denied audit label is correct, because only the administrator enrollment start route writes that audit. Only the generic rate-limit middleware log was misleading, and it now names the actual operation.

The extension keeps its old 404 pairing message so it still works with older Hubs.

Hide tracked repositories from canvas pairing, rotate canvas refresh
tokens with reuse revocation, and coalesce unknown-credential audits.
Require exact agent OAuth redirect URIs, lock and check viewers before
agent refresh rotation, and validate registered redirect schemes.
End replaced browser sessions, revoke GitHub user tokens after sign-in,
audit authentication proof failures by reason, log redacted agent
connection errors, share the effective-administrator SQL fragment, and
correct authentication docs.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@alliscode
alliscode requested review from a team and nshki as code owners December 1, 2026 22:19
Copilot AI adaptive review requested due to automatic review settings December 1, 2026 22:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Interacting session and refresh-token lifecycle changes require human review of concurrency semantics and compatibility with unobserved Copilot client redirect registrations.

No open issues 🎉

What changed in this PR

This PR hardens Hub’s canvas connections, agent authorization, and browser sign-in before integration into main. It also updates the Copilot canvas extension to retain rotated refresh tokens.

Changes:

  • Defers repository lookup until pairing approval, rotates canvas refresh tokens, and limits repeated unknown-credential audits.
  • Tightens agent OAuth validation, ends previous browser sessions during sign-in, and adds best-effort GitHub token revocation.
  • Shares administrator SQL checks and updates logging, regression coverage, migrations, and authentication documentation.
File Description
hub/​test/​database/​canvas-pairing-names-migration/​index.db.test.ts Tests pairing migration and approval constraints.
hub/​server/​utils/​logging/​summarize-error/​index.ts Summarizes errors without their messages.
hub/​server/​utils/​logging/​summarize-error/​index.test.ts Tests error-summary redaction.
hub/​server/​utils/​effective-administrator-exists/​index.ts Centralizes the administrator SQL predicate.
hub/​server/​routes/​api/​canvas/​index.ts Updates pairing responses and client audit identifiers.
hub/​server/​routes/​api/​canvas/​extension.db.test.ts Covers pairing privacy, rotation, reuse, and audit limits.
hub/​server/​routes/​api/​auth/​types.ts Adds previous-session termination to callback dependencies.
hub/​server/​routes/​api/​auth/​index.test.ts Tests session replacement and contention responses.
hub/​server/​routes/​api/​auth/​create-authentication-route-dependencies/​index.ts Wires session termination and revocation logging.
hub/​server/​routes/​api/​auth/​callback/​get/​index.ts Ends previous sessions before replacing cookies.
hub/​server/​routes/​agent-connections/​index.ts Logs summarized agent-tool errors.
hub/​server/​routes/​agent-connections/​index.db.test.ts Tests refresh rejection and required redirect matching.
hub/​server/​repositories/​sensitive-authorizations/​verify-sensitive-authorization/​index.ts Uses the shared administrator predicate.
hub/​server/​repositories/​sensitive-authorizations/​get-sensitive-authorization/​index.ts Uses the shared administrator predicate.
hub/​server/​repositories/​sensitive-authorizations/​create-sensitive-authorization/​index.ts Uses the shared administrator predicate.
hub/​server/​repositories/​sensitive-authorizations/​cancel-sensitive-authorization/​index.ts Uses the shared administrator predicate.
hub/​server/​repositories/​memberships/​search-administrator-candidates/​index.ts Shares administrator exclusion logic.
hub/​server/​repositories/​memberships/​lock-effective-administrators/​index.ts Shares administrator selection logic.
hub/​server/​repositories/​hub-sessions/​find-administrator-hub-session/​index.ts Shares administrator session checks.
hub/​server/​repositories/​copilot-connection-state/​disconnect-copilot-connection-without-effective-administrator/​index.ts Shares administrator connection checks.
hub/​server/​repositories/​canvas/​revoke-reused-canvas-grant/​index.ts Revokes grants when replaced refresh tokens return.
hub/​server/​repositories/​canvas/​record-audit/​index.ts Records refresh-token reuse reasons.
hub/​server/​repositories/​canvas/​pairings/​index.ts Binds repositories during approval.
hub/​server/​repositories/​canvas/​grants/​index.ts Implements staged refresh-token rotation.
hub/​server/​repositories/​agent-connections/​find-refreshable-agent-connection/​index.ts Finds live connections before refresh rotation.
hub/​server/​integrations/​mcp/​agent-oauth-provider/​index.ts Validates registered redirect URI schemes and fragments.
hub/​server/​integrations/​mcp/​agent-oauth-provider/​index.test.ts Tests accepted and rejected redirect URIs.
hub/​server/​integrations/​mcp/​agent-oauth-provider/​constants.ts Defines permitted loopback hosts and app schemes.
hub/​server/​integrations/​github/​create-user-authentication-adapter/​types.ts Adds optional revocation logging.
hub/​server/​integrations/​github/​create-user-authentication-adapter/​index.ts Revokes GitHub user tokens after use.
hub/​server/​integrations/​github/​create-user-authentication-adapter/​index.test.ts Tests revocation requests and best-effort failures.
hub/​server/​integrations/​express/​authentication-rate-limit/​index.ts Names the actual rate-limited operation in logs.
hub/​server/​integrations/​express/​authentication-rate-limit/​index.test.ts Tests operation-specific logging.
hub/​server/​index.ts Passes the logger into authentication setup.
hub/​server/​index.test.ts Updates authentication dependency expectations.
hub/​server/​domains/​canvas/​pairings/​index.ts Defers repository lookup and limits unknown-device audits.
hub/​server/​domains/​canvas/​connections/​index.ts Returns rotated tokens and handles token reuse.
hub/​server/​domains/​canvas/​audit-unknown-credential/​index.ts Limits unknown-credential audits per client and operation.
hub/​server/​domains/​authentication/​create-github-authentication-context/​index.ts Passes logging into GitHub authentication.
hub/​server/​domains/​authentication/​complete-authentication-proof/​index.ts Audits the actual proof-failure reason.
hub/​server/​domains/​authentication/​complete-authentication-proof/​index.test.ts Tests denial-reason mapping.
hub/​server/​domains/​agent-connections/​create-agent-connections/​index.ts Enforces redirect matching and checks authority before refresh.
hub/​server/​domains/​admission/​authentication-flow.db.test.ts Accounts for GitHub token revocation requests.
hub/​schema/​migrations/​20261229090000_canvas_pairing_names_and_refresh_rotation/​migration.sql Adds pairing names and refresh-rotation storage.
hub/​schema/​canvas-pairing.prisma Stores requested names with optional repository binding.
hub/​schema/​canvas-grant.prisma Adds pending and previous refresh digests.
hub/​docs/​prototype-canvas.md Documents pairing privacy, rotation, and audit limits.
hub/​docs/​learning-foundation.md Documents agent redirect and refresh requirements.
hub/​docs/​authentication.md Clarifies session replacement, revocation, and step-up authentication.
.github/​extensions/​replica-prototype/​create-hub-client/​renewal.test.mjs Tests persistence of rotated refresh credentials.
.github/​extensions/​replica-prototype/​create-hub-client/​index.mts Validates and stores returned refresh tokens.
.github/​extensions/​replica-prototype/​create-hub-client/​index.mjs Updates the runtime client to retain rotated tokens.

Review effort: Balanced (adaptive)

Prototype Author and others added 3 commits December 1, 2026 14:28
Keep the digest of every replaced canvas refresh token so presenting any
of them revokes the grant, not only the most recent one. Allow the GitHub
Copilot app's github-app:// callback, which its published client metadata
lists, and document the redirect URIs that VS Code and the Copilot CLI
register.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved refresh-token lifecycle flaws can leave disconnected connections active, preserve non-rotating credentials, and let late renewal requests invalidate tokens clients have already saved.

7 open issues

Review effort: Max (adaptive)

Prototype Author and others added 3 commits December 1, 2026 16:07
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Store pending and retired refresh tokens in canvas_refresh_tokens, cap pending tokens, prune retired ones, revoke on disconnect with a retired token, and issue renewable connections only to extensions that send X-Replica-Canvas-Refresh-Rotation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants