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:
@@ -64,6 +64,24 @@ export function effectiveLimits(config: BridgeConfig): Limits {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a browser page at `origin` may read this bridge's responses.
|
||||
* Exact matches against the configured list; otherwise, if a loopback host was configured with
|
||||
* `:*`, an `http://<that host>:<port>` origin with a numeric port. Nothing else, and never a
|
||||
* missing or `null` origin.
|
||||
*/
|
||||
export function isAllowedOrigin(config: Pick<BridgeConfig, 'allowedOrigins' | 'allowedLoopbackAnyPort'>, origin: string | undefined): boolean {
|
||||
if (!origin || origin === 'null') return false;
|
||||
if (config.allowedOrigins.includes(origin)) return true;
|
||||
if (config.allowedLoopbackAnyPort.length === 0) return false;
|
||||
// Matched as text, start to end, so look-alikes such as http://127.0.0.1.example.com:80 or
|
||||
// http://127.0.0.1:80@example.com cannot pass.
|
||||
const match = /^http:\/\/(127\.0\.0\.1|localhost|\[::1\]):(\d{1,5})$/.exec(origin);
|
||||
if (!match) return false;
|
||||
const port = Number(match[2]);
|
||||
return port >= 1 && port <= 65535 && config.allowedLoopbackAnyPort.includes(match[1]!);
|
||||
}
|
||||
|
||||
export async function buildServer(deps: ServerDeps): Promise<FastifyInstance> {
|
||||
const { config, devices, engine } = deps;
|
||||
const app = Fastify({
|
||||
@@ -78,10 +96,11 @@ export async function buildServer(deps: ServerDeps): Promise<FastifyInstance> {
|
||||
genReqId: () => crypto.randomUUID(),
|
||||
});
|
||||
|
||||
// Exact-origin CORS. CORS only tells browsers who may read answers; every route
|
||||
// except /healthz and the pairing claim still requires a device credential.
|
||||
// CORS: exact origins, plus (only if configured) any port on a loopback address.
|
||||
// CORS only tells browsers who may read answers; every route except /healthz and the
|
||||
// pairing claim still requires a device credential.
|
||||
await app.register(cors, {
|
||||
origin: config.allowedOrigins.length ? config.allowedOrigins : false,
|
||||
origin: (origin, callback) => callback(null, isAllowedOrigin(config, origin)),
|
||||
methods: ['GET', 'POST', 'DELETE', 'OPTIONS'],
|
||||
allowedHeaders: ['Authorization', 'Content-Type'],
|
||||
credentials: false,
|
||||
|
||||
Reference in New Issue
Block a user