Allow loopback origins on any port when configured (http://127.0.0.1:*)
An app installed through Even Hub is served by the Even phone app from http://127.0.0.1:<port>, and the port changed between two launches on a real phone (40315, then another). An exact-origin allow-list therefore cannot admit it. HERMEVEN_ALLOWED_ORIGINS now accepts one wildcard form, the port of a loopback address over http (127.0.0.1, localhost, [::1]), off unless configured. No other wildcard is accepted. CORS remains advisory: every route still requires the device credential. Tests: 224 unit/integration. Not reviewed, not deployed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -118,6 +118,69 @@ describe('health, auth and binding', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('loopback any-port origins (the Even app serves an installed app from 127.0.0.1 on a new port each launch)', () => {
|
||||
const preflight = (origin: string) =>
|
||||
h.api('OPTIONS', '/v1/pairing/claim', {
|
||||
headers: { Origin: origin, 'Access-Control-Request-Method': 'POST', 'Access-Control-Request-Headers': 'content-type' },
|
||||
});
|
||||
const allowed = async (origin: string) => (await preflight(origin)).headers.get('access-control-allow-origin');
|
||||
|
||||
it('off by default: a loopback page on another port is not allowed', async () => {
|
||||
h = await createHarness({ env: { HERMEVEN_ALLOWED_ORIGINS: 'http://127.0.0.1:40315' } });
|
||||
expect(await allowed('http://127.0.0.1:40315')).toBe('http://127.0.0.1:40315');
|
||||
expect(await allowed('http://127.0.0.1:40316')).toBeNull();
|
||||
});
|
||||
|
||||
it('with http://127.0.0.1:* configured: any port on 127.0.0.1 is allowed and echoed exactly; nothing else is', async () => {
|
||||
h = await createHarness({ env: { HERMEVEN_ALLOWED_ORIGINS: 'https://app.example, http://127.0.0.1:*' } });
|
||||
for (const origin of ['http://127.0.0.1:40315', 'http://127.0.0.1:51234', 'http://127.0.0.1:1', 'http://127.0.0.1:65535', 'https://app.example']) {
|
||||
expect(await allowed(origin), origin).toBe(origin);
|
||||
}
|
||||
for (const origin of [
|
||||
'http://127.0.0.1', // no port: not what the Even app uses, and not what was configured
|
||||
'https://127.0.0.1:40315', // https was not configured
|
||||
'http://localhost:40315', // a different loopback name, not configured
|
||||
'http://[::1]:40315',
|
||||
'http://127.0.0.2:40315',
|
||||
'http://127.0.0.1.example.com:40315',
|
||||
'http://127.0.0.1:40315.example.com',
|
||||
'http://127.0.0.1:40315@example.com',
|
||||
'http://example.com:40315',
|
||||
'http://192.168.1.20:40315',
|
||||
'http://10.0.0.5:8080',
|
||||
'http://127.0.0.1:0',
|
||||
'http://127.0.0.1:65536',
|
||||
'http://127.0.0.1:abc',
|
||||
'http://127.0.0.1:40315/',
|
||||
'https://evil.example',
|
||||
'null',
|
||||
]) {
|
||||
expect(await allowed(origin), origin).toBeNull();
|
||||
}
|
||||
// The response varies by Origin, so a cache cannot serve one origin's answer to another.
|
||||
expect((await preflight('http://127.0.0.1:40315')).headers.get('vary') ?? '').toMatch(/origin/i);
|
||||
});
|
||||
|
||||
it('being an allowed origin grants nothing: every route still needs the device credential', async () => {
|
||||
h = await createHarness({ env: { HERMEVEN_ALLOWED_ORIGINS: 'http://127.0.0.1:*' } });
|
||||
const origin = { Origin: 'http://127.0.0.1:40315' };
|
||||
expect((await h.api('GET', '/v1/device', { headers: origin })).status).toBe(401);
|
||||
expect((await h.textTurn('hvn_wrong', randomUUID(), 'hi')).status).toBe(401);
|
||||
const cred = await h.pair();
|
||||
const ok = await h.api('GET', '/v1/device', { token: cred.token, headers: origin });
|
||||
expect(ok.status).toBe(200);
|
||||
expect(ok.headers.get('access-control-allow-origin')).toBe('http://127.0.0.1:40315');
|
||||
expect(h.hermes.executions).toBe(0);
|
||||
});
|
||||
|
||||
it('localhost and [::1] can be opted in separately', async () => {
|
||||
h = await createHarness({ env: { HERMEVEN_ALLOWED_ORIGINS: 'http://localhost:*,http://[::1]:*' } });
|
||||
expect(await allowed('http://localhost:5173')).toBe('http://localhost:5173');
|
||||
expect(await allowed('http://[::1]:5173')).toBe('http://[::1]:5173');
|
||||
expect(await allowed('http://127.0.0.1:5173')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('pairing', () => {
|
||||
it('codes are single-use', async () => {
|
||||
h = await createHarness();
|
||||
|
||||
Reference in New Issue
Block a user