@@ -64,13 +64,13 @@ function wp_headers( array $headers ): array {
6464 * @see https://docs.sentry.io/platforms/go/security-policy-reporting/#content-security-policy Sentry documentation on setting the CSP reporting.
6565 */
6666 // CSP directives use for reporting CSP violations.
67- $ csp ['report-to ' ] = 'csp-endpoint ' ;
67+ $ csp ['report-to ' ] = [ 'csp-endpoint ' ] ;
6868
6969 // Deprecated CSP directive for reporting, kept for compatibility with old browsers.
70- $ csp ['report-uri ' ] = $ report_url ;
70+ $ csp ['report-uri ' ] = [ $ report_url ] ;
7171
7272 // Include reporting endpoint domain to the list of allowed host
73- $ csp ['connect-src ' ] = ( $ csp [ ' connect-src ' ] ?? '' ) . ' ' . wp_parse_url ( $ report_url , PHP_URL_HOST );
73+ $ csp ['connect-src ' ][] = wp_parse_url ( $ report_url , PHP_URL_HOST );
7474
7575 // Additional headers required by the "report-to" CSP directive.
7676 $ headers ['Report-To ' ] = wp_json_encode (
@@ -110,7 +110,6 @@ function wp_headers( array $headers ): array {
110110add_filter ( 'wp_headers ' , __NAMESPACE__ . '\\wp_headers ' , 99 );
111111
112112/**
113- *
114113 * Prepare CSP attribute values
115114 *
116115 * @param array $csp
@@ -129,37 +128,59 @@ function get_prepare_csp( array $csp ): string {
129128 if ( empty ( $ value ) ) {
130129 continue ;
131130 }
132- $ csp_values .= $ key . ' ' . $ value . '; ' ;
131+
132+ // add space between values.
133+ if ( ! empty ( $ csp_values ) ) {
134+ $ csp_values .= ' ' ;
135+ }
136+
137+ $ csp_values .= sprintf (
138+ '%s %s; ' ,
139+ $ key ,
140+ implode ( ' ' , $ value )
141+ );
133142 }
134143
135- // Remove last space
136- return trim ( $ csp_values );
144+ return $ csp_values ;
137145}
138146
139147/**
140- * Generate CSP headers array
148+ * Get CSP headers directives.
141149 *
142- * @return array
150+ * Add specific values for each directive in the corresponding array.
151+ *
152+ * Some values MUST be wrap with single quote : `'self'`, `'unsafe-inline'` and `'unsafe-eval'`. Use double quotes
153+ * when you want to add them in the array (e.g. `"'self'"`).
154+ *
155+ * @return array<string, array>
143156 * @author Alexandre Sadowski
144157 */
145158function get_csp_headers (): array {
159+
146160 $ csp = [
147- 'default-src ' => "'self' " ,
148- 'script-src ' => "'self' " ,
149- 'style-src ' => "'self' " ,
150- 'img-src ' => "'self' " ,
151- 'font-src ' => "'self' " ,
152- 'connect-src ' => "'self' " ,
153- 'frame-src ' => "'self' " ,
154- 'manifest-src ' => "'self' " ,
155- 'worker-src ' => "'self' " ,
156- 'object-src ' => "'self' " ,
157- 'base-uri ' => "'self' " ,
161+ 'default-src ' => [ "'self' " ],
162+ 'script-src ' => [ "'self' " ],
163+ 'style-src ' => [ "'self' " ],
164+ 'img-src ' => [ "'self' " ],
165+ 'font-src ' => [ "'self' " ],
166+ 'connect-src ' => [ "'self' " ],
167+ 'media-src ' => [ "'self' " ],
168+ 'frame-src ' => [ "'self' " ],
169+ 'manifest-src ' => [ "'self' " ],
170+ 'worker-src ' => [ "'self' " ],
171+ 'object-src ' => [ "'self' " ],
172+ 'base-uri ' => [ "'self' " ],
173+ 'frame-ancestors ' => [ "'self' " ],
158174 ];
159175
160176 //if ( 'production' === WP_ENV ) {
161177 // $csp = [];
162178 //}
163179
180+ /**
181+ * Filter CSP values.
182+ *
183+ * @param array $csp The array of CSP values keyed by their directives.
184+ */
164185 return apply_filters ( 'csp_headers ' , $ csp );
165186}
0 commit comments