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: require local authority for initial password setup (#8771)
[._.]/ Adam Eivy1b0a4de0ba0f
diff --git a/docs/SETUP.md b/docs/SETUP.mdindex 4b4d965469..7b0dbb0fbd 100644--- a/docs/SETUP.md+++ b/docs/SETUP.md@@ -31,6 +31,20 @@ The installer performs step 4 automatically whenever the preceding account settiRun `npm run setup:guide` at any time to print the current walkthrough and correct URL. Add `-- --summary` for one line or `-- --json` for automation. PortOS's managed browser also opens the trusted MagicDNS URL when one is provisioned rather than defaulting to localhost.+## Instance password++Authentication is optional and off by default. Set a strong, unique password in+**Settings → Security** to protect the instance API and data. **First-time setup+requires a browser on the PortOS host using a loopback URL**: use+`http://localhost:5555` without HTTPS, the local mirror+`http://localhost:5553` with HTTPS, or `http://localhost:5554` for the local Vite UI.+A LAN/tailnet URL or a remote browser relayed through Vite cannot set the first+password; the Security page displays the host-control refusal.++Once configured, sign in from your other devices over the private network.+Changing the password requires both an operator session and the current+password, and signs out existing sessions. Peer credentials cannot change it.+## Choose an AI providerInitial setup is complete once at least one enabled provider is actually runnable. Choose one path under **AI → Providers**:diff --git a/server/lib/hostControlRoutes.js b/server/lib/hostControlRoutes.jsindex e014d2355c..8bce59eb7b 100644--- a/server/lib/hostControlRoutes.js+++ b/server/lib/hostControlRoutes.js@@ -58,6 +58,10 @@ import { isPlainObject } from './objects.js';import { escapeRegExp } from './textUtils.js';export const HOST_CONTROL_ROUTES = Object.freeze([+ // Setting a password mints an operator session: bootstrap must be local,+ // and rotation must already hold operator authority (#8771).+ 'POST /api/auth/password',+// Apps: create/edit choose the repo path and the start/build commands; the// lifecycle and launch routes run them under PM2 or the native launcher.'POST /api/apps',diff --git a/server/routes/auth.js b/server/routes/auth.jsindex de778fffcb..93be23b439 100644--- a/server/routes/auth.js+++ b/server/routes/auth.js@@ -139,9 +139,9 @@ router.post('/logout', asyncHandler(async (req, res) => {router.post('/password', asyncHandler(async (req, res) => {const body = validateRequest(setPasswordSchema, req.body || {});const alreadyEnabled = await isAuthEnabled();- // First-time set is the ONLY public mutation here — once auth is on, the- // route is gated by the API auth middleware in server/index.js, so we- // reach this branch only with a valid session.+ // The centralized host-control gate requires a local connection for first+ // setup, or an operator session for rotation. setPassword additionally+ // verifies the current password before changing an enabled configuration.const stale = await ownedStaleCookieNames(req);const { token } = await setPassword({newPassword: body.newPassword,diff --git a/server/routes/auth.test.js b/server/routes/auth.test.jsindex facd4ce298..4e5f6bb923 100644--- a/server/routes/auth.test.js+++ b/server/routes/auth.test.js@@ -1,6 +1,6 @@import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';import express from 'express';-import { writeFileSync } from 'fs';+import { readFileSync, writeFileSync } from 'fs';import { join } from 'path';import { mockPathsDataRoot } from '../lib/mockPathsDataRoot.js';import { bindSettingsFile } from '../lib/settingsTestUtil.js';@@ -40,18 +40,36 @@ const resetSettings = async () => {await writeSettingsFile({});};-const buildApp = async () => {+const instanceRegistry = vi.hoisted(() => ({ data: { self: null, peers: [] } }));+vi.mock('../services/instanceIdentity.js', async () => ({+ ...(await vi.importActual('../services/instanceIdentity.js')),+ loadData: async () => instanceRegistry.data,+}));++const buildApp = async ({ remoteAddress } = {}) => {// Re-import the route module under the current mock state so the test sees// a fresh auth-service binding each time.vi.resetModules();const { default: authRoutes } = await import('./auth.js');const app = express();+ if (remoteAddress) {+ // Model the socket peer, never an HTTP header, ahead of production gates.+ app.use((req, _res, next) => {+ Object.defineProperty(req.socket, 'remoteAddress', { value: remoteAddress });+ next();+ });+ const { authGate, hostControlRouteGate, requireHostControl } = await import('../services/authGate.js');+ app.use(authGate);+ app.use(hostControlRouteGate);+ app.post('/api/commands/execute', requireHostControl, (_req, res) => res.json({ reached: true }));+ }app.use(express.json());app.use('/api/auth', authRoutes);return app;};beforeEach(async () => {+ instanceRegistry.data = { self: null, peers: [] };await resetSettings();});@@ -281,3 +299,85 @@ describe('password-free risk status', () => {expect(readFileSync(join(tempRoot, 'settings.json'), 'utf8')).toBe('{corrupt');});});++describe('password setup through production authorization gates (#8771)', () => {+ const stores = () => ['settings.json', 'auth-sessions.json']+ .map((name) => readFileSync(join(tempRoot, name), 'utf8'));+ const passwordBody = { newPassword: 'example-password' };++ it.each([+ ['direct remote caller', '192.0.2.10', {}],+ ['forged forwarding headers', '192.0.2.10', {+ 'X-Forwarded-For': '127.0.0.1',+ 'X-PortOS-Dev-Proxy-Client-Address': '127.0.0.1',+ }],+ ['remote Vite client', '127.0.0.1', {+ 'X-PortOS-Dev-Proxy-Client-Address': '192.0.2.10',+ }],+ ])('refuses %s without changing settings or sessions or granting host control', async (_name, remoteAddress, headers) => {+ const app = await buildApp({ remoteAddress });+ const auth = await import('../services/auth.js');+ // Even an existing session must survive a rejected setup unchanged.+ await auth.createSession();+ const before = stores();+ const send = (path) => Object.entries(headers).reduce(+ (req, [key, value]) => req.set(key, value), request(app).post(path),+ );+ const refused = await send('/api/auth/password').send(passwordBody);+ expect(refused.status).toBe(403);+ expect(refused.body.code).toBe('HOST_CONTROL_FORBIDDEN');+ expect(refused.headers['set-cookie']).toBeUndefined();+ expect(stores()).toEqual(before);+ const command = await send('/api/commands/execute').send({});+ expect(command.status).toBe(403);+ expect(command.body.code).toBe('HOST_CONTROL_FORBIDDEN');+ });++ it.each([undefined, '127.0.0.1'])('allows local setup with Vite client marker %s', async (proxyClient) => {+ const app = await buildApp({ remoteAddress: '127.0.0.1' });+ let setup = request(app).post('/api/auth/password');+ if (proxyClient) setup = setup.set('X-PortOS-Dev-Proxy-Client-Address', proxyClient);+ const response = await setup.send(passwordBody);+ expect(response.status).toBe(200);+ expect(response.headers['set-cookie']).toMatch(/portos_auth_\d+=/);+ const command = await request(app).post('/api/commands/execute')+ .set('Cookie', response.headers['set-cookie']).send({});+ expect(command.body).toEqual({ reached: true });+ });++ it('rotates only with an operator session and current password, never peer credentials', async () => {+ const app = await buildApp({ remoteAddress: '192.0.2.10' });+ const auth = await import('../services/auth.js');+ const { token } = await auth.setPassword(passwordBody);+ const peer = { id: 'example-peer', instanceId: 'example-instance', enabled: true,+ syncSecret: 'example-pair-secret-0123456789-abcdef' };+ instanceRegistry.data.peers = [peer];+ const { derivePeerAuthToken } = await import('../lib/peerHttpClient.js');+ const body = { currentPassword: 'example-password', newPassword: 'example-replacement' };+ const before = stores();+ const scoped = await request(app).post('/api/auth/password')+ .set('X-PortOS-Instance-Id', peer.instanceId)+ .set('X-PortOS-Peer-Auth', derivePeerAuthToken(peer.syncSecret, peer.instanceId)).send(body);+ expect(scoped.status).toBe(403);+ expect(scoped.body.code).toBe('PEER_SCOPE_FORBIDDEN');+ expect(scoped.headers['set-cookie']).toBeUndefined();+ const basic = await request(app).post('/api/auth/password')+ .set('Authorization', 'Basic ' + Buffer.from(':example-password').toString('base64')).send(body);+ expect(basic.status).toBe(403);+ expect(basic.body.code).toBe('HOST_CONTROL_FORBIDDEN');+ expect(basic.headers['set-cookie']).toBeUndefined();+ const cookie = `portos_auth=${token}`;+ const wrong = await request(app).post('/api/auth/password').set('Authorization', `Bearer ${token}`)+ .send({ ...body, currentPassword: 'wrong-password' });+ expect(wrong.status).toBe(401);+ expect(wrong.body.code).toBe('AUTH_BAD_CURRENT');+ expect(stores()).toEqual(before);+ const rotated = await request(app).post('/api/auth/password').set('Authorization', `Bearer ${token}`).send(body);+ expect(rotated.status).toBe(200);+ expect(rotated.headers['set-cookie']).toMatch(/portos_auth_\d+=/);+ expect((await request(app).post('/api/commands/execute').set('Cookie', cookie).send({})).status).toBe(401);+ expect((await request(app).post('/api/commands/execute')+ .set('Cookie', rotated.headers['set-cookie']).send({})).body).toEqual({ reached: true });+ expect(await auth.verifyPassword(body.newPassword)).toBe(true);+ });+});