Skip to content

Commit 2e17671

Browse files
author
patched.codes[bot]
committed
Patched html.js
1 parent 2441ace commit 2e17671

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

html.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22
import PropTypes from 'prop-types'
33
import React, { PureComponent } from 'react'
4-
import serialize from 'serialize-javascript'
4+
import DOMPurify from 'dompurify'
55

66
// @twreporter
77
import webfonts from '@twreporter/react-components/lib/text/utils/webfonts'
@@ -110,17 +110,41 @@ export default class Html extends PureComponent {
110110
{styleElement}
111111
</head>
112112
<body>
113-
<div id="root" dangerouslySetInnerHTML={{ __html: contentMarkup }} />
113+
<div id="root"
114+
dangerouslySetInnerHTML={{
115+
__html: DOMPurify.sanitize(contentMarkup, {
116+
FORBID_SCRIPTS: true, // Completely forbid scripts in content
117+
FORBID_EVAL: true,
118+
FORBID_TAGS: ['script', 'iframe', 'object', 'embed', 'form', 'input'],
119+
ADD_ATTR: ['target'],
120+
ALLOWED_TAGS: [
121+
// Content tags only - no script tags
122+
'div', 'p', 'span', 'a', 'img', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
123+
'ul', 'ol', 'li', 'br', 'strong', 'em', 'blockquote', 'figure', 'figcaption',
124+
'table', 'thead', 'tbody', 'tr', 'th', 'td'
125+
],
126+
ALLOWED_ATTR: [
127+
'href', 'src', 'alt', 'title', 'class', 'id', 'target'
128+
],
129+
CUSTOM_ELEMENT_HANDLING: {
130+
tagNameCheck: /^[a-zA-Z\-]+$/,
131+
attributeNameCheck: /^[a-zA-Z\-]+$/,
132+
allowCustomizedBuiltInElements: false
133+
},
134+
RETURN_DOM_FRAGMENT: false,
135+
RETURN_DOM: false
136+
})
137+
}}
138+
/>
114139
<script
115140
defer
116141
src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.zh-Hant-TW"
117142
/>
118143
<script
119-
dangerouslySetInnerHTML={{
120-
__html: `window.__REDUX_STATE__=${serialize(store.getState())};`,
121-
}}
122144
charSet="UTF-8"
123-
/>
145+
>
146+
{`window.__REDUX_STATE__ = ${JSON.stringify(store.getState())};`}
147+
</script>
124148
{_.map(scripts, (script, key) => (
125149
<script src={script} key={'scripts' + key} charSet="UTF-8" />
126150
))}

0 commit comments

Comments
 (0)