Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(headers): explicit directives #466

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading