Skip to content

Commit 4ce4448

Browse files
committed
All: add Content-Security-Policy-Report-Only header to all wordpress sites
Ref jquery/infrastructure-puppet#54 Ref jquery/infrastructure-puppet#57
1 parent 799f789 commit 4ce4448

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-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

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

252252
return $classes;
253253
} );
254+
255+
/**
256+
* Content Security Policy
257+
*/
258+
function jq_content_security_policy() {
259+
$nonce = wp_create_nonce( JQUERY_LIVE_SITE );
260+
$policy = array(
261+
'default-src' => "'self'",
262+
'script-src' => "'self' 'nonce-$nonce' code.jquery.com",
263+
// The SHA is for the inline style from typesense
264+
// 'unsafe-hashes' is required in order to use hashes in style-src
265+
'style-src' => "'self' 'nonce-$nonce' 'sha256-biLFinpqYMtWHmXfkA1BPeCY0/fNt46SAZ+BBk5YUog=' 'unsafe-hashes'",
266+
// data: SVG images are used in typesense
267+
'img-src' => "'self' data:",
268+
'connect-src' => "'self' typesense.jquery.com",
269+
'font-src' => "'self'",
270+
'object-src' => "'none'",
271+
'media-src' => "'self'",
272+
'frame-src' => "'self'",
273+
'child-src' => "'self'",
274+
'form-action' => "'self'",
275+
'frame-ancestors' => "'none'",
276+
'base-uri' => "'self'",
277+
'block-all-mixed-content' => '',
278+
'report-uri' => 'https://csp-report-api.openjs-foundation.workers.dev/',
279+
);
280+
281+
$policy = apply_filters( 'jq_content_security_policy', $policy );
282+
283+
$policy_string = '';
284+
foreach ( $policy as $key => $value ) {
285+
$policy_string .= $key . ' ' . $value . '; ';
286+
}
287+
288+
header( 'Content-Security-Policy-Report-Only: ' . $policy_string );
289+
}

themes/jquery/header.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
<?php jq_content_security_policy() ?>
12
<!doctype html>
23
<html class="no-js" <?php language_attributes(); ?>>
34
<head>
45
<meta charset="utf-8">
56
<meta http-equiv="X-UA-Compatible" content="IE=edge">
67

78
<title><?php
8-
global $page, $paged;
99
wp_title( '|', true, 'right' );
1010
bloginfo( 'name' );
1111
$site_description = get_bloginfo( 'description', 'display' );

0 commit comments

Comments
 (0)