Skip to content

Commit 8d85e55

Browse files
authored
Merge pull request #448 from Baroshem/chore/2.0.0-rc.1
Chore/2.0.0 rc.1
2 parents 8305b48 + 1bdb71d commit 8d85e55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+4278
-2667
lines changed

docs/content/1.documentation/1.getting-started/2.configuration.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ security: {
117117
ssg: {
118118
meta: true,
119119
hashScripts: true,
120-
hashStyles: false
120+
hashStyles: false,
121+
nitroHeaders: true,
122+
exportToPresets: true,
121123
},
122124
sri: true
123125
}

docs/content/1.documentation/1.getting-started/3.usage.md

+83-13
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ export default defineNitroPlugin((nitroApp) => {
102102
})
103103
```
104104

105+
::alert{type="warning"}
106+
Runtime-hook configuration only applies to headers delivered on HTML pages.
107+
<br>
108+
Headers delivered on other resources (e.g. images, js and css files, api routes etc.) are not modifiable via runtime hooks.
109+
::
110+
105111
## Configuration priority order
106112

107113
Nuxt-Security applies your rules in the following prority order:
@@ -256,31 +262,95 @@ export default defineNuxtConfig({
256262
```
257263

258264

259-
## Overwriting or modifying existing values
265+
## Modifying security options
260266

261-
Within your runtime hooks, you can either overwrite or modify the existing values for any security option.
262-
One of the easiest way to merge existing rules with your own is to use `defu`:
267+
Within your runtime hooks, you can either modify or overwrite the existing values for any security option.
268+
269+
### Merging with replacement
270+
271+
One of the easiest way to merge existing rules with your own is to use `defuReplaceArray`:
263272

264273
```ts{}[server/plugins/filename.ts]
265-
import defu from 'defu'
274+
// You don't need to import defuReplaceArray as it is auto-imported by Nuxt Security
266275
267276
export default defineNitroPlugin((nitroApp) => {
268277
nitroApp.hooks.hook('nuxt-security:routeRules', async(routeRules) => {
269-
// You can fetch configuration data asynchronously from an external source
270-
const validDomain = await $fetch('https://some-site.com/rules')
271-
// You can then override the security options of any route
278+
routeRules['/some/route'] = defuReplaceArray(
279+
{
280+
headers: {
281+
contentSecurityPolicy: {
282+
"script-src": ["'self'", "..."]
283+
// The script-src directive will be replaced with "'self' ..."
284+
}
285+
}
286+
},
287+
routeRules['/some/route'] // The other existing rules for /some/route will be preserved
288+
)
289+
})
290+
})
291+
```
292+
293+
In the example above,
294+
- All existing security options for `/some/route` will be maintained, and only the `script-src` CSP directive will be modified.
295+
- The existing content of the `script-src` directive will be erased and replaced by your values
296+
297+
Read more about [`defuReplaceArray`](/documentation/advanced/auto-imports/#defuReplaceArray)
298+
299+
::alert{type="info"}
300+
`defuReplaceArray` is auto-imported by Nuxt Security. You can use this utility anywhere in your /server folder.
301+
::
302+
303+
### Merging with addition
304+
305+
If you want to add additional values to the existing settings, you can use the standard `defu` utility to merge your rules.
306+
307+
```ts{}[server/plugins/filename.ts]
308+
// You will need to import defu
309+
import { defu } from 'defu'
310+
export default defineNitroPlugin((nitroApp) => {
311+
nitroApp.hooks.hook('nuxt-security:routeRules', async(routeRules) => {
272312
routeRules['/some/route'] = defu(
273313
{
274314
headers: {
275315
contentSecurityPolicy: {
276-
"connect-src": ["'self'", validDomain]
277-
},
278-
xFrameOptions: false
279-
},
280-
hidePoweredBy: false
316+
"script-src": ["'self'", "..."]
317+
// The values "'self' ..." will be added to the existing values
318+
}
319+
}
281320
},
282-
routeRules['/some/route']
321+
routeRules['/some/route'] // The other existing rules for /some/route will be preserved
283322
)
284323
})
285324
})
286325
```
326+
327+
In the example above,
328+
- All existing security options for `/some/route` will be maintained, and only the `script-src` CSP directive will be modified.
329+
- The existing content of the `script-src` directive will be preserved, and your values will be added to the existing values.
330+
331+
Read more about [`defu`](https://github.com/unjs/defu)
332+
333+
334+
### Overwriting rules
335+
336+
If you want to erase the existing settings, don't use defu and overwrite the values:
337+
338+
```ts{}[server/plugins/filename.ts]
339+
export default defineNitroPlugin((nitroApp) => {
340+
nitroApp.hooks.hook('nuxt-security:routeRules', async(routeRules) => {
341+
routeRules['/some/route'] = {
342+
headers: {
343+
contentSecurityPolicy: {
344+
"script-src": ["'self'", "..."]
345+
}
346+
}
347+
}
348+
// Any existing rules for /some/route will be erased
349+
})
350+
})
351+
```
352+
353+
In the example above,
354+
- All existing security options for `/some/route` will be erased.
355+
- The `script-src` directive will contain your values.
356+

docs/content/1.documentation/2.headers/1.csp.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ contentSecurityPolicy: {
7575
'sandbox'?: CSPSandboxValue[] | false;
7676
'form-action'?: CSPSourceValue[] | false;
7777
'frame-ancestors'?: ("'self'" | "'none'" | string)[] | false;
78-
'navigate-to'?: ("'self'" | "'none'" | "'unsafe-allow-redirects'" | string)[] | false;
7978
'report-uri'?: string[] | false;
8079
'report-to'?: string | false;
8180
'upgrade-insecure-requests'?: boolean;
@@ -255,6 +254,7 @@ export default defineNuxtConfig({
255254
meta: true, // Enables CSP as a meta tag in SSG mode
256255
hashScripts: true, // Enables CSP hash support for scripts in SSG mode
257256
hashStyles: false // Disables CSP hash support for styles in SSG mode (recommended)
257+
exportToPresets: true // Export security headers to Nitro presets
258258
},
259259
sri: true,
260260
headers: {

docs/content/1.documentation/5.advanced/2.faq.md

-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,6 @@ If you want to expose your app in local network to test it by using other device
385385
security: {
386386
headers: {
387387
crossOriginEmbedderPolicy: process.env.NODE_ENV === 'development' ? 'unsafe-none' : 'require-corp', //https://github.com/Baroshem/nuxt-security/issues/101
388-
strictTransportSecurity: true,
389388
contentSecurityPolicy: {
390389
"upgrade-insecure-requests": process.env.NODE_ENV === 'development' ? false : true // USE ONLY IN DEV MODE
391390
}

docs/content/1.documentation/5.advanced/3.strict-csp.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -669,16 +669,42 @@ Nuxt Security uses a different approach, depending on whether SSR or SSG is used
669669
670670
**CSP Headers for SSG via Nitro Presets**
671671
672+
Nuxt Security supports CSP via HTTP headers for Nitro Presets that generate HTTP headers.
673+
672674
When using the SSG mode, some static hosting services such as Vercel or Netlify provide the ability to specify a configuration file that governs the value of the headers that will be generated. When these hosting services benefit from a [Nitro Preset](https://nitro.unjs.io/deploy/#overview), it is possible for Nuxt Security to predict the value of the CSP headers for each page and write the value to the configuration file.
673675
674-
Nuxt Security supports CSP via HTTP headers for Nitro Presets that output HTTP headers.
676+
This feature is enabled by default with the `ssg: exportToPresets` option.
675677
676678
::alert{type="info"}
677679
If you deploy your SSG site on Vercel or Netlify, you will benefit automatically from CSP Headers.
678680
<br>
679681
CSP will be delivered via HTTP headers, in addition to the standard `<meta http-equiv>` approach. If you want to disable the meta tag, so that only the HTTP headers are used, you can do so with the `ssg: meta` option.
680682
::
681683
684+
**CSP Headers for SSG via `prerenderedHeaders` hook**
685+
686+
Nuxt Security allows you to generate your own headers rules with the `nuxt-security:prerenderedHeaders` buildtime hook.
687+
688+
If you do not deploy with a Nitro preset, or if you have specific requirements that are not met by the `ssg: exportToPresets` default, you can use this hook to generate your headers configuration file yourself.
689+
690+
See our documentation on the [prerenderedPages hook](/documentation/advanced/hooks/#prerendered-headers-hook)
691+
692+
::alert{type="info"}
693+
This will allow you to deliver CSP via HTTP headers, in addition to the standard `<meta http-equiv>` approach.
694+
::
695+
696+
**CSP Headers for Hybrid Pre-Rendered Pages**
697+
698+
Nuxt Security supports CSP via HTTP headers for pre-rendered pages of Hybrid applications.
699+
700+
This feature is enabled by default with the `ssg: nitroHeaders` option.
701+
702+
::alert{type="info"}
703+
In Hybrid applications, CSP of pre-rendered pages will be delivered via HTTP headers, in addition to the standard `<meta http-equiv>` approach.
704+
<br>
705+
If you want to disable the meta tag, so that only the HTTP headers are used, you can do so with the `ssg: meta` option.
706+
::
707+
682708
### Per Route CSP
683709
684710
Nuxt Security gives you the ability to define per-route CSP. For instance, you can have Strict CSP on the admin section of your application, and a more relaxed policy on the blog section.
@@ -718,6 +744,8 @@ export default defineNuxtConfig({
718744
meta: true, // Enables CSP as a meta tag in SSG mode
719745
hashScripts: true, // Enables CSP hash support for scripts in SSG mode
720746
hashStyles: false // Disables CSP hash support for styles in SSG mode (recommended)
747+
nitroHeaders: true // Allow Nitro to serve security headers for pre-rendered routes
748+
exportToPresets: true // Export pre-rendered security headers to Nitro presets
721749
},
722750
// You can use nonce and ssg simultaneously
723751
// Nuxt Security will take care of choosing the adequate parameters when you build for either SSR or SSG

0 commit comments

Comments
 (0)