Skip to content

Commit 32e2e21

Browse files
committed
fix(tanstackstart-react): Keep rejecting non-POST requests to a static tunnelRoute with a 405
Without it, Start renders the app for them and runs its root loaders, which #24615 fixed for the route based tunnel.
1 parent 809e456 commit 32e2e21

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎dev-packages/e2e-tests/test-applications/tanstackstart-react/tests/tunnel.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ test('Sends client-side errors through the configured tunnel route', async ({ pa
6161
expect(errorEvent.transaction).toBe(`${basePath}/`);
6262
});
6363

64-
test('Only answers POSTs on the managed tunnel path and leaves other methods to the app', async ({ request }) => {
64+
test('Rejects non-POST requests to the managed tunnel route instead of rendering the app', async ({ request }) => {
6565
test.skip(tunnelRouteMode !== 'static', 'Requires a known managed tunnel path');
6666

6767
const response = await request.get('/monitor');
6868

69-
expect(response.status()).toBe(404);
69+
expect(response.status()).toBe(405);
70+
expect(response.headers()['allow']).toBe('POST');
7071
});
7172

7273
function pathnameMatchesTunnelRoute(pathname: string): boolean {

‎packages/tanstackstart-react/src/server/tunnelRoute.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,22 @@ export function wrapServerEntryWithSentryTunnel<T extends ServerEntry>(
124124
return entry;
125125
}
126126

127+
const fixedPath = tunnelRoute?.path?.replace(/\/$/, '');
128+
127129
// Keeps the prototype, so a class instance entry keeps its other handlers, like `scheduled` or `queue`. `fetch` is
128130
// defined rather than assigned, which a read-only `fetch` on a frozen prototype would reject.
129131
return Object.create(Object.getPrototypeOf(entry), {
130132
...Object.getOwnPropertyDescriptors(entry),
131133
fetch: {
132134
// Cloudflare Workers pass `env` and `ctx` after the request, and `withSentry` needs `ctx` to flush.
133-
value: (request: Request, ...rest: unknown[]) =>
134-
handleTunnelRequestIfMatched(request) ?? entry.fetch(request, ...rest),
135+
value: (request: Request, ...rest: unknown[]) => {
136+
// Start would render the app for any other method on this path, running its root loaders.
137+
if (fixedPath && request.method !== 'POST' && new URL(request.url).pathname.replace(/\/$/, '') === fixedPath) {
138+
return new Response(null, { status: 405, headers: { Allow: 'POST' } });
139+
}
140+
141+
return handleTunnelRequestIfMatched(request) ?? entry.fetch(request, ...rest);
142+
},
135143
writable: true,
136144
configurable: true,
137145
enumerable: true,

‎packages/tanstackstart-react/test/server/wrapServerEntryWithSentryTunnel.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ describe('wrapServerEntryWithSentryTunnel', () => {
6666
expect(await response.text()).toBe('forwarded');
6767
});
6868

69+
it('rejects other methods on a fixed `tunnelRoute` path instead of rendering the app', async () => {
70+
setClient(undefined);
71+
const entry: { fetch: (request: Request) => Response } = { fetch: appFetch };
72+
const wrapped = wrapServerEntryWithSentryTunnel(entry, { path: '/monitor' });
73+
74+
const response = await wrapped.fetch(new Request('http://localhost:3000/monitor'));
75+
76+
expect(response.status).toBe(405);
77+
expect(response.headers.get('allow')).toBe('POST');
78+
expect(appFetch).not.toHaveBeenCalled();
79+
expect(await (await wrapped.fetch(new Request('http://localhost:3000/other'))).text()).toBe('app');
80+
});
81+
6982
it('passes other requests and their options to the app', async () => {
7083
setClient({});
7184
const request = makeEnvelopeRequest('http://localhost:3000/api/users');

0 commit comments

Comments
 (0)