Files
Chris Scott f8e4cfe136 feat(device-pairing): pair phones and browsers with a QR code
Settings -> Account gets "Connect a device", a paired-devices list, and a
remote access headers editor, so the mobile app and other browsers can sign
in without typing a password and pass a reverse proxy such as Cloudflare
Access.

- one-time pairing codes (3 min TTL) minted by a Better Auth plugin and
  redeemed into a new session that records the device name
- POST /api/device-pairing/links builds the ocm://link app link with the
  stored headers as h= parameters and a browser /pair link; plain-HTTP and
  loopback origins are blocked or warned about
- remote access headers are stored encrypted, returned by name only, and
  validated (8 headers, 512-char values, 1800 encoded characters, reserved
  transport names blocked); a Cloudflare Access service token preset fills
  CF-Access-Client-Id and CF-Access-Client-Secret
- login offers "Sign in with QR code" and /pair redeems a scanned code,
  returning to the login redirect target
- revoking a device deletes its session, closes its live connections and
  removes its push subscriptions; the current session cannot be revoked
2026-10-24 19:26:21 -04:00

39 lines
1.6 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { getTrustedOrigins, isTrustedOrigin } from '@opencode-manager/shared/config/env'
const TRUSTED = ['https://m.example.com', 'http://localhost:5173'] as const
describe('isTrustedOrigin', () => {
it('accepts a listed origin', () => {
expect(isTrustedOrigin('https://m.example.com', {}, TRUSTED)).toBe(true)
expect(isTrustedOrigin('https://m.example.com', { requireOrigin: true }, TRUSTED)).toBe(true)
})
it('rejects an unlisted origin', () => {
expect(isTrustedOrigin('https://evil.example', {}, TRUSTED)).toBe(false)
expect(isTrustedOrigin('https://evil.example', { requireOrigin: true }, TRUSTED)).toBe(false)
})
it('treats a missing origin as trusted unless an origin is required', () => {
expect(isTrustedOrigin(undefined, {}, TRUSTED)).toBe(true)
expect(isTrustedOrigin(undefined, { requireOrigin: true }, TRUSTED)).toBe(false)
})
it('treats an empty origin as missing', () => {
expect(isTrustedOrigin('', {}, TRUSTED)).toBe(true)
expect(isTrustedOrigin('', { requireOrigin: true }, TRUSTED)).toBe(false)
})
it('treats the null origin as untrusted', () => {
expect(isTrustedOrigin('null', {}, TRUSTED)).toBe(false)
expect(isTrustedOrigin('null', { requireOrigin: true }, [...TRUSTED, 'null'])).toBe(false)
})
it('reads the configured origins by default', () => {
const configured = getTrustedOrigins()
expect(configured.length).toBeGreaterThan(0)
expect(isTrustedOrigin(configured[0])).toBe(true)
expect(isTrustedOrigin('https://not-configured.example')).toBe(false)
})
})