Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2423/#2536: CSP allow setting inline nonce with PrimeReact.inlineCssNonce #2525

Merged
merged 1 commit into from Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions components/lib/api/Api.d.ts
Expand Up @@ -20,6 +20,7 @@ interface FilterMatchModeOptions {
interface APIOptions {
ripple?: boolean;
inputStyle?: InputStyleType;
inlineCssNonce?: string;
locale?: string;
appendTo?: AppendToType;
cssTransition?: boolean;
Expand Down
2 changes: 2 additions & 0 deletions components/lib/api/PrimeReact.js
Expand Up @@ -14,6 +14,8 @@ export default class PrimeReact {

static autoZIndex = true;

static inlineCssNonce = null;

static zIndex = {
modal: 1100,
overlay: 1000,
Expand Down
22 changes: 19 additions & 3 deletions components/lib/utils/DomHandler.js
@@ -1,3 +1,5 @@
import PrimeReact from '../api/Api';

export default class DomHandler {

static innerWidth(el) {
Expand Down Expand Up @@ -870,15 +872,29 @@ export default class DomHandler {
}

/**
* Anytime an inline style is created check environment variable 'process.env.REACT_APP_CSS_NONCE'
* to set a CSP NONCE.
* Anytime an inline style is created check for CSP Nonce.
* Create React App/Next look for environment variable 'process.env.REACT_APP_CSS_NONCE'.
* Vite look for environment variable 'import.meta.env.VITE_CSS_NONCE'
* Finally look for global variable PrimeReact.inlineCssNonce to set a CSP NONCE.
*
* @see https://github.com/primefaces/primereact/issues/2423
* @return HtmlStyleElement
*/
static createInlineStyle() {
let styleElement = document.createElement('style');
let nonce = process.env.REACT_APP_CSS_NONCE;
let nonce = '';
// CRA and Next
if (process) {
nonce = process.env.REACT_APP_CSS_NONCE;
}
// Vite
if (!nonce && import.meta.env) {
nonce = import.meta.env.VITE_CSS_NONCE;
}
// global variable
if (!nonce) {
nonce = PrimeReact.inlineCssNonce;
}
if (nonce) {
styleElement.setAttribute('nonce', nonce);
}
Expand Down