Skip to content

Commit ab6506c

Browse files
committed
All: Allow unsafe-inline scripts on wp-admin
The WordPress admin uses various inline scripts. This hasn't affected us on doc sites so far, because we generally don't log into wp-admin there. This affects blog.jquerymobile.com and soon other blogs. Ref jquery/infrastructure-puppet#17
1 parent 8fa5c5e commit ab6506c

File tree

3 files changed

+53
-44
lines changed

3 files changed

+53
-44
lines changed

plugins/jquery-actions.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
// (such as references to theme assets and intra-site links).
99
// This does not influence 'home' and 'siteurl' options, and thus
1010
// does not affect <link rel=canonical> and sitemap output.
11-
if ( @$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' ) {
11+
$jq_proto = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '';
12+
if ( $jq_proto == 'https' ) {
1213
$_SERVER['HTTPS'] = '1';
13-
} elseif ( @$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'http' ) {
14+
} elseif ( $jq_proto == 'http' ) {
1415
$_SERVER['HTTPS'] = '0';
1516
}
17+
unset( $jq_proto );
18+
1619
add_filter( 'wp_headers', function ( $headers ) {
1720
if ( isset( $headers['Vary'] ) ) {
1821
$headers['Vary'] .= ',X-Forwarded-Proto';

plugins/jquery-filters.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,54 @@
7676
});
7777
}
7878

79+
/**
80+
* Content Security Policy
81+
* https://github.com/jquery/infrastructure-puppet/issues/54
82+
*/
83+
add_action( 'send_headers', function() {
84+
$nonce = bin2hex( random_bytes( 8 ) );
85+
$report_url = 'https://csp-report-api.openjs-foundation.workers.dev/';
86+
$policy = array(
87+
'default-src' => "'self'",
88+
'script-src' => "'self' 'nonce-$nonce' code.jquery.com",
89+
// The nonce is here so inline scripts can be used in the theme
90+
'style-src' => "'self' 'nonce-$nonce' code.jquery.com",
91+
// Allow style="" attributes in blog posts and markdown.
92+
'style-src-attr' => "'unsafe-inline'",
93+
// data: SVG images are used in typesense
94+
// Allow gravatars in wordpress admins
95+
'img-src' => "'self' data: secure.gravatar.com code.jquery.com",
96+
'connect-src' => "'self' typesense.jquery.com",
97+
// Allow data fonts for the wordpress admins
98+
'font-src' => "'self' data:",
99+
'object-src' => "'none'",
100+
'frame-ancestors' => "'none'",
101+
'base-uri' => "'self'",
102+
'block-all-mixed-content' => '',
103+
'report-to' => 'csp-endpoint',
104+
// Add report-uri for Firefox, which
105+
// does not yet support report-to
106+
'report-uri' => $report_url,
107+
);
108+
109+
$policy = apply_filters( 'jq_content_security_policy', $policy );
110+
111+
if ( is_admin() ) {
112+
// wp-admin (as used by blogs) requires inline scripts, inline styles,
113+
// and workers from blob: URLs
114+
$policy[ 'script-src' ] = "'self' 'unsafe-inline' blob: code.jquery.com";
115+
$policy[ 'style-src' ] = "'self' 'unsafe-inline' code.jquery.com";
116+
}
117+
118+
$policy_string = '';
119+
foreach ( $policy as $key => $value ) {
120+
$policy_string .= $key . ' ' . $value . '; ';
121+
}
122+
123+
header( 'Reporting-Endpoints: csp-endpoint="' . $report_url . '"' );
124+
header( 'Content-Security-Policy: ' . $policy_string );
125+
} );
126+
79127
// Disable WordPress text transformations (smart quotes, etc.) for posts.
80128
remove_filter( 'the_content', 'wptexturize' );
81129

themes/jquery/functions.php

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -252,45 +252,3 @@ function jq_image_posted_on() {
252252

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

0 commit comments

Comments
 (0)