cssinjs / cssinjs/jss

Code Execution Error after Applying Content Security Policy (CSP)

Open
#1,645 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.1k
Forks
386
PR merge metrics
No merged PRs in 30d

Description

This is my first submission.
Thank you in advance for your cooperation.
I am not good at English, Sorry if it is hard to read in my poor English.

I apologize for any confusion. It's appropriate to provide the details here since you were unsure whether to create an issue or start a discussion. If there is an issue, please let me know, and I'll assist you accordingly.

__Expected behavior:__
I expected the code to execute without errors after applying the nonce to the Style tags.

__Describe the bug:__
When setting the nonce and attempting to execute the code, I encountered the following errors:

※I am attaching a screenshot of the error being displayed. We would appreciate it if you could check it.

Error:
Refused to apply inline style because it violates the following Content Security Policy directive: "style-src-elem 'self' 'nonce-474cd72a-68a6-4c73-ab04-3be566102f8f'". Either the 'unsafe-inline' keyword, a hash ('sha256-AbpHGcgLb+kRsJGnwFEktk7uzpZOCcBY74+YBdrKVGs='), or a nonce ('nonce-...') is required to enable inline execution.

__Reproduction:__
We are in the process of applying the Content Security Policy (CSP).
CSP support: https://cssinjs.org/csp/?v=v10.8.2

To reproduce the issue, follow these steps:
1.set the following csp header
property="csp-nonce"
```

```
http-equiv="Content-Security-Policy"
```

```
2. Set the nonce for the Style tags.
3. Execute the code containing the following snippets:

Snippet 1:

```
jsxCopy code
function insertStyle(style, options) {
var insertionPoint = options.insertionPoint;
var nextNode = findPrevNode(options);

if (nextNode !== false && nextNode.parent) {
nextNode.parent.insertBefore(style, nextNode.node); // Error occurs here
return;
}

if (insertionPoint && typeof insertionPoint.nodeType === 'number') {
var insertionPointElement = insertionPoint;
var parentNode = insertionPointElement.parentNode;
if (parentNode) parentNode.insertBefore(style, insertionPointElement.nextSibling);
else process.env.NODE_ENV !== "production" ? warning(false, '[JSS] Insertion point is not in the DOM.') : void 0;
return;
}

getHead().appendChild(style); // Error occurs here
}

```

Snippet 2:

```
jsxCopy code
_proto.deploy = function deploy() {
var sheet = this.sheet;
if (!sheet) return;

if (sheet.options.link) {
this.insertRules(sheet.rules);
return;
}
this.element.textContent = "\n" + sheet.toString() + "\n"; // Error occurs here
}

```

__Versions (please complete the following information):__
- jss:10.9.0
- Browser [e.g. chrome, safari]:Microsoft edge、chrome
- OS [e.g. Windows, macOS]: Windows 10、Windows 11、macOS
- Material-ui : v4
- Server Side Rendering(SSR):not use

__Managing expectations:__

Is there a solution, and if so, can you please tell me how to deal with it?
If it seems difficult to deal with, can we enroll in commercial support and you will be able to help us?

![screenshot codesnipet2-error](https://github.com/cssinjs/jss/assets/75819114/60fb83ca-08f0-417d-814a-d52274697de3)
![screenshot codesnipet1-error](https://github.com/cssinjs/jss/assets/75819114/8cf91edb-8e01-49d2-9802-eb6fa1c9ff70)

Contributor guide

Open the contributing guide

Research direction

Reproduce the report using the supplied CSP meta tags and JSS 10.9.0, then trace the insertStyle function and _proto.deploy snippets where the issue reports failures. Compare behavior in Chrome or Edge with the nonce-bearing style tags; done means code executes without the reported CSP inline-style errors under the stated policy.

Written by the indexing model from the issue text.

Assessment

Tech stack
css, javascript
Domain
frontend, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.