Skip to content

Commit 333228f

Browse files
authored
All: add Content-Security-Policy-Report-Only header to all wordpress sites (#463)
Ref jquery/infrastructure-puppet#54 Ref jquery/infrastructure-puppet#57
1 parent 9960ace commit 333228f

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

themes/api.jquery.com/functions.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Allow inline scripts and styles in API demos
4+
add_filter( 'jq_content_security_policy', function ( $policy ) {
5+
$policy[ 'script-src' ] = "'self' 'unsafe-inline' code.jquery.com";
6+
$policy[ 'style-src' ] = "'self' 'unsafe-inline'";
7+
return $policy;
8+
} );

themes/api.jquerymobile.com/functions.php

+7
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,10 @@ function jq_mobile_api_version_current() {
2828
$thisVersion[ 1 ] :
2929
jq_mobile_api_version_latest();
3030
}
31+
32+
// Allow inline scripts and styles in API demos
33+
add_filter( 'jq_content_security_policy', function ( $policy ) {
34+
$policy[ 'script-src' ] = "'self' 'unsafe-inline' code.jquery.com";
35+
$policy[ 'style-src' ] = "'self' 'unsafe-inline'";
36+
return $policy;
37+
} );

themes/api.jqueryui.com/functions.php

+7
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ function jq_ui_api_version_current() {
2424
$thisVersion[ 1 ] :
2525
jq_ui_api_version_latest();
2626
}
27+
28+
// Allow inline scripts and styles in API demos
29+
add_filter( 'jq_content_security_policy', function ( $policy ) {
30+
$policy[ 'script-src' ] = "'self' 'unsafe-inline' code.jquery.com";
31+
$policy[ 'style-src' ] = "'self' 'unsafe-inline'";
32+
return $policy;
33+
} );

themes/jquery/functions.php

+45
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,48 @@ function jq_image_posted_on() {
251251

252252
return $classes;
253253
} );
254+
255+
/**
256+
* Content Security Policy
257+
*/
258+
function jq_content_security_policy() {
259+
if ( !JQUERY_STAGING ) {
260+
return;
261+
}
262+
$nonce = bin2hex( random_bytes( 8 ) );
263+
$report_url = 'https://csp-report-api.openjs-foundation.workers.dev/';
264+
$policy = array(
265+
'default-src' => "'self'",
266+
'script-src' => "'self' 'nonce-$nonce' code.jquery.com",
267+
// The nonce is here so inline scripts can be used in the theme
268+
'style-src' => "'self' 'nonce-$nonce'",
269+
// data: SVG images are used in typesense
270+
'img-src' => "'self' data:",
271+
'connect-src' => "'self' typesense.jquery.com",
272+
'font-src' => "'self'",
273+
'object-src' => "'none'",
274+
'media-src' => "'self'",
275+
'frame-src' => "'self'",
276+
'child-src' => "'self'",
277+
'form-action' => "'self'",
278+
'frame-ancestors' => "'none'",
279+
'base-uri' => "'self'",
280+
'block-all-mixed-content' => '',
281+
'report-to' => 'csp-endpoint',
282+
// Add report-uri for Firefox, which
283+
// does not yet support report-to
284+
'report-uri' => $report_url,
285+
);
286+
287+
$policy = apply_filters( 'jq_content_security_policy', $policy );
288+
289+
$policy_string = '';
290+
foreach ( $policy as $key => $value ) {
291+
$policy_string .= $key . ' ' . $value . '; ';
292+
}
293+
294+
header( 'Reporting-Endpoints: csp-endpoint="' . $report_url . '"' );
295+
header( 'Content-Security-Policy-Report-Only: ' . $policy_string );
296+
}
297+
298+
add_action( 'send_headers', 'jq_content_security_policy' );

themes/jquery/header.php

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66

77
<title><?php
8-
global $page, $paged;
98
wp_title( '|', true, 'right' );
109
bloginfo( 'name' );
1110
$site_description = get_bloginfo( 'description', 'display' );

0 commit comments

Comments
 (0)