fix(servers): guard stored credentials and reconcile remote session status

Reject a server address change to a new origin unless a password or
pairing is supplied, so the stored password is never sent to another
host. Test connection now checks the edited values and warns when an
origin change needs credentials. Replay and reconcile remote session
statuses on stream reconnect and browser resync so linked-server
sessions no longer stay busy. Treat malformed server session URLs as
not found instead of throwing.
This commit is contained in:
Chris Scott committed 2026-10-18 21:53:19 -04:00
1 parent 3a7227a602
commit 5586120ba2
18 files changed
+843 -62

No files matched your search

@@ -6,6 +6,7 @@ import { migrate } from '../db/migration-runner'
import { allMigrations } from '../db/migrations'
import { decryptSecret } from '../utils/crypto'
import {
OpenCodeServerCredentialsRequiredError,
OpenCodeServerError,
OpenCodeServerNotFoundError,
OpenCodeServerPairingError,
@@ -255,6 +256,107 @@ describe('OpenCodeServerRegistry', () => {
OpenCodeServerNotFoundError,
)
})
it('rejects an origin change that would reuse the stored password', async () => {
const server = await createServer({
name: 'Studio',
baseUrl: 'https://studio.example.com',
password: 'super-secret',
})
const error = await registry
.update(server.id, { baseUrl: 'https://other-host.example.com' })
.catch((caught: unknown) => caught)
expect(error).toBeInstanceOf(OpenCodeServerCredentialsRequiredError)
expect((error as { status?: number }).status).toBe(400)
expect((error as Error).message).toBe(
'Changing the server address requires re-entering the password or pairing again',
)
expect(decryptSecret(readStoredSecret(db, server.id)!.value)).toBe('super-secret')
expect(registry.get(server.id)?.baseUrl).toBe('https://studio.example.com')
})
it('allows a path change on the same origin without new credentials', async () => {
const server = await createServer({
name: 'Studio',
baseUrl: 'https://studio.example.com',
password: 'super-secret',
})
const updated = await registry.update(server.id, { baseUrl: 'https://studio.example.com/opencode/' })
expect(updated.baseUrl).toBe('https://studio.example.com/opencode')
expect(updated.hasPassword).toBe(true)
expect(decryptSecret(readStoredSecret(db, server.id)!.value)).toBe('super-secret')
})
it('allows an origin change with a new password', async () => {
const server = await createServer({
name: 'Studio',
baseUrl: 'https://studio.example.com',
password: 'super-secret',
})
const updated = await registry.update(server.id, {
baseUrl: 'https://other-host.example.com',
password: 'next-secret',
})
expect(updated.baseUrl).toBe('https://other-host.example.com')
expect(decryptSecret(readStoredSecret(db, server.id)!.value)).toBe('next-secret')
})
it('allows an origin change with an explicit password clear', async () => {
const server = await createServer({
name: 'Studio',
baseUrl: 'https://studio.example.com',
password: 'super-secret',
})
const updated = await registry.update(server.id, {
baseUrl: 'https://other-host.example.com',
password: null,
})
expect(updated.baseUrl).toBe('https://other-host.example.com')
expect(updated.hasPassword).toBe(false)
expect(readStoredSecret(db, server.id)).toBeUndefined()
})
it('allows an origin change when the server has no stored password', async () => {
const server = await createServer({ name: 'Open', baseUrl: 'https://studio.example.com' })
const updated = await registry.update(server.id, { baseUrl: 'https://other-host.example.com' })
expect(updated.baseUrl).toBe('https://other-host.example.com')
expect(updated.hasPassword).toBe(false)
})
it('allows an origin change with pairing credentials', async () => {
fetchFn.mockImplementationOnce(async (_url, init) => {
const { nonce } = JSON.parse(String(init?.body)) as { nonce: string }
return jsonResponse(await encryptOpenCodePairingCredentials({
code: PAIR_CODE,
nonce,
credentials: { username: 'alice', password: 'paired-pass' },
}))
})
const server = await createServer({
name: 'Studio',
baseUrl: 'https://studio.example.com',
password: 'super-secret',
})
const updated = await registry.update(server.id, {
baseUrl: 'https://other-host.example.com',
pairing: { pairUrl: PAIR_URL, code: PAIR_CODE },
})
expect(updated.baseUrl).toBe('https://other-host.example.com')
expect(updated.username).toBe('alice')
expect(decryptSecret(readStoredSecret(db, server.id)!.value)).toBe('paired-pass')
})
})
describe('delete', () => {