diff --git a/startos/main.ts b/startos/main.ts index 10a1485..5088e46 100644 --- a/startos/main.ts +++ b/startos/main.ts @@ -1,7 +1,8 @@ +import { utils } from '@start9labs/start-sdk' import { i18n } from './i18n' import { sdk } from './sdk' import { manifest } from './manifest' -import { apiPort, dataDir } from './utils' +import { apiHostId, apiPort, dataDir } from './utils' import { configureApiKey } from './actions/configureApiKey' import { configJson } from './fileModels/config.json' import { storeJson } from './fileModels/store.json' @@ -23,6 +24,40 @@ export const main = sdk.setupMain(async ({ effects }) => { await sdk.action.clearTask(effects, `${manifest.id}:${configureApiKey.id}`) } + // DNS-rebinding hardening. Upstream's proxy is bound to 0.0.0.0, and without + // PPQ_ALLOWED_HOSTS it accepts any Host header — a hostile web page could + // reach it as "same-origin" and even overwrite the stored API key via + // /setup/api-key. Pin the accepted Hosts to exactly the addresses StartOS + // serves this binding on, plus loopback and the bridge (what Open WebUI + // dials). Resolved reactively so an address added later is picked up on the + // next restart. + const servedHosts = await sdk.host + .getOwn( + effects, + apiHostId, + (host) => + host + ? utils + .filledAddress(host, { + hostId: apiHostId, + internalPort: apiPort, + username: null, + scheme: null, + sslScheme: null, + suffix: '', + }) + .matchesAny([{ visibility: 'private' }, { visibility: 'public' }]) + .filter({ exclude: { kind: ['localhost', 'link-local'] } }) + .hostnames.map((h) => h.hostname) + : [], + ) + .const() + + const osIp = await sdk.getOsIp(effects) + const allowedHosts = Array.from( + new Set(['localhost', '127.0.0.1', osIp, ...servedHosts].filter(Boolean)), + ).join(',') + const subcontainer = sdk.SubContainer.of( effects, { imageId: 'proxy' }, @@ -48,12 +83,20 @@ export const main = sdk.setupMain(async ({ effects }) => { subcontainer, exec: { command: ['node', '/app/dist/bin/server.js'], - env: { - PPQ_DATA_DIR: dataDir, - HOST: '0.0.0.0', - PORT: apiPort.toString(), - DEBUG: debug ? 'true' : 'false', - }, + env: allowedHosts + ? { + PPQ_DATA_DIR: dataDir, + HOST: '0.0.0.0', + PORT: apiPort.toString(), + DEBUG: debug ? 'true' : 'false', + PPQ_ALLOWED_HOSTS: allowedHosts, + } + : { + PPQ_DATA_DIR: dataDir, + HOST: '0.0.0.0', + PORT: apiPort.toString(), + DEBUG: debug ? 'true' : 'false', + }, }, // The proxy only binds its port after remote attestation of the enclave // succeeds, so a listening port means the encrypted channel is verified @@ -72,4 +115,4 @@ export const main = sdk.setupMain(async ({ effects }) => { }, requires: ['chown'], }) -}) +}) \ No newline at end of file