Skip to content

Commit 9386fa9

Browse files
authored
Merge pull request #1295 from UziTech/demo-xss
use iframe to sandbox generated html
2 parents 1efe36b + d42f086 commit 9386fa9

File tree

6 files changed

+38
-5
lines changed

6 files changed

+38
-5
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ Our [documentation pages](https://marked.js.org) are also rendered using marked
2929

3030
**In-browser:** `npm install marked --save`
3131

32-
## Usage
32+
## Usage
33+
34+
### Warning: 🚨 Marked does not [sanitize](https://marked.js.org/#/USING_ADVANCED.md#options) the output HTML by default 🚨
3335

3436
**CLI**
3537

@@ -64,4 +66,3 @@ $ cat hello.html
6466
## License
6567

6668
Copyright (c) 2011-2018, Christopher Jeffrey. (MIT License)
67-

docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ These documentation pages are also rendered using marked 💯
3939

4040
<h2 id="usage">Usage</h2>
4141

42+
### Warning: 🚨 Marked does not [sanitize](https://marked.js.org/#/USING_ADVANCED.md#options) the output HTML by default 🚨
43+
4244
**CLI**
4345

4446
``` bash

docs/demo/demo.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@ header h1 {
5353
flex-grow: 1;
5454
flex-shrink: 1;
5555
}
56+
57+
#preview {
58+
display: flex;
59+
}
60+
61+
#preview iframe {
62+
flex-grow: 1;
63+
}

docs/demo/demo.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if (!window.fetch) {
99

1010
var $inputElem = document.querySelector('#input');
1111
var $outputTypeElem = document.querySelector('#outputType');
12-
var $previewElem = document.querySelector('#preview');
12+
var $previewIframe = document.querySelector('#preview iframe');
1313
var $permalinkElem = document.querySelector('#permalink');
1414
var $clearElem = document.querySelector('#clear');
1515
var $htmlElem = document.querySelector('#html');
@@ -20,6 +20,13 @@ var $activeElem = null;
2020
var changeTimeout = null;
2121
var search = searchToObject();
2222

23+
var iframeLoaded = false;
24+
$previewIframe.addEventListener('load', function () {
25+
iframeLoaded = true;
26+
inputDirty = true;
27+
checkForChanges();
28+
})
29+
2330
if ('text' in search) {
2431
$inputElem.value = search.text;
2532
} else {
@@ -51,7 +58,7 @@ function handleChange() {
5158
$panes[i].style.display = 'none';
5259
}
5360
$activeElem = document.querySelector('#' + $outputTypeElem.value);
54-
$activeElem.style.display = 'block';
61+
$activeElem.style.display = '';
5562

5663
updateLink();
5764
};
@@ -155,7 +162,9 @@ function checkForChanges() {
155162

156163
var parsed = marked.parser(lexed);
157164

158-
$previewElem.innerHTML = (parsed);
165+
if (iframeLoaded) {
166+
$previewIframe.contentDocument.body.innerHTML = (parsed);
167+
}
159168
$htmlElem.value = (parsed);
160169
$lexerElem.value = (lexedList.join('\n'));
161170

docs/demo/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ <h1>Marked Demo</h1>
4242
<noscript>
4343
<h2>You'll need to enable Javascript to use this tool.</h2>
4444
</noscript>
45+
<iframe src="./preview.html" frameborder="0" sandbox="allow-same-origin"></iframe>
4546
</div>
4647

4748
<textarea id="html" class="pane" readonly="readonly"></textarea>

docs/demo/preview.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
<!doctype html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="utf-8">
6+
<title>marked.js preview</title>
7+
<link rel="stylesheet" href="./demo.css" />
8+
<base target="_parent">
9+
</head>
10+
<body>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)