A free open source IT asset/license management system
Fixed #19914 - add base_uri and frame-ancestors to CSP
snipe52f48744ae46
diff --git a/app/Http/Middleware/SecurityHeaders.php b/app/Http/Middleware/SecurityHeaders.phpindex 1a988a33d6..96ae090714 100644--- a/app/Http/Middleware/SecurityHeaders.php+++ b/app/Http/Middleware/SecurityHeaders.php@@ -45,7 +45,6 @@ class SecurityHeaders$feature_policy[] = "autoplay 'none'";$feature_policy[] = "camera 'none'";$feature_policy[] = "display-capture 'none'";- $feature_policy[] = "document-domain 'none'";$feature_policy[] = "encrypted-media 'none'";$feature_policy[] = "fullscreen 'none'";$feature_policy[] = "geolocation 'none'";@@ -89,6 +88,10 @@ class SecurityHeaders$csp_policy[] = "object-src 'none'";$csp_policy[] = "font-src 'self' data:";$csp_policy[] = "img-src 'self' data: ".config('app.url').' '.config('app.additional_csp_urls').' '.config('filesystems.disks.public.url').' https://secure.gravatar.com http://gravatar.com maps.google.com maps.gstatic.com *.googleapis.com';+ $csp_policy[] = "base-uri 'self'";+ if (config('app.allow_iframing') == false) {+ $csp_policy[] = "frame-ancestors 'none'";+ }$csp_policy = implode(';', $csp_policy);diff --git a/tests/Feature/Security/SecurityHeadersCspTest.php b/tests/Feature/Security/SecurityHeadersCspTest.phpnew file mode 100644index 0000000000..146c0a1a97--- /dev/null+++ b/tests/Feature/Security/SecurityHeadersCspTest.php@@ -0,0 +1,70 @@+<?php++namespace Tests\Feature\Security;++use App\Http\Middleware\SecurityHeaders;+use Illuminate\Http\Request;+use Illuminate\Http\Response;+use Tests\TestCase;++class SecurityHeadersCspTest extends TestCase+{+ protected function setUp(): void+ {+ parent::setUp();++ // SecurityHeaders emits the CSP header only when debug is off+ // and ENABLE_CSP is on. Flip both so the test exercises the+ // real header-emission branch.+ config([+ 'app.debug' => false,+ 'app.enable_csp' => 'true',+ ]);+ }++ private function runMiddleware(): Response+ {+ return (new SecurityHeaders)->handle(Request::create('/'), fn () => new Response('ok'));+ }++ public function test_csp_sets_base_uri_to_self(): void+ {+ $csp = $this->runMiddleware()->headers->get('Content-Security-Policy');++ $this->assertNotNull($csp, 'SecurityHeaders middleware must emit a CSP header when enable_csp is true.');+ $this->assertStringContainsString("base-uri 'self'", $csp);+ }++ public function test_csp_sets_frame_ancestors_none_when_iframing_disallowed(): void+ {+ config(['app.allow_iframing' => false]);++ $csp = $this->runMiddleware()->headers->get('Content-Security-Policy');++ $this->assertStringContainsString("frame-ancestors 'none'", $csp);+ }++ public function test_csp_omits_frame_ancestors_when_iframing_allowed(): void+ {+ // When ALLOW_IFRAMING is on, admins intentionally embed+ // Snipe-IT in a parent portal. Emitting frame-ancestors here+ // would re-break the intentional iframe.+ config(['app.allow_iframing' => true]);++ $csp = $this->runMiddleware()->headers->get('Content-Security-Policy');++ $this->assertStringNotContainsString('frame-ancestors', $csp);+ }++ public function test_csp_header_is_not_emitted_when_disabled(): void+ {+ config(['app.enable_csp' => false]);++ $csp = $this->runMiddleware()->headers->get('Content-Security-Policy');++ $this->assertNull(+ $csp,+ 'ENABLE_CSP=false must suppress the CSP header entirely so admins can opt out.',+ );+ }+}