Skip to content

Commit ee188bf

Browse files
authored
feat(config): new style.nonce option for CSP (codex-team#2519)
1 parent 3533774 commit ee188bf

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### 2.29.0
44

5+
- `New` — Editor Config now has the `style.nonce` attribute that could be used to allowlist editor style tag for Content Security Policy "style-src"
56
- `Fix` — Passing an empty array via initial data or `blocks.render()` won't break the editor
67
- `Fix` — Layout did not shrink when a large document cleared in Chrome
78
- `Fix` — Multiple Tooltip elements creation fixed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@editorjs/editorjs",
3-
"version": "2.29.0-rc.4",
3+
"version": "2.29.0-rc.5",
44
"description": "Editor.js — Native JS, based on API and Open Source",
55
"main": "dist/editorjs.umd.js",
66
"module": "dist/editorjs.mjs",

src/components/dom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default class Dom {
5252
* @param {object} [attributes] - any attributes
5353
* @returns {HTMLElement}
5454
*/
55-
public static make(tagName: string, classNames: string | string[] = null, attributes: object = {}): HTMLElement {
55+
public static make(tagName: string, classNames: string | string[] | null = null, attributes: object = {}): HTMLElement {
5656
const el = document.createElement(tagName);
5757

5858
if (Array.isArray(classNames)) {

src/components/modules/ui.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,15 @@ export default class UI extends Module<UINodes> {
294294
textContent: styles.toString(),
295295
});
296296

297+
/**
298+
* If user enabled Content Security Policy, he can pass nonce through the config
299+
*
300+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce
301+
*/
302+
if (this.config.style && !_.isEmpty(this.config.style) && this.config.style.nonce) {
303+
tag.setAttribute('nonce', this.config.style.nonce);
304+
}
305+
297306
/**
298307
* Append styles at the top of HEAD tag
299308
*/

test/cypress/tests/initialization.cy.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,21 @@ describe('Editor basic initialization', () => {
4848
.should('eq', 'false');
4949
});
5050
});
51+
52+
describe('style', () => {
53+
describe('nonce', () => {
54+
it('should add passed nonce as attribute to editor style tag', () => {
55+
cy.createEditor({
56+
style: {
57+
nonce: 'test-nonce',
58+
},
59+
}).as('editorInstance');
60+
61+
cy.get('[data-cy=editorjs]')
62+
.get('#editor-js-styles')
63+
.should('have.attr', 'nonce', 'test-nonce');
64+
});
65+
});
66+
});
5167
});
5268
});

types/configs/editor-config.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,15 @@ export interface EditorConfig {
104104
* Common Block Tunes list. Will be added to all the blocks which do not specify their own 'tunes' set
105105
*/
106106
tunes?: string[];
107+
108+
/**
109+
* Section for style-related settings
110+
*/
111+
style?: {
112+
/**
113+
* A random value to handle Content Security Policy "style-src" policy
114+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce
115+
*/
116+
nonce?: string;
117+
}
107118
}

0 commit comments

Comments
 (0)