Skip to content

Commit

Permalink
prefer explicit CSP directives
Browse files Browse the repository at this point in the history
  • Loading branch information
vejja committed May 31, 2024
1 parent 38192f3 commit 54abe53
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,21 @@ security: {
crossOriginEmbedderPolicy: 'require-corp',
contentSecurityPolicy: {
'base-uri': ["'none'"],
'default-src': ["'self'"],
'default-src' : ["'none'"],
'connect-src': ["'self'", 'https:'],
'font-src': ["'self'", 'https:', 'data:'],
'form-action': ["'self'"],
'frame-ancestors': ["'self'"],
'frame-src': ["'self'"],
'img-src': ["'self'", 'data:'],
'manifest-src': ["'self'"],
'media-src': ["'self'"],
'object-src': ["'none'"],
'script-src-attr': ["'none'"],
'style-src': ["'self'", 'https:', "'unsafe-inline'"],
'script-src': ["'self'", 'https:', "'unsafe-inline'", "'strict-dynamic'", "'nonce-{{nonce}}'"],
'upgrade-insecure-requests': true
'upgrade-insecure-requests': true,
'worker-src': ["'self'"],
},
originAgentCluster: '?1',
referrerPolicy: 'no-referrer',
Expand Down
2 changes: 1 addition & 1 deletion docs/content/1.documentation/2.headers/1.csp.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ You can also disable this header by `contentSecurityPolicy: false`.
By default, Nuxt Security will set following value for this header:

```http
Content-Security-Policy: base-uri 'none'; default-src 'self'; connect-src 'self', https:; font-src 'self' https: data:; img-src 'self' data:; object-src 'none'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; script-src 'self' https: 'unsafe-inline' 'strict-dynamic' 'nonce-{{nonce}}'; upgrade-insecure-requests;
Content-Security-Policy: base-uri 'none'; default-src 'none'; connect-src 'self' https:; font-src 'self' https: data:; form-action 'self'; frame-ancestors 'self'; frame-src 'self'; img-src 'self' data:; manifest-src 'self'; media-src 'self'; object-src 'none'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; script-src 'self' https: 'unsafe-inline' 'strict-dynamic' 'nonce-{{nonce}}'; upgrade-insecure-requests; worker-src 'self';
```

## Available values
Expand Down
8 changes: 6 additions & 2 deletions docs/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ export default defineNuxtConfig({
headers: {
contentSecurityPolicy: {
'img-src': ["'self'", "data:", 'https:'], // Allow https: external images
'connect-src': process.env.NODE_ENV === 'development' ? ["'self'", 'https:', 'ws:'] : ["'self'", 'https:'], // Allow self and image api
'frame-src': ['https://www.youtube-nocookie.com', 'https://stackblitz.com'], // Allow self and youtube and stackblitz iframes
'connect-src': process.env.NODE_ENV === 'development' ? ["'self'", 'https:', 'ws:'] : ["'self'", 'https:'], // Allow websocket in dev mode
'frame-src': ['https://www.youtube-nocookie.com', 'https://stackblitz.com'], // Allow youtube and stackblitz iframes
},
permissionsPolicy: {
"picture-in-picture": ['self', '"https://www.youtube-nocookie.com"'], // Allow picture-in-picture for youtube
"fullscreen": ['self', '"https://www.youtube-nocookie.com"'], // Allow fullscreen for youtube
},
crossOriginEmbedderPolicy: 'unsafe-none', // Allow youtube and stackblitz iframes
}
Expand Down
10 changes: 8 additions & 2 deletions src/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ export const defaultSecurityConfig = (serverlUrl: string): ModuleOptions => ({
crossOriginEmbedderPolicy: 'require-corp',
contentSecurityPolicy: {
'base-uri': ["'none'"],
'default-src' : ["'self'"],
'default-src' : ["'none'"],
'connect-src': ["'self'", 'https:'],
'font-src': ["'self'", 'https:', 'data:'],
'form-action': ["'self'"],
'frame-ancestors': ["'self'"],
'frame-src': ["'self'"],
'img-src': ["'self'", 'data:'],
'manifest-src': ["'self'"],
'media-src': ["'self'"],
'object-src': ["'none'"],
'script-src-attr': ["'none'"],
'style-src': ["'self'", 'https:', "'unsafe-inline'"],
'script-src': ["'self'", 'https:', "'unsafe-inline'", "'strict-dynamic'", "'nonce-{{nonce}}'"],
'upgrade-insecure-requests': true
'upgrade-insecure-requests': true,
'worker-src': ["'self'"],
},
originAgentCluster: '?1',
referrerPolicy: 'no-referrer',
Expand Down
2 changes: 1 addition & 1 deletion test/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('[nuxt-security] Headers', async () => {
expect(cspHeaderValue).toBeTruthy()
expect(nonceValue).toBeDefined()
expect(nonceValue).toHaveLength(24)
expect(cspHeaderValue).toBe(`base-uri 'none'; default-src 'self'; connect-src 'self' https:; font-src 'self' https: data:; img-src 'self' data:; object-src 'none'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; script-src 'self' https: 'unsafe-inline' 'strict-dynamic' 'nonce-${nonceValue}'; upgrade-insecure-requests;`)
expect(cspHeaderValue).toBe(`base-uri 'none'; default-src 'none'; connect-src 'self' https:; font-src 'self' https: data:; form-action 'self'; frame-ancestors 'self'; frame-src 'self'; img-src 'self' data:; manifest-src 'self'; media-src 'self'; object-src 'none'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; script-src 'self' https: 'unsafe-inline' 'strict-dynamic' 'nonce-${nonceValue}'; upgrade-insecure-requests; worker-src 'self';`)
})

it('has `cross-origin-embedder-policy` header set with correct default value', async () => {
Expand Down
Loading

0 comments on commit 54abe53

Please sign in to comment.