Skip to content

Commit d218b25

Browse files
committed
Add basic support for csp nonce
1 parent 3b22865 commit d218b25

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

packages/superdoc/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta name="csp-nonce" content="testnonce123" />
7+
<meta
8+
http-equiv="Content-Security-Policy"
9+
content="
10+
style-src 'self' 'nonce-testnonce123';
11+
style-src-attr 'unsafe-inline';
12+
"
13+
/>
614
<title>SuperDoc</title>
715
</head>
816
<body>

packages/superdoc/src/core/SuperDoc.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ export class SuperDoc extends EventEmitter {
219219
// Initialize collaboration if configured
220220
await this.#initCollaboration(this.config.modules);
221221

222+
// Apply csp nonce if provided
223+
if (this.config.cspNonce) this.#patchNaiveUIStyles();
224+
222225
// this.#initTelemetry();
223226
this.#initVueApp();
224227
this.#initListeners();
@@ -259,6 +262,19 @@ export class SuperDoc extends EventEmitter {
259262
};
260263
}
261264

265+
#patchNaiveUIStyles() {
266+
const cspNonce = this.config.cspNonce;
267+
268+
const originalCreateElement = document.createElement
269+
document.createElement = function(tagName) {
270+
const element = originalCreateElement.call(this, tagName)
271+
if (tagName.toLowerCase() === 'style') {
272+
element.setAttribute('nonce', cspNonce)
273+
}
274+
return element
275+
}
276+
}
277+
262278
#initDocuments() {
263279
const doc = this.config.document;
264280
const hasDocumentConfig = !!doc && typeof doc === 'object' && Object.keys(this.config.document)?.length;

packages/superdoc/src/dev/components/SuperdocDev.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const init = async () => {
7777
// isNewFile: true,
7878
// },
7979
// ],
80+
cspNonce: 'testnonce123',
8081
modules: {
8182
comments: {
8283
// comments: sampleComments,

0 commit comments

Comments
 (0)