@@ -868,13 +868,45 @@ public function testSandboxMixedModeEnforcedAndReportOnly(): void
868868 $ this ->assertContains ('sandbox allow-scripts ' , $ this ->getCspDirectives ($ reportOnly ));
869869 }
870870
871+ #[PreserveGlobalState(false )]
872+ #[RunInSeparateProcess]
873+ public function testOptionMovedBetweenReportingModesPreservesEmptyMarker (): void
874+ {
875+ $ this ->csp ->addSandbox ('' , false );
876+ $ this ->csp ->addSandbox ('allow-scripts ' , false );
877+ $ this ->csp ->addSandbox ('allow-scripts ' , true );
878+
879+ $ this ->csp ->addFrameAncestor ('' , false );
880+ $ this ->csp ->addFrameAncestor ('https://example.com ' , false );
881+ $ this ->csp ->addFrameAncestor ('https://example.com ' , true );
882+
883+ $ this ->assertTrue ($ this ->work ());
884+
885+ $ enforced = $ this ->getHeaderEmitted ('Content-Security-Policy ' );
886+ $ this ->assertIsString ($ enforced );
887+ $ enforcedDirectives = $ this ->getCspDirectives ($ enforced );
888+ $ this ->assertContains ('sandbox ' , $ enforcedDirectives );
889+ $ this ->assertContains ('frame-ancestors ' , $ enforcedDirectives );
890+ $ this ->assertNotContains ('sandbox allow-scripts ' , $ enforcedDirectives );
891+ $ this ->assertNotContains ('frame-ancestors https://example.com ' , $ enforcedDirectives );
892+
893+ $ reportOnly = $ this ->getHeaderEmitted ('Content-Security-Policy-Report-Only ' );
894+ $ this ->assertIsString ($ reportOnly );
895+ $ reportOnlyDirectives = $ this ->getCspDirectives ($ reportOnly );
896+ $ this ->assertContains ('sandbox allow-scripts ' , $ reportOnlyDirectives );
897+ $ this ->assertContains ('frame-ancestors https://example.com ' , $ reportOnlyDirectives );
898+ }
899+
871900 #[PreserveGlobalState(false )]
872901 #[RunInSeparateProcess]
873902 public function testConfiguredEmptyDirectivesEmitted (): void
874903 {
875904 $ config = new CSPConfig ();
876905 $ config ->sandbox = '' ;
877906 $ config ->frameAncestors = '' ;
907+ $ config ->formAction = '' ;
908+ $ config ->fontSrc = '' ;
909+ $ config ->connectSrc = '' ;
878910
879911 $ csp = new ContentSecurityPolicy ($ config );
880912
@@ -886,6 +918,73 @@ public function testConfiguredEmptyDirectivesEmitted(): void
886918 $ directives = $ this ->getCspDirectives ($ response ->getHeaderLine ('Content-Security-Policy ' ));
887919 $ this ->assertContains ('sandbox ' , $ directives );
888920 $ this ->assertContains ('frame-ancestors ' , $ directives );
921+ $ this ->assertContains ('form-action ' , $ directives );
922+ $ this ->assertContains ('font-src ' , $ directives );
923+ $ this ->assertContains ('connect-src ' , $ directives );
924+ }
925+
926+ #[PreserveGlobalState(false )]
927+ #[RunInSeparateProcess]
928+ public function testConfiguredEmptyFormActionWithAddFormActionNoop (): void
929+ {
930+ $ config = new CSPConfig ();
931+ $ config ->formAction = '' ;
932+
933+ $ csp = new ContentSecurityPolicy ($ config );
934+
935+ $ response = new Response (config (App::class));
936+ $ response ->pretend (true );
937+
938+ $ csp ->addFormAction ([]);
939+ $ csp ->finalize ($ response );
940+
941+ $ directives = $ this ->getCspDirectives ($ response ->getHeaderLine ('Content-Security-Policy ' ));
942+ $ this ->assertContains ('form-action ' , $ directives );
943+ }
944+
945+ #[PreserveGlobalState(false )]
946+ #[RunInSeparateProcess]
947+ public function testClearDirectiveThenAddEmptySource (): void
948+ {
949+ $ this ->csp ->clearDirective ('form-action ' );
950+ $ this ->csp ->addFormAction ('' );
951+ $ this ->csp ->clearDirective ('connect-src ' );
952+ $ this ->csp ->addConnectSrc ('' );
953+ $ this ->assertTrue ($ this ->work ());
954+
955+ $ header = $ this ->getHeaderEmitted ('Content-Security-Policy ' );
956+ $ this ->assertIsString ($ header );
957+ $ directives = $ this ->getCspDirectives ($ header );
958+ $ this ->assertContains ('form-action ' , $ directives );
959+ $ this ->assertContains ('connect-src ' , $ directives );
960+ }
961+
962+ #[PreserveGlobalState(false )]
963+ #[RunInSeparateProcess]
964+ public function testAddEmptySourceToFontSrc (): void
965+ {
966+ $ this ->csp ->addFontSrc ('' );
967+ $ this ->assertTrue ($ this ->work ());
968+
969+ $ header = $ this ->getHeaderEmitted ('Content-Security-Policy ' );
970+ $ this ->assertIsString ($ header );
971+ $ directives = $ this ->getCspDirectives ($ header );
972+ $ this ->assertContains ('font-src ' , $ directives );
973+ }
974+
975+ #[PreserveGlobalState(false )]
976+ #[RunInSeparateProcess]
977+ public function testRedundantEmptySourceFilteredWhenOtherSourcesPresent (): void
978+ {
979+ // connectSrc defaults to 'self'
980+ $ this ->csp ->addConnectSrc ('' );
981+ $ this ->assertTrue ($ this ->work ());
982+
983+ $ header = $ this ->getHeaderEmitted ('Content-Security-Policy ' );
984+ $ this ->assertIsString ($ header );
985+ $ directives = $ this ->getCspDirectives ($ header );
986+ $ this ->assertContains ("connect-src 'self' " , $ directives );
987+ $ this ->assertNotContains ('connect-src ' , $ directives );
889988 }
890989
891990 #[PreserveGlobalState(false )]
0 commit comments