Skip to content

Commit b65c2f5

Browse files
authored
Gate production deployment modes (#11)
1 parent 7d18e7e commit b65c2f5

9 files changed

Lines changed: 331 additions & 69 deletions

‎scripts/orbit-local-dev.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ if (build.status !== 0) process.exit(build.status ?? 1);
4545

4646
const vars = {
4747
ORBIT_ENVIRONMENT: 'local',
48+
ORBIT_DEPLOYMENT_MODE: 'live',
4849
ORBIT_ALLOWED_ORIGIN: 'http://localhost:4321',
4950
ORBIT_GITHUB_CALLBACK_URL: 'http://localhost:4321/v1/auth/github/callback',
5051
ORBIT_PLATFORM_OWNER_GITHUB_ID: '126420524',

‎scripts/orbit-slice1-worker-tests.ts‎

Lines changed: 233 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,43 @@ import assert from 'node:assert/strict';
22
import { describe, test } from 'node:test';
33
import worker from '../src/worker';
44
import {
5+
assertDeploymentBindings,
56
assertIdentityBindings,
67
type OrbitBindings,
8+
type OrbitDeploymentMode,
79
} from '../src/server/identity/bindings';
810

11+
const DARK_LAUNCH_ORIGIN = 'https://orbit-v6-production.samett33710.workers.dev';
12+
const LIVE_ORIGIN = 'https://orbit.sametbasbug.dev';
13+
const CRAWLER_DENIAL = 'noindex, nofollow, noarchive';
14+
15+
function testDatabase(): OrbitBindings['DB'] {
16+
return {
17+
prepare() {
18+
const statement = {
19+
bind() {
20+
return statement;
21+
},
22+
async run() {
23+
return { success: true };
24+
},
25+
async first() {
26+
return null;
27+
},
28+
async all() {
29+
return { results: [] };
30+
},
31+
};
32+
return statement;
33+
},
34+
async batch() {
35+
return [];
36+
},
37+
} as OrbitBindings['DB'];
38+
}
39+
940
const baseBindings = {
10-
DB: {} as OrbitBindings['DB'],
41+
DB: testDatabase(),
1142
ORBIT_PLATFORM_OWNER_GITHUB_ID: '126420524',
1243
GITHUB_OAUTH_CLIENT_ID: 'test-client-id',
1344
GITHUB_OAUTH_CLIENT_SECRET: 'test-client-secret',
@@ -19,60 +50,230 @@ const baseBindings = {
1950
ORBIT_CURSOR_PEPPER_V1: 'test-cursor-pepper-at-least-32-bytes-long',
2051
} as const;
2152

53+
function assetBinding(): NonNullable<OrbitBindings['ASSETS']> {
54+
return {
55+
async fetch(request) {
56+
const path = new URL(request.url).pathname;
57+
if (path === '/missing') return new Response('Not found', { status: 404 });
58+
if (path === '/robots.txt') {
59+
return new Response('User-agent: *\nAllow: /\n', {
60+
headers: { 'content-type': 'text/plain; charset=utf-8' },
61+
});
62+
}
63+
return new Response('<!doctype html><title>Orbit</title>', {
64+
headers: { 'content-type': 'text/html; charset=utf-8' },
65+
});
66+
},
67+
};
68+
}
69+
2270
function stagingBindings(overrides: Partial<OrbitBindings> = {}): OrbitBindings {
2371
return {
2472
...baseBindings,
2573
ORBIT_ENVIRONMENT: 'staging',
74+
ORBIT_DEPLOYMENT_MODE: 'dark_launch',
2675
ORBIT_ALLOWED_ORIGIN: 'https://orbit-v6-staging.samett33710.workers.dev',
2776
ORBIT_GITHUB_CALLBACK_URL:
2877
'https://orbit-v6-staging.samett33710.workers.dev/v1/auth/github/callback',
29-
ASSETS: {
30-
async fetch() {
31-
return new Response('<!doctype html><title>Orbit staging</title>', {
32-
headers: { 'content-type': 'text/html; charset=utf-8' },
33-
});
34-
},
35-
},
78+
ASSETS: assetBinding(),
79+
...overrides,
80+
};
81+
}
82+
83+
function productionBindings(
84+
mode: OrbitDeploymentMode,
85+
overrides: Partial<OrbitBindings> = {},
86+
): OrbitBindings {
87+
const origin = mode === 'dark_launch' ? DARK_LAUNCH_ORIGIN : LIVE_ORIGIN;
88+
return {
89+
...baseBindings,
90+
ORBIT_ENVIRONMENT: 'production',
91+
ORBIT_DEPLOYMENT_MODE: mode,
92+
ORBIT_ALLOWED_ORIGIN: origin,
93+
ORBIT_GITHUB_CALLBACK_URL: `${origin}/v1/auth/github/callback`,
94+
ASSETS: assetBinding(),
3695
...overrides,
3796
};
3897
}
3998

40-
describe('Orbit V6 staging contract', () => {
41-
test('accepts only the exact staging origin and callback', () => {
99+
function localBindings(environment: 'local' | 'test'): OrbitBindings {
100+
return {
101+
...baseBindings,
102+
ORBIT_ENVIRONMENT: environment,
103+
ORBIT_DEPLOYMENT_MODE: 'live',
104+
ORBIT_ALLOWED_ORIGIN: environment === 'local' ? 'http://localhost:4321' : 'https://test.example',
105+
ORBIT_GITHUB_CALLBACK_URL: environment === 'local'
106+
? 'http://localhost:4321/v1/auth/github/callback'
107+
: 'https://test.example/v1/auth/github/callback',
108+
ASSETS: assetBinding(),
109+
};
110+
}
111+
112+
async function assertCrawlerDenied(response: Response): Promise<void> {
113+
assert.equal(response.headers.get('x-robots-tag'), CRAWLER_DENIAL);
114+
await response.arrayBuffer();
115+
}
116+
117+
async function protectedSurfaceResponses(env: OrbitBindings): Promise<Response[]> {
118+
const origin = env.ORBIT_ALLOWED_ORIGIN;
119+
return [
120+
await worker.fetch(new Request(`${origin}/healthz`), env),
121+
await worker.fetch(new Request(`${origin}/dashboard`), env),
122+
await worker.fetch(new Request(`${origin}/`), env),
123+
await worker.fetch(new Request(`${origin}/missing`), env),
124+
await worker.fetch(new Request(`${origin}/v1/not-found`), env),
125+
await worker.fetch(new Request(`${origin}/v1/auth/github/start`, {
126+
method: 'POST',
127+
headers: { 'content-type': 'application/json', origin },
128+
body: '{}',
129+
}), env),
130+
];
131+
}
132+
133+
describe('Orbit V6 deployment-mode contract', () => {
134+
test('requires an explicit supported deployment mode', () => {
135+
assert.throws(
136+
() => assertDeploymentBindings({
137+
...productionBindings('dark_launch'),
138+
ORBIT_DEPLOYMENT_MODE: undefined,
139+
} as unknown as OrbitBindings),
140+
/invalid_deployment_mode/u,
141+
);
142+
assert.throws(
143+
() => assertDeploymentBindings({
144+
...productionBindings('dark_launch'),
145+
ORBIT_DEPLOYMENT_MODE: 'preview',
146+
} as unknown as OrbitBindings),
147+
/invalid_deployment_mode/u,
148+
);
149+
});
150+
151+
test('accepts exact production dark-launch Workers.dev origin and callback', () => {
152+
assert.doesNotThrow(() => assertIdentityBindings(productionBindings('dark_launch')));
153+
});
154+
155+
test('rejects wrong, wildcard and lookalike Workers.dev hosts', () => {
156+
for (const origin of [
157+
'https://orbit-v6-production.attacker.workers.dev',
158+
'https://*.workers.dev',
159+
'https://orbit-v6-production.samett33710.workers.dev.evil.example',
160+
'https://evil-orbit-v6-production.samett33710.workers.dev',
161+
]) {
162+
assert.throws(
163+
() => assertDeploymentBindings(productionBindings('dark_launch', {
164+
ORBIT_ALLOWED_ORIGIN: origin,
165+
ORBIT_GITHUB_CALLBACK_URL: `${origin}/v1/auth/github/callback`,
166+
})),
167+
/invalid_production_origin/u,
168+
);
169+
}
170+
});
171+
172+
test('accepts exact production live origin and callback', () => {
173+
assert.doesNotThrow(() => assertIdentityBindings(productionBindings('live')));
174+
});
175+
176+
test('rejects dark-launch values in live mode and live values in dark-launch mode', () => {
177+
assert.throws(
178+
() => assertDeploymentBindings(productionBindings('live', {
179+
ORBIT_ALLOWED_ORIGIN: DARK_LAUNCH_ORIGIN,
180+
ORBIT_GITHUB_CALLBACK_URL: `${DARK_LAUNCH_ORIGIN}/v1/auth/github/callback`,
181+
})),
182+
/invalid_production_origin/u,
183+
);
184+
assert.throws(
185+
() => assertDeploymentBindings(productionBindings('dark_launch', {
186+
ORBIT_ALLOWED_ORIGIN: LIVE_ORIGIN,
187+
ORBIT_GITHUB_CALLBACK_URL: `${LIVE_ORIGIN}/v1/auth/github/callback`,
188+
})),
189+
/invalid_production_origin/u,
190+
);
191+
});
192+
193+
test('rejects callbacks that do not exactly match their production mode', () => {
194+
assert.throws(
195+
() => assertDeploymentBindings(productionBindings('dark_launch', {
196+
ORBIT_GITHUB_CALLBACK_URL: `${LIVE_ORIGIN}/v1/auth/github/callback`,
197+
})),
198+
/invalid_production_callback/u,
199+
);
200+
assert.throws(
201+
() => assertDeploymentBindings(productionBindings('live', {
202+
ORBIT_GITHUB_CALLBACK_URL: `${DARK_LAUNCH_ORIGIN}/v1/auth/github/callback`,
203+
})),
204+
/invalid_production_callback/u,
205+
);
206+
});
207+
208+
test('keeps the exact staging contract and requires dark-launch mode', () => {
42209
assert.doesNotThrow(() => assertIdentityBindings(stagingBindings()));
43210
assert.throws(
44-
() => assertIdentityBindings(stagingBindings({ ORBIT_ALLOWED_ORIGIN: 'https://evil.example' })),
211+
() => assertDeploymentBindings(stagingBindings({ ORBIT_DEPLOYMENT_MODE: 'live' })),
212+
/invalid_staging_deployment_mode/u,
213+
);
214+
assert.throws(
215+
() => assertDeploymentBindings(stagingBindings({ ORBIT_ALLOWED_ORIGIN: 'https://evil.example' })),
45216
/invalid_staging_origin/u,
46217
);
47218
assert.throws(
48-
() => assertIdentityBindings(stagingBindings({ ORBIT_GITHUB_CALLBACK_URL: 'https://evil.example/callback' })),
219+
() => assertDeploymentBindings(stagingBindings({ ORBIT_GITHUB_CALLBACK_URL: 'https://evil.example/callback' })),
49220
/invalid_staging_callback/u,
50221
);
51222
});
52223

53-
test('adds a crawler-denial header to health and static staging responses', async () => {
54-
const env = stagingBindings();
55-
const health = await worker.fetch(new Request(`${env.ORBIT_ALLOWED_ORIGIN}/healthz`), env);
56-
assert.equal(health.status, 200);
57-
assert.equal(health.headers.get('x-robots-tag'), 'noindex, nofollow, noarchive');
58-
assert.deepEqual(await health.json(), {
59-
ok: true,
60-
service: 'orbit-v6',
61-
environment: 'staging',
224+
test('adds crawler denial to every staging and production dark-launch response class', async () => {
225+
for (const env of [stagingBindings(), productionBindings('dark_launch')]) {
226+
const responses = await protectedSurfaceResponses(env);
227+
assert.deepEqual(responses.map((response) => response.status), [200, 200, 200, 404, 404, 201]);
228+
for (const response of responses) await assertCrawlerDenied(response);
229+
}
230+
231+
const staging = stagingBindings();
232+
const oauthRedirect = await worker.fetch(new Request(`${staging.ORBIT_ALLOWED_ORIGIN}/__staging/oauth`), staging);
233+
assert.equal(oauthRedirect.status, 302);
234+
await assertCrawlerDenied(oauthRedirect);
235+
236+
const failingAssets = productionBindings('dark_launch', {
237+
ASSETS: {
238+
async fetch() {
239+
throw new Error('simulated_asset_failure');
240+
},
241+
},
62242
});
243+
const failure = await worker.fetch(new Request(`${failingAssets.ORBIT_ALLOWED_ORIGIN}/failure`), failingAssets);
244+
assert.equal(failure.status, 500);
245+
await assertCrawlerDenied(failure);
246+
});
63247

64-
const page = await worker.fetch(new Request(`${env.ORBIT_ALLOWED_ORIGIN}/`), env);
65-
assert.equal(page.status, 200);
66-
assert.equal(page.headers.get('x-robots-tag'), 'noindex, nofollow, noarchive');
248+
test('serves a dynamic deny-all robots policy in staging and production dark launch', async () => {
249+
for (const env of [stagingBindings(), productionBindings('dark_launch')]) {
250+
const response = await worker.fetch(new Request(`${env.ORBIT_ALLOWED_ORIGIN}/robots.txt`), env);
251+
assert.equal(response.status, 200);
252+
assert.equal(response.headers.get('content-type'), 'text/plain; charset=utf-8');
253+
assert.equal(response.headers.get('cache-control'), 'no-store');
254+
assert.equal(response.headers.get('x-robots-tag'), CRAWLER_DENIAL);
255+
assert.equal(await response.text(), 'User-agent: *\nDisallow: /\n');
256+
}
67257
});
68258

69-
test('does not add staging crawler policy in production', async () => {
70-
const env = stagingBindings({
71-
ORBIT_ENVIRONMENT: 'production',
72-
ORBIT_ALLOWED_ORIGIN: 'https://orbit.sametbasbug.dev',
73-
ORBIT_GITHUB_CALLBACK_URL: 'https://orbit.sametbasbug.dev/v1/auth/github/callback',
74-
});
75-
const response = await worker.fetch(new Request('https://orbit.sametbasbug.dev/healthz'), env);
76-
assert.equal(response.headers.get('x-robots-tag'), null);
259+
test('keeps production live indexable and serves the public robots asset', async () => {
260+
const env = productionBindings('live');
261+
const responses = await protectedSurfaceResponses(env);
262+
for (const response of responses) {
263+
assert.equal(response.headers.get('x-robots-tag'), null);
264+
await response.arrayBuffer();
265+
}
266+
const robots = await worker.fetch(new Request(`${env.ORBIT_ALLOWED_ORIGIN}/robots.txt`), env);
267+
assert.equal(robots.headers.get('x-robots-tag'), null);
268+
assert.match(await robots.text(), /Allow: \//u);
269+
});
270+
271+
test('preserves local and test validation without adding crawler denial', async () => {
272+
for (const env of [localBindings('local'), localBindings('test')]) {
273+
assert.doesNotThrow(() => assertIdentityBindings(env));
274+
const response = await worker.fetch(new Request(`${env.ORBIT_ALLOWED_ORIGIN}/healthz`), env);
275+
assert.equal(response.status, 200);
276+
assert.equal(response.headers.get('x-robots-tag'), null);
277+
}
77278
});
78279
});

‎scripts/orbit-slice5-images-cpu-staging-e2e.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ try {
149149
preview_urls: false,
150150
vars: {
151151
ORBIT_ENVIRONMENT: 'test',
152+
ORBIT_DEPLOYMENT_MODE: 'live',
152153
ORBIT_ALLOWED_ORIGIN: origin,
153154
ORBIT_GITHUB_CALLBACK_URL: `${origin}/v1/auth/github/callback`,
154155
ORBIT_PLATFORM_OWNER_GITHUB_ID: '126420524',

0 commit comments

Comments
 (0)