CODY-003 Request identity is durable before submission. Storage results are
explicit at the DevicePort boundary (storageSet resolves true only
on the host's own timely confirmation; storageGet distinguishes
unknown from empty). A request is not sent until the phone has
confirmed storing its id; a timeout or refusal holds it back,
unsent and retryable under the same id. Unreadable storage at
start is not treated as unpaired; an unsaveable pairing is revoked.
CODY-006 The state directory is set to 0700 on every open, including when
it already existed, and the bridge refuses to start with an
operator-facing error if it cannot be. Deployment guide creates
it with install -d -m 0700.
Checks: typecheck clean; npm test 194 passed; client build ok;
test:real-stt 11 passed; test:real-hermes 9 passed; test:simulator 6
passed; git diff --check clean.
Not reviewed. No physical-device, droplet or production evidence.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
267 lines
11 KiB
TypeScript
267 lines
11 KiB
TypeScript
import { spawnSync } from 'node:child_process';
|
|
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { CALL_TIMEOUT_MS, EvenDevice } from '../../apps/even-client/src/device/evenAdapter.ts';
|
|
import { BridgeClient, isLoopbackHost, normalizeBridgeUrl } from '../../apps/even-client/src/net/bridgeClient.ts';
|
|
|
|
describe('CODY-001: the bridge address must be HTTPS, or HTTP to loopback only', () => {
|
|
const REMOTE_HTTP = [
|
|
'http://bridge.example.com',
|
|
'http://bridge.example.com:8787/x',
|
|
'http://203.0.113.7:8787', // public
|
|
'http://192.168.1.20:8787', // private LAN
|
|
'http://10.0.0.5',
|
|
'http://172.16.4.4:80',
|
|
'http://100.114.1.2:8787', // carrier-grade / tailnet range
|
|
'http://169.254.1.1',
|
|
'http://[fe80::1]:8787',
|
|
'http://[2001:db8::1]',
|
|
'http://0.0.0.0:8787',
|
|
'http://localhost.example.com', // loopback look-alikes
|
|
'http://127.0.0.1.example.com',
|
|
'http://127.evil.example',
|
|
'http://my-laptop.local:8787',
|
|
];
|
|
|
|
it('rejects every public and private-LAN HTTP address', () => {
|
|
for (const url of REMOTE_HTTP) expect(() => normalizeBridgeUrl(url), url).toThrow(/https/);
|
|
});
|
|
|
|
it('rejects other schemes, credentials in the URL, and non-URLs', () => {
|
|
for (const url of ['ftp://bridge.example', 'ws://bridge.example', 'javascript:alert(1)', 'file:///etc/passwd', 'bridge.example', '']) {
|
|
expect(() => normalizeBridgeUrl(url), url).toThrow();
|
|
}
|
|
expect(() => normalizeBridgeUrl('https://user:pass@bridge.example')).toThrow(/user name or password/);
|
|
});
|
|
|
|
it('accepts HTTPS anywhere and HTTP only on loopback', () => {
|
|
expect(normalizeBridgeUrl(' https://bridge.example.com/ ')).toBe('https://bridge.example.com');
|
|
expect(normalizeBridgeUrl('https://192.168.1.20:8443/p/x/')).toBe('https://192.168.1.20:8443/p/x');
|
|
expect(normalizeBridgeUrl('http://127.0.0.1:8787')).toBe('http://127.0.0.1:8787');
|
|
expect(normalizeBridgeUrl('http://localhost:8787/')).toBe('http://localhost:8787');
|
|
expect(normalizeBridgeUrl('http://[::1]:8787')).toBe('http://[::1]:8787');
|
|
expect(normalizeBridgeUrl('http://127.8.9.10')).toBe('http://127.8.9.10');
|
|
expect(isLoopbackHost('LOCALHOST')).toBe(false); // URL parsing lower-cases; raw input is not trusted
|
|
});
|
|
|
|
it('the HTTP client itself refuses a remote HTTP base URL, so no code path can bypass the check', () => {
|
|
expect(() => new BridgeClient({ baseUrl: 'http://192.168.1.20:8787' })).toThrow(/https/);
|
|
expect(() => new BridgeClient({ baseUrl: 'https://bridge.example.com' })).not.toThrow();
|
|
});
|
|
|
|
describe('manifest generation', () => {
|
|
const script = fileURLToPath(new URL('../../apps/even-client/scripts/write-manifest.mjs', import.meta.url));
|
|
let dir: string;
|
|
beforeEach(() => {
|
|
dir = mkdtempSync(join(tmpdir(), 'hermeven-manifest-'));
|
|
});
|
|
afterEach(() => rmSync(dir, { recursive: true, force: true }));
|
|
const run = (origin: string) =>
|
|
spawnSync(process.execPath, [script], {
|
|
env: { ...process.env, HERMEVEN_BRIDGE_ORIGIN: origin, HERMEVEN_MANIFEST_OUT: join(dir, 'app.json') },
|
|
encoding: 'utf8',
|
|
});
|
|
|
|
it('exits nonzero and writes nothing for remote HTTP origins', () => {
|
|
for (const origin of ['http://bridge.example.com', 'http://192.168.1.20:8787', 'http://10.0.0.5', 'http://localhost.example.com']) {
|
|
const res = run(origin);
|
|
expect(res.status, origin).toBe(1);
|
|
expect(res.stderr).toContain('https://');
|
|
expect(existsSync(join(dir, 'app.json')), origin).toBe(false);
|
|
}
|
|
});
|
|
|
|
it('writes the whitelist for HTTPS, and for loopback HTTP with a warning', () => {
|
|
const https = run('https://bridge.example.com');
|
|
expect(https.status).toBe(0);
|
|
const manifest = JSON.parse(readFileSync(join(dir, 'app.json'), 'utf8'));
|
|
expect(manifest.permissions[0]).toMatchObject({ name: 'network', whitelist: ['https://bridge.example.com'] });
|
|
const loopback = run('http://127.0.0.1:8787');
|
|
expect(loopback.status).toBe(0);
|
|
expect(loopback.stderr).toContain('simulator only');
|
|
});
|
|
|
|
it('still refuses origins with a path', () => {
|
|
expect(run('https://bridge.example.com/api').status).toBe(1);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('CODY-002: host calls stay serialised and a late microphone start ends closed', () => {
|
|
type Call = { name: string; args: unknown[]; resolve: (value: unknown) => void; settled: boolean };
|
|
|
|
/** A stand-in for the Even host whose calls settle only when the test says so. */
|
|
function fakeHost() {
|
|
const calls: Call[] = [];
|
|
let inFlight = 0;
|
|
let maxInFlight = 0;
|
|
let micOpen = false;
|
|
const call = (name: string, args: unknown[], onSettle?: (value: unknown) => void) =>
|
|
new Promise((resolve) => {
|
|
inFlight += 1;
|
|
maxInFlight = Math.max(maxInFlight, inFlight);
|
|
const entry: Call = {
|
|
name,
|
|
args,
|
|
settled: false,
|
|
resolve: (value) => {
|
|
entry.settled = true;
|
|
inFlight -= 1;
|
|
onSettle?.(value);
|
|
resolve(value);
|
|
},
|
|
};
|
|
calls.push(entry);
|
|
});
|
|
const bridge = {
|
|
createStartUpPageContainer: (...a: unknown[]) => call('create', a),
|
|
textContainerUpgrade: (...a: unknown[]) => call('text', a),
|
|
audioControl: (open: boolean, ...rest: unknown[]) =>
|
|
call(open ? 'mic_on' : 'mic_off', [open, ...rest], (ok) => {
|
|
if (ok) micOpen = open;
|
|
}),
|
|
shutDownPageContainer: (...a: unknown[]) => call('exit', a),
|
|
getLocalStorage: (...a: unknown[]) => call('get', a),
|
|
setLocalStorage: (...a: unknown[]) => call('set', a),
|
|
};
|
|
return { bridge, calls, micOpen: () => micOpen, maxInFlight: () => maxInFlight, pending: () => calls.filter((c) => !c.settled) };
|
|
}
|
|
const tick = async () => {
|
|
for (let i = 0; i < 10; i++) await Promise.resolve();
|
|
};
|
|
|
|
beforeEach(() => vi.useFakeTimers());
|
|
afterEach(() => vi.useRealTimers());
|
|
|
|
it('timeout, then the host opens the microphone late: the microphone ends closed and calls never overlap', async () => {
|
|
const host = fakeHost();
|
|
const device = new EvenDevice(host.bridge as never);
|
|
|
|
const starting = device.startMic();
|
|
await tick();
|
|
expect(host.pending().map((c) => c.name)).toEqual(['mic_on']);
|
|
|
|
// The host does not answer within the caller's timeout.
|
|
await vi.advanceTimersByTimeAsync(CALL_TIMEOUT_MS + 1);
|
|
expect(await starting).toBe(false); // the app is told the microphone did not open
|
|
|
|
// Nothing else has been sent to the host while the start is still pending.
|
|
expect(host.calls.map((c) => c.name)).toEqual(['mic_on']);
|
|
expect(host.maxInFlight()).toBe(1);
|
|
|
|
// Now the late completion arrives: the host really did open the microphone.
|
|
host.pending()[0]!.resolve(true);
|
|
await tick();
|
|
expect(host.micOpen()).toBe(true);
|
|
// The compensating stop goes out next, strictly after the start settled.
|
|
expect(host.calls.map((c) => c.name)).toEqual(['mic_on', 'mic_off']);
|
|
host.pending()[0]!.resolve(true);
|
|
await tick();
|
|
expect(host.micOpen()).toBe(false);
|
|
expect(host.maxInFlight()).toBe(1);
|
|
expect(host.pending()).toEqual([]);
|
|
});
|
|
|
|
it('a stop requested while the start is still pending waits its turn and still leaves the microphone closed', async () => {
|
|
const host = fakeHost();
|
|
const device = new EvenDevice(host.bridge as never);
|
|
const starting = device.startMic();
|
|
await tick();
|
|
const stopping = device.stopMic(); // user cancelled while the microphone was opening
|
|
device.show({ body: 'x', status: 'y' });
|
|
await tick();
|
|
expect(host.calls.map((c) => c.name)).toEqual(['mic_on']); // stop is queued, not sent alongside
|
|
|
|
host.pending()[0]!.resolve(true);
|
|
await tick();
|
|
expect(await starting).toBe(true);
|
|
expect(host.calls.map((c) => c.name)).toEqual(['mic_on', 'mic_off']);
|
|
host.pending()[0]!.resolve(true);
|
|
await stopping;
|
|
expect(host.micOpen()).toBe(false);
|
|
expect(host.maxInFlight()).toBe(1);
|
|
});
|
|
|
|
it('a start that times out and then fails late leaves nothing open and the queue usable', async () => {
|
|
const host = fakeHost();
|
|
const device = new EvenDevice(host.bridge as never);
|
|
const starting = device.startMic();
|
|
await vi.advanceTimersByTimeAsync(CALL_TIMEOUT_MS + 1);
|
|
expect(await starting).toBe(false);
|
|
host.pending()[0]!.resolve(false); // the host refused after all
|
|
await tick();
|
|
host.pending()[0]!.resolve(true); // the compensating stop
|
|
await tick();
|
|
expect(host.micOpen()).toBe(false);
|
|
|
|
// A later start works normally.
|
|
const again = device.startMic();
|
|
await tick();
|
|
host.pending()[0]!.resolve(true);
|
|
expect(await again).toBe(true);
|
|
expect(host.micOpen()).toBe(true);
|
|
expect(host.maxInFlight()).toBe(1);
|
|
});
|
|
|
|
it('many calls behind a stuck one each time out for their caller but are sent one at a time, in order', async () => {
|
|
const host = fakeHost();
|
|
const device = new EvenDevice(host.bridge as never);
|
|
const a = device.storageGet('a');
|
|
const b = device.storageSet('b', '1');
|
|
const c = device.requestExit();
|
|
await tick();
|
|
expect(host.calls.map((x) => x.name)).toEqual(['get']);
|
|
await vi.advanceTimersByTimeAsync(CALL_TIMEOUT_MS + 1);
|
|
expect(await a).toEqual({ ok: false }); // bounded for the caller, and reported as unknown
|
|
await b;
|
|
await c;
|
|
expect(host.calls.map((x) => x.name)).toEqual(['get']); // still only the stuck call at the host
|
|
host.pending()[0]!.resolve('late');
|
|
await tick();
|
|
expect(host.calls.map((x) => x.name)).toEqual(['get', 'set']);
|
|
host.pending()[0]!.resolve(true);
|
|
await tick();
|
|
expect(host.calls.map((x) => x.name)).toEqual(['get', 'set', 'exit']);
|
|
expect(host.maxInFlight()).toBe(1);
|
|
});
|
|
|
|
it('storage: only a timely `true` from the host is success; a timeout or `false` is "not confirmed", a stalled read is "unknown"', async () => {
|
|
const host = fakeHost();
|
|
const device = new EvenDevice(host.bridge as never);
|
|
|
|
const confirmed = device.storageSet('k', 'v');
|
|
await tick();
|
|
host.pending()[0]!.resolve(true);
|
|
expect(await confirmed).toBe(true);
|
|
|
|
const refused = device.storageSet('k', 'v');
|
|
await tick();
|
|
host.pending()[0]!.resolve(false);
|
|
expect(await refused).toBe(false);
|
|
|
|
const stalled = device.storageSet('k', 'v');
|
|
await vi.advanceTimersByTimeAsync(CALL_TIMEOUT_MS + 1);
|
|
expect(await stalled).toBe(false); // the wait ran out; that is not a successful write
|
|
host.pending()[0]!.resolve(true); // ...even though the host completes it later
|
|
await tick();
|
|
|
|
const read = device.storageGet('k');
|
|
await tick();
|
|
host.pending()[0]!.resolve('value');
|
|
expect(await read).toEqual({ ok: true, value: 'value' });
|
|
|
|
const empty = device.storageGet('k');
|
|
await tick();
|
|
host.pending()[0]!.resolve('');
|
|
expect(await empty).toEqual({ ok: true, value: '' });
|
|
|
|
const unknown = device.storageGet('k');
|
|
await vi.advanceTimersByTimeAsync(CALL_TIMEOUT_MS + 1);
|
|
expect(await unknown).toEqual({ ok: false });
|
|
expect(host.maxInFlight()).toBe(1);
|
|
});
|
|
});
|