diff --git a/apps/cli/__tests__/preview/missing-imports-bundle-health.test.ts b/apps/cli/__tests__/preview/missing-imports-bundle-health.test.ts index e9efd6e9..57448d6e 100644 --- a/apps/cli/__tests__/preview/missing-imports-bundle-health.test.ts +++ b/apps/cli/__tests__/preview/missing-imports-bundle-health.test.ts @@ -164,7 +164,7 @@ describe('git exclude', () => { expect(addPreviewGitExcludes(file)).toBe(false); expect(readFileSync(file, 'utf8')).toBe( '# git ls-files --others --exclude-from=.git/info/exclude\n*.local\n' + - '# CodeAgent preview runtime files\n/.wrangler/\n/.codeam/*\n!/.codeam/preview.json\n', + '# CodeAgent preview runtime files\n/.wrangler/\n/.codeam/\n', ); }); }); diff --git a/apps/cli/__tests__/preview/remix-app-server.test.ts b/apps/cli/__tests__/preview/remix-app-server.test.ts index 285fe6b6..6e093655 100644 --- a/apps/cli/__tests__/preview/remix-app-server.test.ts +++ b/apps/cli/__tests__/preview/remix-app-server.test.ts @@ -22,6 +22,7 @@ import { REMIX_SERVER_COMPATIBILITY_DATE, } from '../../src/services/preview/remix-app-server'; import { + devRequestHeaders, isBundleRequest, publicOriginOf, routeFor, @@ -169,6 +170,22 @@ describe('app-server proxy — routing like the platform Worker (dispatch.ts)', }); }); +describe('app-server proxy — the dev server sees a same-origin request as its own', () => { + it("rewrites a same-origin Origin to the dev server's (Expo 500s a foreign one: the icon fonts)", () => { + const h = devRequestHeaders( + { host: 'p.example.com', origin: 'https://p.example.com', 'sec-fetch-dest': 'font' }, + 8081, + ); + expect(h).toMatchObject({ host: 'localhost:8081', origin: 'http://localhost:8081' }); + }); + it('leaves a cross-origin Origin alone, and adds none when there was none', () => { + expect(devRequestHeaders({ host: 'p.example.com', origin: 'https://evil.example' }, 8081).origin).toBe( + 'https://evil.example', + ); + expect(devRequestHeaders({ host: 'p.example.com' }, 8081).origin).toBeUndefined(); + }); +}); + describe('app-server proxy — live', () => { const listen = (handler: http.RequestListener): Promise => new Promise((resolve) => { @@ -184,7 +201,9 @@ describe('app-server proxy — live', () => { res.writeHead(200, { 'content-type': 'application/json' }); res.end('{"ok":true}'); }); + const devOrigins: (string | undefined)[] = []; const dev = await listen((req, res) => { + devOrigins.push(req.headers.origin); if (req.url?.startsWith('/index.bundle')) { res.writeHead(200, { 'content-type': 'application/javascript', 'content-length': '7' }); res.end('var a=1'); @@ -211,6 +230,9 @@ describe('app-server proxy — live', () => { expect(bundle).toBe(`globalThis.__REMIX_SERVER_BASE__="${base}/_v/local/";var a=1`); expect(await (await fetch(`${base}/hbc.bundle?platform=ios`)).text()).toBe('BYTES'); expect(await (await fetch(`${base}/`)).text()).toBe(''); + // A same-origin font request reaches the dev server with its own origin. + await fetch(`${base}/assets/?unstable_path=Feather.ttf`, { headers: { Origin: base } }); + expect(devOrigins.at(-1)).toBe(`http://localhost:${portOf(dev)}`); } finally { await proxy.close(); appServer.close(); diff --git a/apps/cli/src/services/preview/app-server-proxy.ts b/apps/cli/src/services/preview/app-server-proxy.ts index a197bcd1..6e9ac62d 100644 --- a/apps/cli/src/services/preview/app-server-proxy.ts +++ b/apps/cli/src/services/preview/app-server-proxy.ts @@ -95,6 +95,24 @@ export function serverRequestHeaders( return out; } +/** + * The dev server's headers for a request the proxy forwards to it. Host becomes the dev server's + * own (as the inspector proxy does), and a SAME-ORIGIN request's `Origin` follows it: Expo's dev + * middleware refuses an origin that is not its own host with a 500 (`default-src 'none'`), so the + * icon fonts (`/assets/?unstable_path=…Feather.ttf`, fetched by the browser with `Origin`) failed + * on the owner's codespace while curl, which sends none, got them (2026-11-23). A cross-origin + * `Origin` is left alone. + */ +export function devRequestHeaders( + headers: http.IncomingHttpHeaders, + devPort: number, +): http.OutgoingHttpHeaders { + const out = upstreamRequestHeaders(headers, devPort); + const origin = one(headers.origin); + if (origin && origin === publicOriginOf(headers)) out.origin = `http://localhost:${devPort}`; + return out; +} + export async function startAppServerProxy(opts: AppServerProxyOptions): Promise { const devHost = opts.devHost ?? '127.0.0.1'; const upgraded = new Set(); @@ -107,7 +125,7 @@ export async function startAppServerProxy(opts: AppServerProxyOptions): Promise< const inject = !toServer && opts.injectBase && isBundleRequest(url); const headers = toServer ? serverRequestHeaders(req.headers, opts.serverPort, origin) - : upstreamRequestHeaders(req.headers, opts.devPort); + : devRequestHeaders(req.headers, opts.devPort); // Can't prepend into compressed bytes: bundles come uncompressed through the proxy. if (inject) delete headers['accept-encoding']; const upstream = http.request( @@ -148,7 +166,7 @@ export async function startAppServerProxy(opts: AppServerProxyOptions): Promise< const port = toServer ? opts.serverPort : opts.devPort; const headers = toServer ? serverRequestHeaders(req.headers, opts.serverPort, publicOriginOf(req.headers)) - : upstreamRequestHeaders(req.headers, opts.devPort); + : devRequestHeaders(req.headers, opts.devPort); const upstream = net.connect(port, toServer ? '127.0.0.1' : devHost, () => { const lines = [`${req.method} ${toServer ? route.path : url} HTTP/1.1`]; for (const [k, v] of Object.entries(headers)) { diff --git a/apps/cli/src/services/preview/bundle-health.ts b/apps/cli/src/services/preview/bundle-health.ts index 28e26fe8..806d233c 100644 --- a/apps/cli/src/services/preview/bundle-health.ts +++ b/apps/cli/src/services/preview/bundle-health.ts @@ -86,13 +86,15 @@ export async function checkAppBundle( /** * Keep the preview's runtime files out of the user's `git status` (the change card counted 21 * files / +8788 lines of `.wrangler/` + `.codeam/`). Written to `.git/info/exclude` — never the - * user's `.gitignore` — and only once. A committed `.codeam/preview.json` stays visible. + * user's `.gitignore` — and only once. A committed `.codeam/preview.json` stays visible (git never + * excludes a tracked file). */ export const GIT_EXCLUDE_LINES = [ '# CodeAgent preview runtime files', '/.wrangler/', - '/.codeam/*', - '!/.codeam/preview.json', + // The whole folder: git never hides a TRACKED file, so a committed .codeam/preview.json stays + // visible anyway, and the one we write ourselves (the detection cache) is ours, not a change. + '/.codeam/', ]; export function addPreviewGitExcludes(excludeFile: string): boolean {