-
Notifications
You must be signed in to change notification settings - Fork 14
Lock down CSP in “.html” files to prove we can. #289
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="content-security-policy" content="default-src 'self'; script-src 'self' 'sha256-vpd4e6Q2EYlXOAcBOCPYNRqOSK1caV1iPamby7fBE30=' https://esm.sh/[email protected]/;"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because an import map is necessarily an inlined script (at least for now), we need to explicitly allow it. The browser actually makes this super easy since the CSP error will log out the hash in the console and you can just copy-paste it here. I was worried that this might be a maintenance burden, but it’s actually not bad at all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's actually horribleawesome There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ha. I agree. I had the same emotional journey. |
||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta charset="utf-8"> | ||
<!-- We use “unsafe-eval” below because we need to trick the browser into | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where we loosen our policy — I added explanatory commentary. |
||
not caching tagged template string objects to test interpretation. --> | ||
<meta http-equiv="content-security-policy" content="default-src 'self'; script-src 'self' 'unsafe-eval';"> | ||
<link rel="stylesheet" href="./index.css"> | ||
</head> | ||
<body> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta charset="utf-8"> | ||
<!-- We use “unsafe-eval” below because we need to trick the browser into | ||
not caching tagged template string objects to test interpretation. --> | ||
<meta http-equiv="content-security-policy" content="default-src 'self'; script-src 'self' 'unsafe-eval' 'sha256-2gPcHEAV/bRWIxMnz3UA5z6w2m9RdFis8ZAa8Y/EZVg=' https://esm.sh/[email protected] https://esm.sh/[email protected]/;"> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta charset="utf-8"> | ||
<!-- TODO: The react library has a bunch of dependencies which makes the CSP | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where we were forces to loosen our policy more than we’d like… I added a By contrast, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. working as intended! |
||
unwieldy. Rather than enumerate them all, a blanket rule is encoded | ||
below to allow any access to https://esm.sh/. You wouldn’t want to do | ||
that on a production site, this is just for the demo. --> | ||
<meta http-equiv="content-security-policy" content="default-src 'self'; script-src 'self' 'sha256-HVRkfAW4ErWhOQ71jSlHDwiHtimNBYSzVw0wcuf6XwA=' https://esm.sh/;"> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
import BaseElement from './base-element.js'; | ||
|
||
export default class DemoUhtml extends BaseElement { | ||
static get styles() { | ||
const styleSheet = new CSSStyleSheet(); | ||
styleSheet.replaceSync(` | ||
#container[emoji]::before { | ||
content: " " attr(emoji); | ||
font-size: 2rem; | ||
} | ||
`); | ||
return [styleSheet]; | ||
} | ||
|
||
static get properties() { | ||
return { | ||
emoji: { | ||
|
@@ -16,12 +27,6 @@ export default class DemoUhtml extends BaseElement { | |
static template(html) { | ||
return ({ emoji, message }) => { | ||
return html` | ||
<style> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simply changing this inline |
||
#container[emoji]::before { | ||
content: " " attr(emoji); | ||
font-size: 2rem; | ||
} | ||
</style> | ||
<div id="container" emoji="${emoji}">Rendered “${message}” using <code>µhtml</code>.</div> | ||
`; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simply changing this inline
<style>
tag usage so we can have a stricter content security policy on this particular demo. To be clear, this is not production code.