Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
dc9a0c5
Fail over to the local daemon when the configured server is unreachable
lawrencecchen Jul 28, 2026
f271cf4
Add sr setup, doctor and cleanup, and move lifecycle under sr server
lawrencecchen Jul 28, 2026
9f7e236
Auto-start the local daemon when launching an agent
lawrencecchen Jul 28, 2026
f99083f
Add team-vault credential leases with local egress
lawrencecchen Jul 29, 2026
73498c1
Merge remote-tracking branch 'origin/main' into feat/client-failover
lawrencecchen Jul 29, 2026
61a0481
Test credential storage mode overrides legacy servers
lawrencecchen Jul 29, 2026
10fa100
Make credential storage mode authoritative
lawrencecchen Jul 29, 2026
b66d411
Test lifecycle waits for daemon readiness
lawrencecchen Jul 29, 2026
2a09316
Wait for daemon health after lifecycle starts
lawrencecchen Jul 29, 2026
8895763
Isolate doctor tests from live team config
lawrencecchen Jul 29, 2026
fc1bbf6
Test credential custody review regressions
lawrencecchen Jul 29, 2026
5d5c726
Harden central credential custody and local egress
lawrencecchen Jul 29, 2026
99b1e63
Test daemon custody and lease lifecycle regressions
lawrencecchen Jul 29, 2026
1dbb4c6
Harden local credential proxy custody
lawrencecchen Jul 29, 2026
6f7f586
Test credential failover review regressions
lawrencecchen Jul 29, 2026
0392925
Harden credential failover and provider custody
lawrencecchen Jul 29, 2026
7ac1cee
Allow explicit proxy health recovery probes
lawrencecchen Jul 29, 2026
d90bde9
Test device authorization client binding
lawrencecchen Jul 29, 2026
3bfec39
Bind Subrouter login to its device authorization
lawrencecchen Jul 29, 2026
298a336
Test systemd path escaping
lawrencecchen Jul 29, 2026
dfa19a2
Escape systemd user service paths
lawrencecchen Jul 29, 2026
1c714fc
Add regression coverage for cloud credential boundaries
lawrencecchen Jul 29, 2026
8ab2695
Harden local proxy and cloud credential recovery
lawrencecchen Jul 29, 2026
953c310
Test stale credential lease outcomes
lawrencecchen Jul 29, 2026
7da74c6
Ignore stale credential lease outcomes
lawrencecchen Jul 29, 2026
0be9500
Keep Go module metadata tidy
lawrencecchen Jul 29, 2026
8f3b799
Test central adoption and monotonic cooldowns
lawrencecchen Jul 29, 2026
7ecab5c
Adopt uploaded refresh chains and preserve cooldowns
lawrencecchen Jul 29, 2026
7916574
Test team-vault failure isolation and cleanup
lawrencecchen Jul 29, 2026
7c9892e
Harden team leases and scoped failover
lawrencecchen Jul 29, 2026
b888dae
Test quota-scoped forbidden lease rotation
lawrencecchen Jul 29, 2026
1bb42c5
Rotate model-scoped forbidden leases
lawrencecchen Jul 29, 2026
cd8c320
Test provider-specific forbidden lease handling
lawrencecchen Jul 29, 2026
e21eec6
Classify forbidden leases by provider context
lawrencecchen Jul 29, 2026
446c833
Test Claude API-key forbidden scope
lawrencecchen Jul 29, 2026
ba3da7f
Scope Claude forbidden leases by auth mode
lawrencecchen Jul 29, 2026
03b24b0
Test provider-specific lease retry deadlines
lawrencecchen Jul 29, 2026
2856789
Use provider-specific lease retry deadlines
lawrencecchen Jul 29, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cloudflare/packages/worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ curl -sS -X DELETE https://subrouter.cmux.dev/tenant/accounts/$ACCOUNT_ID \
OAuth uploads schedule the existing Durable Object alarm refresh path. Rotated
refresh tokens are persisted in the tenant DO.

## Local Egress Credential Leases

`POST /tenant/leases` selects a team account, refreshes it centrally when
needed, and returns an access-only five-minute lease. The response never
contains a provider refresh token or ID token. The caller sends the provider
request from its own machine, then reports the result to
`POST /tenant/leases/:id/events`.

`POST /tenant/accounts?adopt=1` and the matching repair route force one central
OAuth refresh before acknowledging the credential. This rotates refresh
custody away from the source laptop without making a model or usage request
from Cloudflare.

Refreshes are single-flight per account. A response that proves the refresh
chain invalid, or leaves rotation ambiguous after a transport or parse failure,
freezes the account until `POST /tenant/accounts/:id/repair` replaces it.
Blocked accounts are removed from routing, so another healthy team credential
can serve the lease.

## Admin Tenant Data

Tenant-scoped admin reads require an explicit `?tenant=<id>`:
Expand Down
65 changes: 56 additions & 9 deletions cloudflare/packages/worker/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface AccountCredentials {
readonly tokenEndpoint?: string
readonly clientId?: string
readonly lastRefreshAt?: number
readonly credentialGeneration?: number
readonly refreshFailure?: OAuthRefreshFailure
readonly usageUrl?: string
readonly [key: string]: unknown
Expand Down Expand Up @@ -581,7 +582,7 @@ export const refreshOAuthCredentials = async (
if (!force && !credentialNeedsRefresh(credentials, now)) {
return { credentials, refreshed: false }
}
if (credentials.refreshFailure && terminalRefreshFailure(credentials.refreshFailure)) {
if (credentials.refreshFailure && blockingRefreshFailure(credentials.refreshFailure)) {
throw new Error(credentials.refreshFailure.message)
}
if (providerForAccount(kind) === "claude") {
Expand Down Expand Up @@ -625,6 +626,7 @@ const refreshCodexCredentials = async (
idToken: payload.id_token,
...(expiresAt !== undefined ? { expiresAt } : {}),
lastRefreshAt: now,
credentialGeneration: (credentials.credentialGeneration ?? 0) + 1,
},
refreshed: true,
}
Expand Down Expand Up @@ -669,6 +671,7 @@ const refreshClaudeCredentials = async (
: {}),
...(expiresAt !== undefined ? { expiresAt } : {}),
lastRefreshAt: now,
credentialGeneration: (credentials.credentialGeneration ?? 0) + 1,
},
refreshed: true,
}
Expand All @@ -680,33 +683,62 @@ const parseRefreshJSON = (body: string): Record<string, unknown> & {
id_token?: string
expires_in?: number
} => {
const payload = JSON.parse(body) as Record<string, unknown>
return payload
try {
return JSON.parse(body) as Record<string, unknown>
} catch {
// JSON parser errors may quote the provider body. Never let an echoed
// credential become part of the persisted refresh-failure record.
throw new Error("OAuth refresh response was not valid JSON")
}
}

const refreshError = (status: number, body: string): Error => {
let message = body.trim()
let message = "provider rejected the refresh"
let providerCode: string | undefined
let providerType: string | undefined
try {
const payload = JSON.parse(body) as {
error?: { message?: string; code?: string; type?: string } | string
}
if (typeof payload.error === "string") {
message = payload.error
providerCode = sanitizedTerminalRefreshCode(payload.error)
message = providerCode ?? message
} else if (payload.error) {
message = payload.error.message ?? message
providerCode = payload.error.code
providerType = payload.error.type
providerCode =
sanitizedTerminalRefreshCode(
providerCode,
providerType,
payload.error.message,
) ?? providerCode
message = providerCode ?? providerType ?? message
}
} catch {
// Keep the raw provider body in the error message.
// Provider response bodies can contain echoed credentials. Status is enough
// to diagnose this failure without persisting an untrusted body.
}
const error = new Error(`OAuth refresh failed (${status}): ${message}`)
Object.assign(error, { status, providerCode, providerType })
return error
}

const sanitizedTerminalRefreshCode = (
...values: ReadonlyArray<string | undefined>
): string | undefined => {
const combined = values.filter(Boolean).join(" ").toLowerCase()
if (combined.includes("invalid_grant") || combined.includes("invalid grant")) {
return "invalid_grant"
}
if (
combined.includes("refresh_token") ||
combined.includes("refresh token")
) {
return "invalid_refresh_token"
}
return undefined
}

export const refreshFailureFromError = (
error: unknown,
now = Date.now()
Expand All @@ -724,8 +756,23 @@ export const refreshFailureFromError = (
export const terminalRefreshFailure = (failure: OAuthRefreshFailure): boolean => {
if (failure.status !== 400 && failure.status !== 401) return false
const combined = `${failure.providerCode ?? ""} ${failure.message}`.toLowerCase()
return combined.includes("refresh_token") || combined.includes("refresh token")
}
return (
combined.includes("invalid_grant") ||
combined.includes("invalid grant") ||
combined.includes("refresh_token") ||
combined.includes("refresh token")
)
}

// A transport failure or malformed success response is ambiguous: the provider
// may have rotated the refresh token before the response was lost. Retrying that
// old token can invalidate the only recoverable credential chain, so central
// refresh stops and asks for repair instead of guessing.
export const uncertainRefreshFailure = (failure: OAuthRefreshFailure): boolean =>
failure.status === 0

export const blockingRefreshFailure = (failure: OAuthRefreshFailure): boolean =>
terminalRefreshFailure(failure) || uncertainRefreshFailure(failure)

export const jwtExpiryMillis = (token: string | undefined): number | null => {
if (!token) return null
Expand Down
Loading