Skip to content

Commit 6ac1a5b

Browse files
Updated the CSP fix with improved comments
and better clarification.
1 parent ceab8a8 commit 6ac1a5b

File tree

5 files changed

+20
-52
lines changed

5 files changed

+20
-52
lines changed

src/client/app/emotionCache.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
//imports the createSche function from React's Emotion library
12
import createCache from '@emotion/cache';
23

4+
//creates the nonce for the script being run
5+
//he nonce works alongside the webpack_nonce and plotly_nonce to protect against unwated scripts
36
const nonce = (document.querySelector('script[nonce]') as HTMLScriptElement | null)?.nonce;
47

58
const emotionCache = createCache({

src/client/app/index.tsx

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* This Source Code Form is subject to the terms of the Mozilla Public
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
//these lines take the nonce, and create the webpack and plotly nonces from it
6+
//these are additional nonces that contribute to styling with webpacvk and plotly
47
const __webpack_nonce__ = (document.querySelector('script[nonce]') as HTMLScriptElement | null)?.nonce;
58
(window as any).__webpack_nonce__ = __webpack_nonce__;
69
(window as any).__plotly_nonce__ = __webpack_nonce__;
710

8-
console.log('Set __webpack_nonce__ to:', __webpack_nonce__);
9-
10-
console.log('Set nonces:', __webpack_nonce__);
1111
declare global {
1212
interface Window {
1313
__webpack_nonce__?: string;
@@ -21,7 +21,6 @@ document.head.appendChild = function (node: any) {
2121
if (
2222
node instanceof HTMLStyleElement
2323
) {
24-
console.log('Appending style, has nonce:', __webpack_nonce__);
2524
node.setAttribute('nonce',__webpack_nonce__|| '');
2625
}
2726

@@ -32,28 +31,7 @@ document.head.appendChild = function (node: any) {
3231
throw err;
3332
}
3433
};
35-
// document.head.appendChild = function (node: any) {
36-
// if (node instanceof HTMLStyleElement) {
37-
// const hasNonce = node.getAttribute('nonce');
38-
// console.log('Appending style, has nonce:', hasNonce);
39-
40-
// if (!hasNonce) {
41-
// const nonceToUse = window.__plotly_nonce__ || window.__webpack_nonce__ || '';
42-
// console.log('Setting nonce on style element:', nonceToUse);
43-
// node.setAttribute('nonce', nonceToUse);
44-
// }
45-
// }
46-
// try {
47-
// return originalAppendChild.call(this, node);
48-
// } catch (err) {
49-
// console.error('Failed to append style:', err, node);
50-
// throw err;
51-
// }
52-
// };
53-
console.log('Before import, __webpack_nonce__ =', __webpack_nonce__);
5434
import 'bootstrap/dist/css/bootstrap.css';
55-
console.log('BootstrapCSS Loaded');
56-
console.log('After import, __webpack_nonce__ =', __webpack_nonce__);
5735
import * as React from 'react';
5836
import { createRoot } from 'react-dom/client';
5937
import { Provider } from 'react-redux';

src/client/index.html

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,20 @@
2323

2424

2525
<!--
26-
The meta tag with "Content-Security-Policy" below is the Content Security Policy, by having default-src as self all CSP rules that are unspecified will only
27-
allow the OED site and resources to be used/displayed. Tags like img-src, media-src, and script-src are also set to self to ensure that only
28-
resources like images, audio/videos, and scripts like JavaScript and TypeScript can only be from OED and will block any types of injections.
29-
The tag font-src is the exception to this as OED also uses a font from a bootstrapcdn.com sub-domain and has this site listed next to 'self'.
30-
To test CSP rules change http-equiv=”Content-Security-Policy” to http-equiv=”Content-Security-Policy-Report-Only” this allows us to send reports of
31-
what would have been blocked without actually blocking it.
26+
The meta tag with "Content-Security-Policy" below is the Content Security Policy, by having default-src as self all CSP rules that are
27+
unspecified will only allow the OED site and resources to be used/displayed. Tags like img-src, media-src, and script-src are also set to self
28+
to ensure that only resources like images, audio/videos, and scripts like JavaScript and TypeScript can only be from OED and will block any
29+
types of injections. The tag font-src is the exception to this as OED also uses a font from a bootstrapcdn.com sub-domain and has this site
30+
listed next to 'self'. To test CSP rules change http-equiv=”Content-Security-Policy” to http-equiv=”Content-Security-Policy-Report-Only” this
31+
allows us to send reports of what would have been blocked without actually blocking it.
3232
33-
For sites using OED and are blocked by these CSP rules may add their site to the exception they may list their website link next to the tag that is blocking
34-
the user site. The site link must be added after 'self' but before the semi colon marking the end of that tag. The font-src tag is a great example on how to implement
35-
a site to the exception list. Another example for adding a site (https://newException.com) to a tag with multiple sites as an exceptions would be :
36-
img-src 'self' http://example.com https://site_example.net; becomes img-src 'self' http://example.com https://site_example.net https://newException.com;
37-
-->
38-
<!-- <meta http-equiv="Content-Security-Policy" content="
39-
default-src 'self';
40-
img-src 'self';
41-
font-src 'self' https://maxcdn.bootstrapcdn.com;
42-
media-src 'self';
43-
script-src 'self' 'nonce-__NONCE__';
44-
style-src 'self' 'nonce-__NONCE__'; "> -->
45-
46-
47-
<!-- <link rel="stylesheet" href="styles/card-page.css" nonce="__NONCE__">
48-
<link rel="stylesheet" href="styles/DateRangeCustom.css" nonce="__NONCE__">
49-
<link rel="stylesheet" href="styles/index.css" nonce="__NONCE__">
50-
<link rel="stylesheet" href="styles/modal.css" nonce="__NONCE__">
51-
<link rel="stylesheet" href="styles/react-select-css.css" nonce="__NONCE__">
52-
<link rel="stylesheet" href="styles/spinner.css" nonce="__NONCE__">
53-
<link rel="stylesheet" href="styles/tooltip.css" nonce="__NONCE__">
33+
For sites using OED and are blocked by these CSP rules may add their site to the exception they may list their website link next to the tag that
34+
is blocking the user site. The site link must be added after 'self' but before the semi colon marking the end of that tag. The font-src tag is a
35+
great example on how to implement a site to the exception list. Another example for adding a site (https://newException.com) to a tag with
36+
multiple sites as an exceptions would be : img-src 'self' http://example.com https://site_example.net; becomes img-src 'self'
37+
http://example.com https://site_example.net https://newException.com;
5438
-->
39+
5540
</head>
5641

5742
<body>

src/server/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ router.get('*', (req, res) => {
148148
const subdir = config.subdir || '/';
149149
let htmlPlusData = html.toString().replace('SUBDIR', subdir);
150150

151+
//assigns a value to the nonce in order to check for authenticity
151152
const nonce = crypto.randomBytes(16).toString('base64url')
152153
htmlPlusData = htmlPlusData.replace(/{{nonce}}/g, nonce)
153154

webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const config = {
4444
{loader: 'style-loader',
4545
options: {
4646
attributes: {
47+
//this line allows the webpack nonce to be applied to styles
4748
nonce: '__webpack_nonce__'
4849
}
4950
}

0 commit comments

Comments
 (0)