Skip to content

Commit c64723b

Browse files
committed
protect against XSS
1 parent 722fb00 commit c64723b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Doc/improve-page.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@ Improve a documentation page
1515
<script>
1616
document.addEventListener('DOMContentLoaded', () => {
1717
const params = new URLSearchParams(window.location.search);
18-
document.body.innerHTML = document.body.innerHTML
19-
.replace(/PAGETITLE/g, params.get('pagetitle'))
20-
.replace(/PAGEURL/g, params.get('pageurl'))
21-
.replace(/PAGESOURCE/g, params.get('pagesource'));
18+
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null);
19+
20+
// Replace .textContent to be safe. innerHTML will execute XSS code.
21+
while (walker.nextNode()) {
22+
const textNode = walker.currentNode;
23+
textNode.textContent = textNode.textContent
24+
.replace(/PAGETITLE/g, params.get('pagetitle'))
25+
.replace(/PAGEURL/g, params.get('pageurl'))
26+
.replace(/PAGESOURCE/g, params.get('pagesource'));
27+
}
2228
});
2329
</script>
2430

0 commit comments

Comments
 (0)