Skip to content

Commit

Permalink
Add additional CSP header configuration options (#2871)
Browse files Browse the repository at this point in the history
* Add additional CSP header configuration options

Added configuration options to extend the allowed lists for the
`child-src`, `font-src`, `form-action`, `frame-src`, `img-src`,
and `media-src` directives in the Content Security Policy (CSP)
headers. These changes enable compatibility with third-party tools,
such as Google Analytics.

* formatting
  • Loading branch information
kiancross authored Jan 2, 2025
1 parent 5629479 commit 1856646
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ QUEUE_CONNECTION=sync
SECURITY_HEADER_HSTS_ENABLE=false
SECURITY_HEADER_CSP_CONNECT_SRC=
SECURITY_HEADER_SCRIPT_SRC_ALLOW=
SECURITY_HEADER_CSP_CHILD_SRC=
SECURITY_HEADER_CSP_FONT_SRC=
SECURITY_HEADER_CSP_FORM_ACTION=
SECURITY_HEADER_CSP_FRAME_SRC=
SECURITY_HEADER_CSP_IMG_SRC=
SECURITY_HEADER_CSP_MEDIA_SRC=
SESSION_SECURE_COOKIE=false

REDIS_HOST=127.0.0.1
Expand Down
10 changes: 8 additions & 2 deletions config/secure-headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/child-src
'child-src' => [
'allow' => explode(',', (string) env('SECURITY_HEADER_CSP_CHILD_SRC', '')),
],

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/connect-src
Expand All @@ -359,11 +360,13 @@

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/font-src
'font-src' => [
'allow' => explode(',', (string) env('SECURITY_HEADER_CSP_FONT_SRC', '')),
],

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/form-action
'form-action' => [
'self' => true,
'allow' => explode(',', (string) env('SECURITY_HEADER_CSP_FORM_ACTION', '')),
],

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors
Expand All @@ -372,6 +375,7 @@

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src
'frame-src' => [
'allow' => explode(',', (string) env('SECURITY_HEADER_CSP_FRAME_SRC', '')),
],

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/img-src
Expand All @@ -398,7 +402,8 @@
[
// @phpstan-ignore-next-line
str_replace(parse_url(env('AWS_URL'), PHP_URL_PATH), '', env('AWS_URL')),
]
],
explode(',', (string) env('SECURITY_HEADER_CSP_IMG_SRC', ''))
),
],

Expand All @@ -418,7 +423,8 @@
[
// @phpstan-ignore-next-line
str_replace(parse_url(env('AWS_URL'), PHP_URL_PATH), '', env('AWS_URL')),
]
],
explode(',', (string) env('SECURITY_HEADER_CSP_MEDIA_SRC', ''))
),
],

Expand Down

0 comments on commit 1856646

Please sign in to comment.