Why I realized publishing a lib like this might be a bad idea ... [and how it could get better] #60
Replies: 5 comments
-
I gave it some thought and I think the main problem regarding maintainability is that it is the single package that depends on latest Next as a whole. I am working on a PR to refactor the lib atm and split it up into many small modules. That paves the way for splitting up the lib in smaller packages that can have different peer dep requirements. While doing, I also saw potential to support more setups/versions of Next and a good way to support This way I can put out major releases gradually for the stuff that works and is tested and keep the more experimental packages in the minors. When I put out a major release for something, I won't change public APIs/functions anymore and obey to SemVer, while with Edit: I converted the package design to an issue: #66 |
Beta Was this translation helpful? Give feedback.
-
Just a very basic question about the middleware and how I understand how it can be used: According to the Next.js documentation, the middleware gives you a I know this might be a stupid question and I have to admit that I'm new to CSP, haven't been testing |
Beta Was this translation helpful? Give feedback.
-
On the middleware part
The advantages of the Next integration over google/strict-csp
That being said, I want to support other configuration options than middleware. All the above stuff would become the @strict-csp/next-ssr package. The @strict-csp/next-headers will take some ideas from strict-csp and be an additional configuration option to decouple configuration from middleware and the latest Next release cycles, but with the above-mentioned advantages. What I learned from this package is, that I wouldn't have created it, if I knew how much work it would be :). I went into it and thought just hash/process some HTML with cheerio, np but getting it to work reliably took me ages, because there are strange edge cases all over the place. In general, expecting HTML files to be present for processing limits the usage of Next.js fundamentally, as it means that you had to refrain from all revalidation features (ISR). You had to decide between the two, as all responses to revalidate requests won't include the processing and be completely broken by CSP blocking. A good reference for HTML processing with cheerio can be found here: vercel/next.js#23993 (comment). The lib is doing something similar for inline styles already, also with cheerio, but in a way that does not rely on HTML files, it processes Update: I tried out the Output of google/strict-csp with the e2e app<!DOCTYPE html>
<html>
<head>
<meta
http-equiv="Content-Security-Policy"
content="script-src 'strict-dynamic' 'sha256-bDS42aVXSmZ3nfsEEK39yVG6qKbrptYpCZpPiyH9Rc0=';object-src 'none';base-uri 'self';"
/>
<meta charset="utf-8" />
<title>Webpack App</title>
<meta name="viewport" content="width=device-width,initial-scale=1" />
</head>
<body>
<script>
var scripts = ["/_next/edge-runtime-webpack.js", "/_next/middleware.js"];
scripts.forEach(function (scriptUrl) {
var s = document.createElement("script");
s.src = scriptUrl;
s.async = false; // preserve execution order.
document.body.appendChild(s);
});
</script>
</body>
</html> |
Beta Was this translation helpful? Give feedback.
-
I discovered this: https://github.com/nessjs/ness, as it also has a CSP integration for Next. That looks like a great option to host Next.js independent from Vercel at a good budget. I will test to see if it works with middleware and if I can integrate it with the Strict CSP features of this lib. |
Beta Was this translation helpful? Give feedback.
-
Im really glad i found this lib as it helped me a lot with CSP in Nextjs (problem is ongoing tho). I would suggest that we point to this announcement in the beginning of readme. |
Beta Was this translation helpful? Give feedback.
-
... because there are parts that require integration with framework internals that can change any upcoming canary build without notice and be released with any patch version and break things for this lib.
I realized this with the latest Next 12.2 updates. There has been a huge load of internal changes that broke the lib, and as they are internal they don't obey SemVer and ensure backward compatibility in any way. It will be impossible for me to maintain something like this.
I will keep observing this for a bit. If those changes were exceptional due to the transition to stable middleware, experimental-edge runtime, RSC/Streaming, etc. I will keep the lib public and continue to maintain it.
If they are the rule, I will convert this to a private project and package and maintain it for myself and my colleagues, so I don't have to obey the rules of a publicly available lib. It is a security lib after all, and if establishing stability should be incompatible with the Next.js release cycle, I think it is unfair to offer it at all.
That's why I think it would be better if Next.js offered this at the framework level, where it is maintained and tested along the other framework internals, like other popular site generators already do:
https://kit.svelte.dev/docs/configuration#csp
https://nuxtjs.org/docs/configuration-glossary/configuration-render/#csp
Alternatives
https://github.com/google/strict-csp
https://github.com/guydumais/next-strict-csp
Beta Was this translation helpful? Give feedback.
All reactions