fix(app): trial dev server serves one of two known stacks only (R13)

trial.config.ts no longer accepts any loopback gateway. A literal
table (vite/trial-targets.ts) holds the db:sandbox trial target
(5193, gateway 57801, issuer db-sandbox) and the db:trial target
(5194, gateway 58801, issuer faunosteo-trial, gateway identity
probe); the gateway picks the entry by exact match and the app
port, app URL and anon key issuer must match it, with no default
and no fallback. Tests cover both mix-ups, the probe and the
values db-sandbox.sh passes today.
This commit is contained in:
bchanot committed 2026-11-10 13:40:42 +01:00
1 parent 60087bab30
commit 9f60d841eb
4 files changed
+513 -18

No files matched your search

+28 -18
View File
@@ -1,22 +1,23 @@
import { fileURLToPath } from 'node:url'
import { defineConfig, mergeConfig, type Plugin } from 'vite'
import base from '../vite.config'
import {
checkGatewayIdentity,
targetPortGuard,
trialTarget,
} from './trial-targets'
/**
* Dev server of `pnpm db:sandbox trial` (docs/development/V1-2-6_OWNER_TRIAL.md).
* Dev server of `pnpm db:sandbox trial` (docs/development/V1-2-6_OWNER_TRIAL.md) and of
* `pnpm db:trial app` (docs/development/SUPABASE.md §5.2).
* The browser talks to this origin only: the three Supabase routes are
* forwarded to the trial gateway on loopback, so the app itself needs no CORS (the proxied
* forwarded to the stack's gateway on loopback, so the app itself needs no CORS (the proxied
* routes keep the stack's own CORS headers).
* The server binds the one host that scripts/db-sandbox.sh validated (loopback or a private
* address of this machine) and refuses any other Host header.
* The server binds the one host the calling script validated (loopback or a private address of
* this machine) and refuses any other Host header. It serves exactly one of the two stacks of
* ./trial-targets.ts: gateway, app port, app URL and anon key issuer must all name the same one,
* and the db:trial gateway must answer its identity; anything else stops it before it listens.
*/
const target = process.env.FAUNOSTEO_TRIAL_GATEWAY
const url = target ? new URL(target) : null
if (!url || url.protocol !== 'http:' || url.hostname !== '127.0.0.1')
throw new Error(
'FAUNOSTEO_TRIAL_GATEWAY must be an http://127.0.0.1:<port> URL',
)
const host = process.env.FAUNOSTEO_TRIAL_HOST ?? ''
const PRIVATE_IPV4 =
/^(127\.0\.0\.1|10(\.\d{1,3}){3}|172\.(1[6-9]|2\d|3[01])(\.\d{1,3}){2}|192\.168(\.\d{1,3}){2})$/
@@ -24,10 +25,14 @@ if (!PRIVATE_IPV4.test(host))
throw new Error(
'FAUNOSTEO_TRIAL_HOST must be 127.0.0.1 or a private IPv4 address',
)
const target = trialTarget(process.env)
const SUPABASE_ROUTES = ['/auth/v1', '/rest/v1', '/storage/v1']
const REPO = fileURLToPath(new URL('../../../', import.meta.url))
// The CLI's --port and --strictPort are part of the resolved config: checked there.
const targetPortOnly = targetPortGuard(target)
/**
* `allowedHosts` still lets every IP literal and localhost through (Vite 7): any HTTP request whose
* Host is not the trial host is refused (403); the editor-launch route is closed (404).
@@ -52,9 +57,10 @@ const trialHostOnly: Plugin = {
},
}
export default defineConfig((env) =>
mergeConfig(base(env), {
plugins: [trialHostOnly],
export default defineConfig(async (env) => {
await checkGatewayIdentity(target)
return mergeConfig(base(env), {
plugins: [trialHostOnly, targetPortOnly],
// One validated host, never 0.0.0.0: the proxy carries the trial account's session.
server: {
host,
@@ -65,10 +71,14 @@ export default defineConfig((env) =>
cors: false,
ws: false,
// The LAN sees the app's own sources only: never the repository's notes, seed or builds.
fs: { allow: ['apps/app', 'packages', 'node_modules'].map((dir) => REPO + dir) },
fs: {
allow: ['apps/app', 'packages', 'node_modules'].map(
(dir) => REPO + dir,
),
},
proxy: Object.fromEntries(
SUPABASE_ROUTES.map((route) => [route, { target }]),
SUPABASE_ROUTES.map((route) => [route, { target: target.gateway }]),
),
},
}),
)
})
})