You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -48,7 +48,7 @@ This package handles all strict CSP conundrums for you and works for:
48
48
49
49
* pages with [`getServerSideProps`](https://nextjs.org/docs/basic-features/data-fetching/get-server-side-props) - **Nonce-based**
50
50
51
-
* pages with [`getStaticProps` + `revalidate` (ISR)]((https://vercel.com/docs/concepts/next.js/incremental-static-regeneration)) - **Hash-based**
51
+
* pages with [`getStaticProps` + `revalidate` (ISR)](https://vercel.com/docs/concepts/next.js/incremental-static-regeneration) - **Hash-based**
52
52
53
53
54
54
**This package always sets CSP as HTTP response header**. That enables violation reporting and report-only mode even for static pages. Plus, it provides a middleware and API handlers that make the setup of CSP violation reporting very easy.
@@ -92,27 +92,21 @@ create a file `middleware.js` in your Next.js project folder (or `pages/_middlew
Thats it. You should be all set now with a strict CSP for your Next.js app!
149
143
144
+
### Default CSP directives
145
+
I found this to be the most minimal yet sensible defaults
146
+
as base for a strict CSP:
147
+
148
+
```js
149
+
constdefaults= {
150
+
directives: {
151
+
'default-src': ['self'],
152
+
'object-src': ['none'],
153
+
'base-uri': ['none'],
154
+
},
155
+
isDev:process.env.NODE_ENV==='development',
156
+
reportOnly:!!process.env.CSP_REPORT_ONLY,
157
+
};
158
+
```
159
+
160
+
You can override a directive's default by setting values or unset it with an empty array:
161
+
162
+
```js
163
+
constcspMiddleware=csp({
164
+
directives: {
165
+
'default-src': [],
166
+
},
167
+
});
168
+
```
169
+
150
170
### Hash-based strict CSP for pages with Incremental Static Regeneration (ISR)
151
-
Add the following code to the top of every route with `getStaticProps` that uses `revalidate` (including the new `res.unstable_revalidate`/`res.revalidate` of on-demand ISR, available since Next 12.1):
171
+
Add the following code to the top of every route with `getStaticProps` that uses `revalidate` (including `res.revalidate` or `res.unstable_revalidate` (< 12.2) of on-demand ISR, available since Next 12.1):
152
172
153
173
```js
154
174
exportconstconfig= {
@@ -166,8 +186,10 @@ Furthermore, most middlewares, functions, parameters and types have JSDoc that i
166
186
add the `reporting` middleware to your middleware chain in `middleware.js` or `pages/_middleware.js`:
Sentry only supports the data format of the `report-uri` directive. It can't receive violation reports in `report-to` format (Google Chrome only serves `report-to`). `sentryCspReporterForEndpoint` does the necessary conversion, so you will receive violation reports from all major browsers in Sentry.
226
252
227
253
## How to add custom (inline) scripts that work with strict CSP?
228
-
Just add them with `next/script` on the pages where you need them. If you want to include a script in all pages, add them to your `pages/app.js`. For examples, have a look at [`apps/e2e/pages/_app.tsx`](https://github.com/nibtime/next-safe-middleware/blob/main/apps/e2e/pages/_document.tsx).
254
+
Just add them with `next/script` on the pages where you need them. If you want to include a script in all pages, add them to your `pages/app.js`.
255
+
256
+
The following files serve as examples for script usage:
Scripts with strategies `afterInteractive` and `lazyOnLoad` will become trusted by transitive trust propagation of `strict-dynamic` and so will be all scripts that they load dynamically, etc. That should cover the majority of use cases.
263
+
`<Script>`'s with strategies `afterInteractive` and `lazyOnLoad` will become trusted by transitive trust propagation of `strict-dynamic` and so will be all scripts that they load dynamically, etc. That should cover the majority of use cases.
232
264
233
-
Scripts with strategy `beforeInteractive` and scripts that are placed as children of `<Head>` in `_document.js` are picked up for CSP by this package.
265
+
`<Script>`'s with strategy `beforeInteractive`you place in `_document.js`and inline `<script>`'s you place as children of `<Head>` in `_document.js` are automatically picked up for strict CSP by this package.
234
266
235
267
What this package will do with such scripts, depends:
236
268
237
269
#### Pages with getServerSideProps (Nonce-based)
238
-
the script element will eventually receive the nonce.
270
+
the script will eventually receive the nonce.
239
271
240
272
#### Pages with getStaticProps (Hash-based)
241
-
1. The script loads from `src` has an integrity attribute: The integrity attribute/hash will be picked up for CSP.
273
+
1. The script loads from `src`and has an integrity attribute: The integrity attribute/hash will be picked up for CSP. Don't forget to set `{crossOrigin: "anonymous"}` in `next.config.js`, else the [SRI](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity) validation will fail.
242
274
243
275
2. The script loads from `src` and doesn't have an integrity attribute: The script will be replaced by an inline proxy script that loads the script. The hash of this proxy script will be picked up for CSP. The actual script eventually becomes trusted by transitive trust propagation of `strict-dynamic`.
244
276
@@ -261,19 +293,20 @@ For this you can use the `nextSafe` middleware that wraps the [`next-safe`](http
// that didn't get geo-blocked and only on requests for pages
385
+
exportdefaultchain(
386
+
geoBlockMiddleware,
387
+
chainMatch(isPageRequest)(...securityMiddleware)
388
+
);
348
389
```
349
390
350
391
### Can CSP/middleware configuration depend on request data?
351
392
Yes. In fact every middleware of this package supports configuration with an (async) initializer function, that receives the request as 1st param (in `req`), the currently set response of the middleware chain as 2nd (in `res`) and for convenience, a `uaParser` instance prepared with the user agent of the request as 3rd (from [`ua-parser-js`](https://www.npmjs.com/package/ua-parser-js), prebundled and minified with this package, for IntelliSense install `@types/ua-parser-js` in your project.
352
393
353
394
For example, you can use this capability to select different CSP configurations for different user agents:
395
+
354
396
```js
355
397
// middleware.js
356
398
import {
357
-
chain,
399
+
chainMatch,
400
+
isPageRequest,
358
401
csp,
359
402
strictDynamic,
360
-
} from'@next-safe/middleware';
403
+
} from"@next-safe/middleware";
361
404
362
-
// CSP in always in report-only mode for Firefox and by env var for other browsers
0 commit comments