Compare commits

...

2 Commits

Author SHA1 Message Date
github-actions[bot]
30eb822151 Version Packages 2021-11-11 10:11:42 +01:00
Thomas Allmer
15a82c0e4d fix: improve mdjs preview iframe security 2021-11-11 10:05:51 +01:00
7 changed files with 71 additions and 8 deletions

4
netlify.toml Normal file
View File

@@ -0,0 +1,4 @@
[[headers]]
for = "/*"
[headers.values]
Content-Security-Policy = "default-src 'self'; script-src 'self' www.googletagmanager.com 'sha256-W6Gq+BvrdAAMbF8E7WHA7UPQxuUOfJM8E9mpKD0oihA=' 'sha256-vFU+IJ5dUUukI5Varwy49dN2d89DmFj7UNewqQv88sw='; style-src 'self' 'unsafe-inline' fonts.googleapis.com; font-src 'self' data: fonts.gstatic.com;"

View File

@@ -1,5 +1,16 @@
# @rocket/cli # @rocket/cli
## 0.10.1
### Patch Changes
- 15a82c0: Enable including script files into the simulator via `<script src=".." mdjs-use>`
- 15a82c0: Allow only a limited set of characters for simulator includes `[a-zA-Z0-9\/\-_]`.
Notably, there is no:
- `:` to prevent `http://...` includes
- `.` so filenames as `this.is.my.js` are not supported. Also includes will be without file endings which will be added automatically
## 0.10.0 ## 0.10.0
### Minor Changes ### Minor Changes

View File

@@ -1,6 +1,6 @@
{ {
"name": "@rocket/cli", "name": "@rocket/cli",
"version": "0.10.0", "version": "0.10.1",
"publishConfig": { "publishConfig": {
"access": "public" "access": "public"
}, },

View File

@@ -14,19 +14,37 @@
<script type="module"> <script type="module">
import { render } from '@mdjs/mdjs-story'; import { render } from '@mdjs/mdjs-story';
function sanitize(input, type) {
return `${document.location.origin}/${input.match(/[a-zA-Z0-9\/\-_]*/)[0]}.${type}`;
}
async function onHashChange() { async function onHashChange() {
const urlParts = new URLSearchParams(document.location.hash.substr(1)); const urlParts = new URLSearchParams(document.location.hash.substr(1));
if (urlParts.get('stylesheets')) { if (urlParts.get('stylesheets')) {
for (const stylesheet of urlParts.getAll('stylesheets')) { for (const stylesheet of urlParts.getAll('stylesheets')) {
if (!document.querySelector(`link[rel="stylesheet"][href="${stylesheet}"]`)) { const safeStylesheetUrl = sanitize(stylesheet, 'css');
if (!document.querySelector(`link[rel="stylesheet"][href="${safeStylesheetUrl}"]`)) {
const link = document.createElement('link'); const link = document.createElement('link');
link.rel = 'stylesheet'; link.rel = 'stylesheet';
link.href = stylesheet; link.href = safeStylesheetUrl;
document.head.appendChild(link); document.head.appendChild(link);
} }
} }
} }
if (urlParts.get('moduleUrls')) {
for (const moduleUrl of urlParts.getAll('moduleUrls')) {
const safeModuleUrl = sanitize(moduleUrl, 'js');
if (!document.querySelector(`script[type=module][src="${safeModuleUrl}"]`)) {
const script = document.createElement('script');
script.type = 'module';
script.src = safeModuleUrl;
document.head.appendChild(script);
}
}
}
if (urlParts.get('theme')) { if (urlParts.get('theme')) {
document.documentElement.setAttribute('theme', urlParts.get('theme')); document.documentElement.setAttribute('theme', urlParts.get('theme'));
} }
@@ -46,7 +64,8 @@
document.documentElement.removeAttribute('edge-distance'); document.documentElement.removeAttribute('edge-distance');
} }
const mod = await import(urlParts.get('story-file')); const safeStoryUrl = sanitize(urlParts.get('story-file'), 'js');
const mod = await import(safeStoryUrl);
render(mod[urlParts.get('story-key')]({ shadowRoot: document }), document.body); render(mod[urlParts.get('story-key')]({ shadowRoot: document }), document.body);
} }

View File

@@ -1,5 +1,16 @@
# @mdjs/mdjs-preview # @mdjs/mdjs-preview
## 0.5.4
### Patch Changes
- 15a82c0: Enable including script files into the simulator via `<script src=".." mdjs-use>`
- 15a82c0: Allow only a limited set of characters for simulator includes `[a-zA-Z0-9\/\-_]`.
Notably, there is no:
- `:` to prevent `http://...` includes
- `.` so filenames as `this.is.my.js` are not supported. Also includes will be without file endings which will be added automatically
## 0.5.3 ## 0.5.3
### Patch Changes ### Patch Changes

View File

@@ -1,6 +1,6 @@
{ {
"name": "@mdjs/mdjs-preview", "name": "@mdjs/mdjs-preview",
"version": "0.5.3", "version": "0.5.4",
"publishConfig": { "publishConfig": {
"access": "public" "access": "public"
}, },

View File

@@ -10,6 +10,16 @@ import {
} from './mdjsViewerSharedStates.js'; } from './mdjsViewerSharedStates.js';
import { addResizeHandler } from './resizeHandler.js'; import { addResizeHandler } from './resizeHandler.js';
/**
* @param {string} input
* @param {'js'|'css'} type
* @returns {string}
*/
function sanitize(input, type) {
const url = new URL(input);
return url.pathname.slice(1, (type.length + 1) * -1);
}
/** /**
* @typedef {object} StoryOptions * @typedef {object} StoryOptions
* @property {HTMLElement | null} StoryOptions.shadowRoot * @property {HTMLElement | null} StoryOptions.shadowRoot
@@ -273,9 +283,8 @@ export class MdJsPreview extends ScopedElementsMixin(LitElement) {
if (!mdjsSetupScript) { if (!mdjsSetupScript) {
throw new Error('Could not find a <script type="module" src="..." mdjs-setup></script>'); throw new Error('Could not find a <script type="module" src="..." mdjs-setup></script>');
} }
const params = new URLSearchParams(); const params = new URLSearchParams();
params.set('story-file', mdjsSetupScript.src); params.set('story-file', sanitize(mdjsSetupScript.src, 'js'));
params.set('story-key', this.key); params.set('story-key', this.key);
params.set('theme', this.theme); params.set('theme', this.theme);
params.set('platform', this.platform); params.set('platform', this.platform);
@@ -287,7 +296,16 @@ export class MdJsPreview extends ScopedElementsMixin(LitElement) {
]); ]);
for (const link of links) { for (const link of links) {
if (link.href) { if (link.href) {
params.append('stylesheets', link.href); params.append('stylesheets', sanitize(link.href, 'css'));
}
}
const moduleUrls = /** @type {HTMLScriptElement[]} */ ([
...document.querySelectorAll('script[type=module][mdjs-use]'),
]);
for (const moduleUrl of moduleUrls) {
if (moduleUrl.src) {
params.append('moduleUrls', sanitize(moduleUrl.src, 'js'));
} }
} }