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

Add style-nonce attribute to import-map-overrides-full element to support use under a Content Security Policy(CSP) #126

Merged
merged 3 commits into from
Jan 18, 2025

Conversation

agevry
Copy link
Contributor

@agevry agevry commented Jan 14, 2025

Enables use of the import-map-overrides UI on a site that uses a strict CSP by providing a style-nonce attribute that can be set with the nonce required by the CSP for style tags.

…port use under a Content Security Policy(CSP)
Copy link

changeset-bot bot commented Jan 14, 2025

🦋 Changeset detected

Latest commit: 43db437

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
import-map-overrides Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -22,7 +22,9 @@ export default [
babelHelpers: "bundled",
}),
nodeResolve(),
postcss(),
postcss({
inject: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically a breaking change because user code could be accidentally or intentionally relying on the injected import-map-overrides.css in the global page, so I added a new changeset for this. Injecting the css into the main document isn't needed, since it's only used within shadow dom

Comment on lines 40 to 45
if (name === "style-nonce") {
this.styleNonce = newValue;
if (this.shadow) {
this.shadow.querySelector("style").nonce = this.styleNonce;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be handled within renderWithPreact, like the other attribute changes are handled. I removed it in the commits I pushed

<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta
http-equiv="Content-Security-Policy"
content="style-src 'nonce-adsffdsa78dsaf87a9'"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed that with this CSP the browser complains without the style-nonce attribute

@@ -43,6 +44,9 @@ function preactCustomElement(Comp, observedAttributes = []) {
this.shadow = this.attachShadow({ mode: "open" });
const style = document.createElement("style");
style.textContent = styles.toString();
if (this.getAttribute("style-nonce")) {
style.setAttribute("nonce", this.getAttribute("style-nonce"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched from style.nonce = to style.setAttribute('nonce', ..) even though both approaches function, since the javascript property version is not visible in the browser devtools and makes it more difficult to know that there is a nonce on the style element

@joeldenning joeldenning merged commit 2241fed into single-spa:main Jan 18, 2025
2 checks passed
@github-actions github-actions bot mentioned this pull request Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants