How to setup csp and global headers with tanstack start? #3028
Replies: 6 comments 4 replies
-
@discoverlance-com I'm using a vinxi middleware to do this. Any feedback from the community is appreciated. ./app.config.ts import { defineConfig } from "@tanstack/start/config";
import tsConfigPaths from "vite-tsconfig-paths";
export default defineConfig({
vite: {
plugins: [
tsConfigPaths({
projects: ["./tsconfig.json"],
}),
],
},
routers: {
ssr: {
middleware: "./app/middleware/ssr.ts",
},
},
}); ./app/middleware/ssr.ts import { defineMiddleware, setResponseHeaders } from "vinxi/http";
export default defineMiddleware({
onBeforeResponse: (event) => {
setResponseHeaders(event, {
"X-Frame-Options": "DENY",
"X-Content-Type-Options": "nosniff",
"Referrer-Policy": "strict-origin-when-cross-origin",
"Permissions-Policy": "geolocation=(), camera=(), microphone=()",
"Content-Security-Policy": "default-src 'self'; script-src 'self' 'unsafe-inline'"
});
},
}); Don't have a solution for nonce's yet but probably it is a post-processing somewhere in the SSR handler, provided a request attribute is set with the correct nonce (pretty similar to the nextjs solution):
|
Beta Was this translation helpful? Give feedback.
-
I am hoping that tanstack start can be updated to support nonce like nextjs does: I think it may just be a change to Scripts component to take in a nonce parameter and render it in the script tags. |
Beta Was this translation helpful? Give feedback.
-
I think the library really needs to prioritise proper support for this. It's very important in a production app to have a content security policy in place. |
Beta Was this translation helpful? Give feedback.
-
Now that devinxi is in main, how can we handle CSP? Seems like #3028 (comment) is no longer an option |
Beta Was this translation helpful? Give feedback.
-
On my side, it works with this : // src/server.ts
import type { RequestHandler } from '@tanstack/react-start/server'
import {
createStartHandler,
defaultStreamHandler
} from '@tanstack/react-start/server'
import { createRouter } from './router'
const handler = createStartHandler({
createRouter
})(defaultStreamHandler)
const withHeaders: RequestHandler = async (context) => {
const response = await handler(context)
response.headers.set('Cross-Origin-Opener-Policy', 'same-origin')
response.headers.set('Cross-Origin-Resource-Policy', 'cross-origin')
return response
}
export default withHeaders |
Beta Was this translation helpful? Give feedback.
-
This will be added in the next week-ish as we prepare for RC |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This has been asked before in the discussion, #2476 but since there's no reply I am asking once more and with respect to tanstack start if anyone has an idea.
I want to configure some default http security headers like
x-frame-options
and such globally and also setup csp with a nonce if possible.How can I approach this and has anyone done it before?
Beta Was this translation helpful? Give feedback.
All reactions