-
Notifications
You must be signed in to change notification settings - Fork 41
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
Vite: Adding nonce in CSP response header and replace the nonce in index.html #751
Comments
@ia3andy there must be some clever way to add a RequestFilter on index.html to replace tokens? We could maybe have :
Which if detected would add a randomly generated NONCE UUID to the |
The solution would also need some way to replace the random generated nonce value in the CSP header itself. This header can become quite complex and the nonce will probably only be a part of the value. So I would imagine a way to replace the nonce placeholder when the response header is set in the properties:
|
@bdevos can you update this bare bones Vite Quinoa starter to have the CSP stuff in it (of course it will fail) so we have a test case. Minimal Vite Quinoa: vite-csp.zip |
I extended the bare bones starter with a vite config and the CSP response headers to test it. I hope it is OK that I made it available on Github: https://github.com/bdevos/vite-csp The changes are, this vite config: https://github.com/bdevos/vite-csp/blob/main/src/main/webui/vite.config.js And the CSP headers are in the properties: https://github.com/bdevos/vite-csp/blob/main/src/main/resources/application.properties I added some comments in the |
excellent! |
I don't know how to do this but it sounds like we need to:
|
I think this is out of the scope of Quinoa, this is server-side-rendering. I would suggest to give a try to the web-bundler where the index.html can actually be rendered using Qute. If you want to stick to Quinoa, then you should be able to introduce a vertx filter in your app code and tranform the index.html content in the response. |
Here is an example of accessing the vertx router to add stuff: https://github.com/quarkusio/code.quarkus.io/blob/main/base/src/main/java/io/quarkus/code/rest/DownloadRerouteFilter.java |
Thanks! I thought I had explored all my options, but I didn't know a Vert.x filter was possible. I will give it a try and report back on whether I was successful. |
If it is successful let us know I think we should add to docs as Vite is so popular and CSP is a good thing. |
@bdevos make any progress? |
@melloware I haven't been able to figure out how to read the Vite-generated public void init(@Observes Router router) {
router.route(HttpMethod.GET, "/").handler(ctx -> {
// Have to access the body of index.html so I can replace the NONCE_PLACEHOLDER
ctx.next();
});
} The problem is that I cannot read the body from the I tried to read the I do have several other options, but they all feel like hacks around the problem. For example, I could write the Quinoa bundle to a separate folder and serve everything myself, or I could use Quinoa for dev mode but build a separate frontend image for production. The coming week, I won't be working on this problem, but I will revisit it afterward to see if I can find a way to process this file within the route observer. Any suggestions, code examples, or documentation that could help me out would be greatly appreciated. |
Discussed in #750
Originally posted by bdevos September 3, 2024
I am currently transitioning a React frontend application to become part of the Quarkus backend, instead of using a separate Nginx container exclusively for the frontend.
The integration with Quarkus Quinoa seems really promising and it's been great for development thus far.
However, I encountered a challenge when attempting to add a
nonce
that is necessary for our Content Security Policy. I have implemented similar solutions in other applications and I'm conversant with the entire process when incorporating the frontend in a standalone Nginx container. Yet, I am unable to figure out how to implement this with Quinoa.What I seek is a way to filter the request that is handling
index.html
. Once that's done, I should be able to insert the nonce into the CSP response header and modify the HTML content set to return in the response.I've tried several filtering methods but none appear to give me access to the requests that Quinoa handles.
As the nonce is a requirement under my company's policy, skipping it isn't an option. I wonder if there's a simpler approach I might have overlooked. Alternatively, would it be smarter to run a development server with Quarkus Quinoa, and then build an Nginx image for the frontend during the release stage?
Vite: Its documentation for the
nonce
support is rather minimal, but works as expected: https://vitejs.dev/guide/features#nonce-randomIn Vite you can configure a nonce, but in order for that nonce to have any effect, it needs to be randomised on every response.
When in the Vite config you set:
The Vite build output in
index.html
will contain it in the script tag like this:What I do now with other applications, is that I replace that
NONCE_PLACEHOLDER
with a random nonce on every request before sending the response to the user. To make it all work the same random nonce has to be included in the CSP response header.The text was updated successfully, but these errors were encountered: