Repository navigation
test(tanstackstart-react): Add CSRF middleware to the e2e fixtures #26451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 5 additions & 2 deletions
7
dev-packages/e2e-tests/test-applications/tanstackstart-react-cloudflare/src/start.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| import { sentryGlobalFunctionMiddleware, sentryGlobalRequestMiddleware } from '@sentry/tanstackstart-react'; | ||
| import { createStart } from '@tanstack/react-start'; | ||
| import { createCsrfMiddleware, createStart } from '@tanstack/react-start'; | ||
|
|
||
| // A custom `start.ts` turns off the CSRF protection that Start installs for server functions by default. | ||
| const csrfMiddleware = createCsrfMiddleware({ filter: ctx => ctx.handlerType === 'serverFn' }); | ||
|
|
||
| export const startInstance = createStart(() => { | ||
| return { | ||
| requestMiddleware: [sentryGlobalRequestMiddleware], | ||
| requestMiddleware: [sentryGlobalRequestMiddleware, csrfMiddleware], | ||
| functionMiddleware: [sentryGlobalFunctionMiddleware], | ||
| }; | ||
| }); |
7 changes: 5 additions & 2 deletions
7
dev-packages/e2e-tests/test-applications/tanstackstart-react-static/app/start.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| import { sentryGlobalFunctionMiddleware, sentryGlobalRequestMiddleware } from '@sentry/tanstackstart-react'; | ||
| import { createStart } from '@tanstack/react-start'; | ||
| import { createCsrfMiddleware, createStart } from '@tanstack/react-start'; | ||
| // NOTE: These are NOT wrapped - auto-instrumentation via the Vite plugin will wrap them | ||
| import { globalFunctionMiddleware, globalRequestMiddleware } from './middleware'; | ||
|
|
||
| // A custom `start.ts` turns off the CSRF protection that Start installs for server functions by default. | ||
| const csrfMiddleware = createCsrfMiddleware({ filter: ctx => ctx.handlerType === 'serverFn' }); | ||
|
|
||
| export const startInstance = createStart(() => { | ||
| return { | ||
| requestMiddleware: [sentryGlobalRequestMiddleware, globalRequestMiddleware], | ||
| requestMiddleware: [sentryGlobalRequestMiddleware, csrfMiddleware, globalRequestMiddleware], | ||
| functionMiddleware: [sentryGlobalFunctionMiddleware, globalFunctionMiddleware], | ||
| }; | ||
| }); |
7 changes: 5 additions & 2 deletions
7
dev-packages/e2e-tests/test-applications/tanstackstart-react/src/start.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| import { sentryGlobalFunctionMiddleware, sentryGlobalRequestMiddleware } from '@sentry/tanstackstart-react'; | ||
| import { createStart } from '@tanstack/react-start'; | ||
| import { createCsrfMiddleware, createStart } from '@tanstack/react-start'; | ||
| // NOTE: These are NOT wrapped - auto-instrumentation via the Vite plugin will wrap them | ||
| import { globalFunctionMiddleware, globalRequestMiddleware } from './middleware'; | ||
|
|
||
| // A custom `start.ts` turns off the CSRF protection that Start installs for server functions by default. | ||
| const csrfMiddleware = createCsrfMiddleware({ filter: ctx => ctx.handlerType === 'serverFn' }); | ||
|
|
||
| export const startInstance = createStart(() => { | ||
| return { | ||
| requestMiddleware: [sentryGlobalRequestMiddleware, globalRequestMiddleware], | ||
| requestMiddleware: [sentryGlobalRequestMiddleware, csrfMiddleware, globalRequestMiddleware], | ||
| functionMiddleware: [sentryGlobalFunctionMiddleware, globalFunctionMiddleware], | ||
| }; | ||
| }); |
45 changes: 45 additions & 0 deletions
45
dev-packages/e2e-tests/test-applications/tanstackstart-react/tests/csrf.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import type { APIRequestContext, Page, Request } from '@playwright/test'; | ||
|
|
||
| async function captureServerFnRequest(page: Page): Promise<Request> { | ||
| const serverFnRequestPromise = page.waitForRequest(request => | ||
| new URL(request.url()).pathname.startsWith('/_serverFn/'), | ||
| ); | ||
|
|
||
| await page.goto('/test-serverFn'); | ||
| await page.locator('html[data-hydrated="true"]').waitFor(); | ||
| await page.getByText('Call server function', { exact: true }).click(); | ||
|
|
||
| return serverFnRequestPromise; | ||
| } | ||
|
|
||
| async function replayWithFetchSite(request: APIRequestContext, serverFnRequest: Request, fetchSite: string) { | ||
| return request.fetch(serverFnRequest.url(), { | ||
| method: serverFnRequest.method(), | ||
| headers: { ...(await serverFnRequest.allHeaders()), 'sec-fetch-site': fetchSite }, | ||
| data: serverFnRequest.postDataBuffer() ?? undefined, | ||
| }); | ||
| } | ||
|
|
||
| test('Blocks a cross-site server function request', async ({ page, request }) => { | ||
| const serverFnRequest = await captureServerFnRequest(page); | ||
|
|
||
| const response = await replayWithFetchSite(request, serverFnRequest, 'cross-site'); | ||
|
|
||
| expect(response.status()).toBe(403); | ||
| }); | ||
|
|
||
| test('Allows a same-origin server function request', async ({ page, request }) => { | ||
| const serverFnRequest = await captureServerFnRequest(page); | ||
|
|
||
| const response = await replayWithFetchSite(request, serverFnRequest, 'same-origin'); | ||
|
|
||
| expect(response.status()).toBe(200); | ||
| }); | ||
|
|
||
| test('Does not block a cross-site request to a server route', async ({ request }) => { | ||
| const response = await request.get('/api/user/123', { headers: { 'sec-fetch-site': 'cross-site' } }); | ||
|
|
||
| expect(response.status()).toBe(200); | ||
| expect(await response.json()).toEqual({ id: '123' }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.