Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: stop treating a page on any loopback port as same-origin to the PortOS API
Adam Eivyedc109ba9937
diff --git a/lib/portosAuthCore.js b/lib/portosAuthCore.jsindex abed31323b..b797c3fe31 100644--- a/lib/portosAuthCore.js+++ b/lib/portosAuthCore.js@@ -17,6 +17,7 @@ import { createHash, timingSafeEqual, scrypt } from 'node:crypto';import { isIP } from 'node:net';import { hostname as osHostname } from 'node:os';import { promisify } from 'node:util';+import { PORTS } from '../server/lib/ports.js';const scryptAsync = promisify(scrypt);@@ -211,6 +212,19 @@ const stripPort = (hostHeader) => {const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '[::1]']);const isLoopback = (hostname) => LOOPBACK_HOSTS.has(hostname.toLowerCase());+// The port in a `host[:port]` authority, or '' when absent (the scheme default).+const portOf = (authority) => /:(\d+)$/.exec(authority)?.[1] ?? '';++// Ports a PortOS-served page can originate from: the API (HTTPS/HTTP), its+// loopback HTTP mirror, the Vite dev server and the Autofixer UI — plus the+// ecosystem overrides for each.+const isPortosLoopbackPort = (originPort, hostPort) => {+ if (originPort === hostPort) return true;+ const env = process.env;+ const own = [PORTS.API, PORTS.API_LOCAL, PORTS.UI, PORTS.AUTOFIXER_UI, env.PORT, env.PORTOS_HTTP_PORT, env.VITE_PORT];+ return own.some((port) => port !== undefined && port !== '' && String(port) === originPort);+};+export const isCrossOrigin = (req) => {const origin = req.headers?.origin;if (!origin) return false;@@ -229,10 +243,14 @@ export const isCrossOrigin = (req) => {// Dev/sidecar workflow exemption: Vite proxies :5554 → :5555 with// changeOrigin, and the autofixer UI on :5560 is opened from the main UI —// so a real same-machine browser request can arrive as- // `Origin: http://localhost:5554` / `Host: localhost:5555`. Treat any- // loopback-to-loopback pairing as same-origin regardless of port — the CSRF- // threat is from attackers on OTHER machines.- if (isLoopback(stripPort(parsed.host)) && isLoopback(stripPort(host))) return false;+ // `Origin: http://localhost:5554` / `Host: localhost:5555`. Treat a+ // loopback-to-loopback pairing as same-origin only when the page is served by+ // PortOS itself (one of its own ports, or the very port being addressed under+ // another loopback name). Any other loopback port is a DIFFERENT principal — a+ // managed app, a dev server, a hostile local page — and must not borrow the+ // session cookie or the loopback trust that password-free host control rests on.+ if (isLoopback(stripPort(parsed.host)) && isLoopback(stripPort(host))+ && isPortosLoopbackPort(portOf(parsed.host), portOf(host))) return false;return true;};diff --git a/server/services/authGate.test.js b/server/services/authGate.test.jsindex 9d1a667088..389048df77 100644--- a/server/services/authGate.test.js+++ b/server/services/authGate.test.js@@ -337,6 +337,34 @@ describe('authGate middleware', () => {expect(result.called).toBe(true);});+ it('rejects a page on another loopback port (a managed app is a different principal)', async () => {+ const auth = await import('./auth.js');+ const { token } = await auth.setPassword({ newPassword: 'correct-horse' });+ const { authGate } = await import('./authGate.js');+ for (const origin of ['http://localhost:3000', 'http://127.0.0.1:8080', 'http://[::1]:4173']) {+ const result = await runGate(authGate, {+ path: '/api/cos',+ headers: { host: 'localhost:5555', origin, cookie: `portos_auth=${token}` },+ });+ expect(result.called, origin).toBe(false);+ expect(result.res.statusCode).toBe(403);+ }+ });++ it('allows the same loopback port under another loopback name and PortOS-owned ports', async () => {+ const auth = await import('./auth.js');+ const { token } = await auth.setPassword({ newPassword: 'correct-horse' });+ const { authGate } = await import('./authGate.js');+ for (const [host, origin] of [+ ['127.0.0.1:5555', 'http://localhost:5555'],+ ['localhost:5560', 'http://localhost:5555'],+ ['localhost:5555', 'http://localhost:5553'],+ ]) {+ const result = await runGate(authGate, { path: '/api/cos', headers: { host, origin, cookie: `portos_auth=${token}` } });+ expect(result.called, `${origin} -> ${host}`).toBe(true);+ }+ });+it('rejects a cross-origin logout (public path still gets the CSRF check)', async () => {const auth = await import('./auth.js');await auth.setPassword({ newPassword: 'correct-horse' });