Set PPQ_ALLOWED_HOSTS dynamically to prevent DNS-rebinding attacks

This commit is contained in:
ai-bot committed 2026-09-26 14:22:51 +00:00
1 parent 10907a6276
commit c6ad02d0a7
1 file changed
+51 -8
+51 -8
View File
@@ -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'],
})
})
})