Closed renet closed 2 years ago
Hi @renet
the problem is that processing other sources than inline scripts and styles isn't implemented yet :).
Regarding next/image
: I also realized that automatic processing there would be great during setting up the docs project. I added everything to CSP manually and that felt pretty redundant.
I am currently working on it (#40).
When I am done with that issue, there will be a file .next/static/~csp/csp-manifest.json
that contains all trustable sources identified during the build process.
Middleware can then read it and is the easiest and most versatile configuration option that uses the manifest. I also intend to split up the package and support others (see https://github.com/nibtime/next-safe-middleware/discussions/60#discussioncomment-3259782).
I was a bit frustrated after the Next 12.2 update, as it introduced a really bad regression to routing together with middleware (even with empty) that also makes the CSP features as a whole unusable when the middleware is the only configuration option. Most code is all the SSR stuff and that can support any version with next/script
, so >= 11 and I don't have to keep up with the latest Next versions all the time.
For clarification: what do you expect the resulting CSP to look like if it was handled automatically?
I expect to have a lot of sha256 hashes in the img-src
and style-src
directives of the CSP. But to be honest, I'm pretty new to the CSP topic, so please don't take my expectations as base for your development. :)
Thanks, this sounds great. Looking forward to the upcoming releases! I already stumbled upon your discussions thread and at least can understand your frustration. I really hope it's not that much of a hassle with future Next.js releases.
Haha no worries, I won't take it verbatim :). But getting good feedback helps to get ideas for improvements. I am also not an expert on CSPs or something, I made this package for myself and all of the sudden people show up here and use it :) But by doing stuff you learn it.
Hi @renet,
I am closing this, I put the possible improvements to my backlog, I will have a look at them sometime after the transition to multiple packages has been done.
What I can really recommend you for stylesheets and images is a semi-automatic approach with a CSP recording tool like Laboratory for Firefox: https://next-safe-middleware.vercel.app/guides/strict-csp-configuration#csp-converter. (Firefox will also be supported for Hash-based Strict CSP in the upcoming version).
I tried this for the docs website and it could have saved me a lot of time. The extension has several recording options for sources, e.g. it can record images or stylesheets by full path. I really like it, it is maybe even preferable to a fully automated approach, since it can accommodate individual requirements better.
In our project, we import some global css files (tailwind, which is dynamically generated at build time, and other static CSS files from dependencies as well as a locally stored global.css file) in the
_app.tsx
. But they seem not to be recognized by the CSP middleware this way. The imports look like this:I tried moving them to the
_document.tsx
, but as soon as I do this, I get the following error at build time:Same goes for some images that are placed in the public folder and referenced in the app using the
next/image
component (which seems to inline the images).What would be the proper way to add those files so that they get recognized by the middleware?
Thanks a lot! :)