|
76 | 76 | });
|
77 | 77 | }
|
78 | 78 |
|
| 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 | + |
79 | 127 | // Disable WordPress text transformations (smart quotes, etc.) for posts.
|
80 | 128 | remove_filter( 'the_content', 'wptexturize' );
|
81 | 129 |
|
|
0 commit comments