Files
dsh-web/shared/tests/pair-access.spec.ts
zhu1090093659 e7c74daefa refactor(shared): consolidate the pairing trust fence into shared/host/pair-access.ts
Git-graph, pet, and skill-explorer carried byte-identical fence logic.
The decision now lives in one shared source distributed as generated
sync-shared copies; each package keeps a self-describing thin wrapper.
Canonical tests added under shared/tests; wrapper specs stay as wiring
tests.
2026-08-27 01:20:12 +08:00

66 lines
2.3 KiB
TypeScript

/**
* Canonical tests for the shared pairing trust fence (shared/host/pair-access.ts);
* each consuming package keeps a small wrapper spec for its own wiring.
*/
import type { IncomingMessage } from 'node:http'
import { describe, expect, it, vi } from 'vitest'
import { isPairedOrLoopbackAllowed } from '../host/pair-access.ts'
function request(options: {
remoteAddress?: string
host?: string
cookie?: string
} = {}): IncomingMessage {
return {
headers: {
host: options.host ?? '127.0.0.1:3000',
...(options.cookie === undefined ? {} : { cookie: options.cookie }),
},
socket: { remoteAddress: options.remoteAddress ?? '127.0.0.1' },
} as IncomingMessage
}
describe('isPairedOrLoopbackAllowed', () => {
it('allows loopback without a pairing service', () => {
expect(isPairedOrLoopbackAllowed({} as never, request())).toBe(true)
})
it('refuses a LAN client when no pairing service is present', () => {
const ctx = { get: () => undefined }
expect(isPairedOrLoopbackAllowed(ctx as never, request({
remoteAddress: '192.168.1.20',
host: 'dsh.example:443',
}))).toBe(false)
})
it('allows a LAN client with a live paired-device cookie', () => {
const isPairedDevice = vi.fn(() => true)
const ctx = { get: (name: string) => name === 'remoteWebUiPairing' ? { isPairedDevice } : undefined }
const req = request({
remoteAddress: '192.168.1.20',
host: 'dsh.example:443',
cookie: 'dsh_pair=dev-1',
})
expect(isPairedOrLoopbackAllowed(ctx as never, req)).toBe(true)
expect(isPairedDevice).toHaveBeenCalledWith(req)
})
it('refuses a LAN client when pairing returns false (revoked or unknown)', () => {
const ctx = { get: () => ({ isPairedDevice: () => false }) }
expect(isPairedOrLoopbackAllowed(ctx as never, request({
remoteAddress: '192.168.1.20',
host: 'dsh.example:443',
cookie: 'dsh_pair=revoked',
}))).toBe(false)
})
it('falls back to the direct remoteWebUiPairing property when ctx.get is absent', () => {
const ctx = { remoteWebUiPairing: { isPairedDevice: () => true } }
expect(isPairedOrLoopbackAllowed(ctx as never, request({
remoteAddress: '192.168.1.20',
host: 'dsh.example:443',
cookie: 'dsh_pair=dev-1',
}))).toBe(true)
})
})