Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
});
});
22 changes: 22 additions & 0 deletions apps/cli/__tests__/preview/remix-app-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
REMIX_SERVER_COMPATIBILITY_DATE,
} from '../../src/services/preview/remix-app-server';
import {
devRequestHeaders,
isBundleRequest,
publicOriginOf,
routeFor,
Expand Down Expand Up @@ -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<http.Server> =>
new Promise((resolve) => {
Expand All @@ -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');
Expand All @@ -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('<html></html>');
// 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();
Expand Down
22 changes: 20 additions & 2 deletions apps/cli/src/services/preview/app-server-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppServerProxy> {
const devHost = opts.devHost ?? '127.0.0.1';
const upgraded = new Set<import('stream').Duplex>();
Expand All @@ -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(
Expand Down Expand Up @@ -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)) {
Expand Down
8 changes: 5 additions & 3 deletions apps/cli/src/services/preview/bundle-health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading