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:
Dan
2026-11-21 13:31:40 -08:00
co-authored by Claude Fable 5.2
parent fd992559af
commit bc00ae9fb1
6 changed files with 140 additions and 7 deletions
+25 -2
View File
@@ -9,6 +9,12 @@ export interface BridgeConfig {
dbPath: string;
/** Exact browser origins allowed by CORS. Empty = no cross-origin browser access. */
allowedOrigins: string[];
/**
* Loopback hosts whose pages are allowed from ANY port (`http://127.0.0.1:*`). Needed because
* the Even phone app serves an installed app from `http://127.0.0.1:<port>` and picks a new
* port on every launch (observed on a real phone, 2026-11-21). Empty unless configured.
*/
allowedLoopbackAnyPort: string[];
/** Refuse to bind a non-loopback address without this (TLS must terminate in front). */
allowNonLoopback: boolean;
/** Trust X-Forwarded-For from a reverse proxy (only when one is actually in front). */
@@ -141,12 +147,28 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): BridgeConfig {
const hermesKey = env.HERMES_API_KEY || '';
if (!hermesKey) throw new ConfigError('HERMES_API_KEY is required (the Hermes API server refuses to start without one)');
const allowedOrigins = (env.HERMEVEN_ALLOWED_ORIGINS || '')
const originEntries = (env.HERMEVEN_ALLOWED_ORIGINS || '')
.split(',')
.map((s) => s.trim())
.filter(Boolean);
// The one permitted wildcard: any port on a loopback address, over plain http, written
// exactly as `http://127.0.0.1:*`, `http://localhost:*` or `http://[::1]:*`. A loopback
// origin can only be a page served on the same device as the browser making the request.
// No other wildcard exists: not for hosts, not for schemes, not for non-loopback ports.
const allowedLoopbackAnyPort: string[] = [];
const allowedOrigins: string[] = [];
for (const entry of originEntries) {
const anyPort = /^http:\/\/(127\.0\.0\.1|localhost|\[::1\]):\*$/.exec(entry);
if (anyPort) allowedLoopbackAnyPort.push(anyPort[1]!);
else allowedOrigins.push(entry);
}
for (const origin of allowedOrigins) {
if (origin === '*' ) throw new ConfigError('HERMEVEN_ALLOWED_ORIGINS must list exact origins, not *');
if (origin.includes('*')) {
throw new ConfigError(
'HERMEVEN_ALLOWED_ORIGINS must list exact origins. The only wildcard accepted is the port of a ' +
'loopback address, written http://127.0.0.1:* (or localhost, or [::1]).',
);
}
let parsed: URL;
try {
parsed = new URL(origin);
@@ -180,6 +202,7 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): BridgeConfig {
port: int(env, 'HERMEVEN_PORT', 8787, 0, 65535),
dbPath: env.HERMEVEN_DB_PATH || './data/hermeven.sqlite',
allowedOrigins,
allowedLoopbackAnyPort,
allowNonLoopback,
trustProxy: bool(env, 'HERMEVEN_TRUST_PROXY'),
hermes: {