feat(servers): harden pairing and run linked-server sessions in the UI

Replace the plaintext pairing exchange with a nonce and HMAC proof plus
AES-256-GCM encrypted credentials, bumping links to v2. Track session
ownership per linked server so activity, session picker, transcript, MCP
and skills views work against remote servers. Share encrypted secret
access, event routing and URL helpers across backend, shared and CLI.
This commit is contained in:
Chris Scott committed 2026-10-18 20:27:34 -04:00
1 parent c591c15743
commit f566556fd6
77 files changed
+3170 -702

No files matched your search

+22 -4
View File
@@ -1,4 +1,7 @@
import { OpenCode } from '@opencode/client'
import { OPENCODE_SERVER_USERNAME } from '../schemas/opencode-servers'
export { OPENCODE_SERVER_USERNAME }
export type {
AgentInfo,
@@ -92,6 +95,23 @@ export type {
McpTimeoutConfig,
} from './mcp'
export {
createOpenCodePairingNonce,
createOpenCodePairingProof,
decryptOpenCodePairingCredentials,
encryptOpenCodePairingCredentials,
isOpenCodePairingCredentials,
isOpenCodePairingNonce,
verifyOpenCodePairingProof,
} from './pairing'
export type {
OpenCodePairingCredentials,
OpenCodePairingDecryptOptions,
OpenCodePairingEncryptedCredentials,
OpenCodePairingKeyOptions,
} from './pairing'
export {
OPENCODE_PINNED_VERSION,
OPENCODE_SUPPORTED_VERSION_RANGE,
@@ -109,8 +129,6 @@ export { OPENCODE_LIFECYCLE_STATES, OPENCODE_RECOVERY_ACTIONS } from './lifecycl
export type { OpenCodeLifecycleState, OpenCodeLifecycleStatus, OpenCodeRecoveryAction } from './lifecycle'
export const OPENCODE_SERVER_USERNAME = 'opencode'
export type OpenCodeApi = ReturnType<typeof OpenCode.make>
export interface OpenCodeApiOptions {
@@ -120,8 +138,8 @@ export interface OpenCodeApiOptions {
headers?: Record<string, string>
}
export function buildOpenCodeBasicAuth(password: string): string {
const bytes = new TextEncoder().encode(`${OPENCODE_SERVER_USERNAME}:${password}`)
export function buildOpenCodeBasicAuth(password: string, username: string = OPENCODE_SERVER_USERNAME): string {
const bytes = new TextEncoder().encode(`${username}:${password}`)
let binary = ''
for (const byte of bytes) binary += String.fromCharCode(byte)
return `Basic ${btoa(binary)}`
+168
View File
@@ -0,0 +1,168 @@
const PAIRING_PROTOCOL = 'ocm-pair-v2'
const PAIRING_PROOF_PREFIX = `${PAIRING_PROTOCOL}:`
const PAIRING_KEY_INFO = `${PAIRING_PROTOCOL} credentials`
const PAIRING_NONCE_BYTES = 32
const PAIRING_IV_BYTES = 12
const PAIRING_KEY_BITS = 256
const textEncoder = new TextEncoder()
const textDecoder = new TextDecoder()
type WebCryptoKey = Awaited<ReturnType<typeof globalThis.crypto.subtle.importKey>>
export interface OpenCodePairingCredentials {
username: string
password: string
}
export interface OpenCodePairingEncryptedCredentials {
iv: string
ciphertext: string
}
export interface OpenCodePairingKeyOptions {
code: string
nonce: string
}
export interface OpenCodePairingDecryptOptions extends OpenCodePairingKeyOptions {
iv: string
ciphertext: string
}
/** Generates a fresh 32-byte pairing nonce encoded as base64url. */
export function createOpenCodePairingNonce(): string {
return base64UrlEncode(globalThis.crypto.getRandomValues(new Uint8Array(PAIRING_NONCE_BYTES)))
}
/** Reports whether a value is a base64url-encoded 32-byte pairing nonce. */
export function isOpenCodePairingNonce(value: unknown): value is string {
if (typeof value !== 'string') return false
const bytes = base64UrlDecode(value)
return bytes !== null && bytes.length === PAIRING_NONCE_BYTES
}
/** Reports whether a value carries non-empty string credentials. */
export function isOpenCodePairingCredentials(value: unknown): value is OpenCodePairingCredentials {
if (typeof value !== 'object' || value === null) return false
const { username, password } = value as { username?: unknown; password?: unknown }
return typeof username === 'string' && username.length > 0 && typeof password === 'string'
}
/** Computes the base64url HMAC-SHA256 proof that binds a pairing code to a nonce. */
export async function createOpenCodePairingProof(code: string, nonce: string): Promise<string> {
const key = await importHmacKey(code)
const signature = await globalThis.crypto.subtle.sign('HMAC', key, pairingProofMessage(nonce))
return base64UrlEncode(new Uint8Array(signature))
}
/** Verifies a pairing proof in constant time through the Web Crypto HMAC verifier. */
export async function verifyOpenCodePairingProof(code: string, nonce: string, proof: string): Promise<boolean> {
const signature = base64UrlDecode(proof)
if (signature === null) return false
const key = await importHmacKey(code)
try {
return await globalThis.crypto.subtle.verify('HMAC', key, signature, pairingProofMessage(nonce))
} catch {
return false
}
}
/** Encrypts pairing credentials with AES-256-GCM under a key derived from the code and nonce. */
export async function encryptOpenCodePairingCredentials(
options: OpenCodePairingKeyOptions & { credentials: OpenCodePairingCredentials },
): Promise<OpenCodePairingEncryptedCredentials> {
const key = await deriveCredentialsKey(options.code, options.nonce)
const iv = globalThis.crypto.getRandomValues(new Uint8Array(PAIRING_IV_BYTES))
const plaintext = encodeText(JSON.stringify({
username: options.credentials.username,
password: options.credentials.password,
}))
const ciphertext = await globalThis.crypto.subtle.encrypt(
{ name: 'AES-GCM', iv, additionalData: pairingAdditionalData() },
key,
plaintext,
)
return {
iv: base64UrlEncode(iv),
ciphertext: base64UrlEncode(new Uint8Array(ciphertext)),
}
}
/** Decrypts pairing credentials, throwing when the payload is tampered with or malformed. */
export async function decryptOpenCodePairingCredentials(
options: OpenCodePairingDecryptOptions,
): Promise<OpenCodePairingCredentials> {
const iv = base64UrlDecode(options.iv)
const ciphertext = base64UrlDecode(options.ciphertext)
if (iv === null || iv.length !== PAIRING_IV_BYTES || ciphertext === null) {
throw new Error('Invalid pairing payload')
}
const key = await deriveCredentialsKey(options.code, options.nonce)
const plaintext = await globalThis.crypto.subtle.decrypt(
{ name: 'AES-GCM', iv, additionalData: pairingAdditionalData() },
key,
ciphertext,
)
const parsed: unknown = JSON.parse(textDecoder.decode(plaintext))
if (!isOpenCodePairingCredentials(parsed)) throw new Error('Invalid pairing payload')
return parsed
}
function encodeText(value: string): Uint8Array<ArrayBuffer> {
return new Uint8Array(textEncoder.encode(value))
}
function pairingProofMessage(nonce: string): Uint8Array<ArrayBuffer> {
return encodeText(`${PAIRING_PROOF_PREFIX}${nonce}`)
}
function pairingAdditionalData(): Uint8Array<ArrayBuffer> {
return encodeText(PAIRING_PROTOCOL)
}
async function importHmacKey(code: string): Promise<WebCryptoKey> {
return globalThis.crypto.subtle.importKey(
'raw',
encodeText(code),
{ name: 'HMAC', hash: 'SHA-256' },
false,
['sign', 'verify'],
)
}
async function deriveCredentialsKey(code: string, nonce: string): Promise<WebCryptoKey> {
const keyMaterial = await globalThis.crypto.subtle.importKey('raw', encodeText(code), 'HKDF', false, ['deriveKey'])
return globalThis.crypto.subtle.deriveKey(
{
name: 'HKDF',
hash: 'SHA-256',
salt: encodeText(nonce),
info: encodeText(PAIRING_KEY_INFO),
},
keyMaterial,
{ name: 'AES-GCM', length: PAIRING_KEY_BITS },
false,
['encrypt', 'decrypt'],
)
}
function base64UrlEncode(bytes: Uint8Array): string {
let binary = ''
for (const byte of bytes) binary += String.fromCharCode(byte)
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
}
function base64UrlDecode(value: string): Uint8Array<ArrayBuffer> | null {
if (!/^[A-Za-z0-9_-]+={0,2}$/.test(value)) return null
const base64 = value.replace(/-/g, '+').replace(/_/g, '/').replace(/=+$/, '')
const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4)
try {
const binary = atob(padded)
const bytes = new Uint8Array(binary.length)
for (let index = 0; index < binary.length; index++) bytes[index] = binary.charCodeAt(index)
return bytes
} catch {
return null
}
}