Skip to content

Commit

Permalink
feat(config): new style.nonce option for CSP (#2519)
Browse files Browse the repository at this point in the history
  • Loading branch information
neSpecc authored Oct 22, 2023
1 parent 3533774 commit ee188bf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 2.29.0

- `New` — Editor Config now has the `style.nonce` attribute that could be used to allowlist editor style tag for Content Security Policy "style-src"
- `Fix` — Passing an empty array via initial data or `blocks.render()` won't break the editor
- `Fix` — Layout did not shrink when a large document cleared in Chrome
- `Fix` — Multiple Tooltip elements creation fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@editorjs/editorjs",
"version": "2.29.0-rc.4",
"version": "2.29.0-rc.5",
"description": "Editor.js — Native JS, based on API and Open Source",
"main": "dist/editorjs.umd.js",
"module": "dist/editorjs.mjs",
Expand Down
2 changes: 1 addition & 1 deletion src/components/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class Dom {
* @param {object} [attributes] - any attributes
* @returns {HTMLElement}
*/
public static make(tagName: string, classNames: string | string[] = null, attributes: object = {}): HTMLElement {
public static make(tagName: string, classNames: string | string[] | null = null, attributes: object = {}): HTMLElement {
const el = document.createElement(tagName);

if (Array.isArray(classNames)) {
Expand Down
9 changes: 9 additions & 0 deletions src/components/modules/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,15 @@ export default class UI extends Module<UINodes> {
textContent: styles.toString(),
});

/**
* If user enabled Content Security Policy, he can pass nonce through the config
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce
*/
if (this.config.style && !_.isEmpty(this.config.style) && this.config.style.nonce) {
tag.setAttribute('nonce', this.config.style.nonce);
}

/**
* Append styles at the top of HEAD tag
*/
Expand Down
16 changes: 16 additions & 0 deletions test/cypress/tests/initialization.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,21 @@ describe('Editor basic initialization', () => {
.should('eq', 'false');
});
});

describe('style', () => {
describe('nonce', () => {
it('should add passed nonce as attribute to editor style tag', () => {
cy.createEditor({
style: {
nonce: 'test-nonce',
},
}).as('editorInstance');

cy.get('[data-cy=editorjs]')
.get('#editor-js-styles')
.should('have.attr', 'nonce', 'test-nonce');
});
});
});
});
});
11 changes: 11 additions & 0 deletions types/configs/editor-config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,15 @@ export interface EditorConfig {
* Common Block Tunes list. Will be added to all the blocks which do not specify their own 'tunes' set
*/
tunes?: string[];

/**
* Section for style-related settings
*/
style?: {
/**
* A random value to handle Content Security Policy "style-src" policy
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce
*/
nonce?: string;
}
}

0 comments on commit ee188bf

Please sign in to comment.