@@ -53,11 +53,16 @@ export default function (editor: Editor, text: string, link: string): PanelConf
53
53
*
54
54
* 同上,列表无法插入链接的原因,是因为在insertLink, 处理text时有问题。
55
55
*/
56
+ const resultText = text . replace ( / < / g, '<' ) . replace ( / > / g, '>' ) // Link xss
56
57
57
- const $elem : DomElement = $ ( `<a href="${ link } " target="_blank">${ text } </a>` )
58
+ const $elem : DomElement = $ ( `<a target="_blank">${ resultText } </a>` )
59
+ const linkDom = $elem . elems [ 0 ] as HTMLAnchorElement
58
60
59
61
// fix: 字符转义问题,https://xxx.org?bar=1¯o=2 => https://xxx.org?bar=1¯o=2
60
- $elem . elems [ 0 ] . innerText = text
62
+ linkDom . innerText = text
63
+
64
+ // 避免拼接字符串,带来的字符串嵌套问题:如: <a href=""><img src=1 xx />"> 造成xss攻击
65
+ linkDom . href = link
61
66
62
67
if ( isActive ( editor ) ) {
63
68
// 选区处于链接中,则选中整个菜单,再执行 insertHTML
@@ -132,6 +137,9 @@ export default function (editor: Editor, text: string, link: string): PanelConf
132
137
width : 300 ,
133
138
height : 0 ,
134
139
140
+ // 拼接字符串的:xss 攻击:
141
+ // 如值为:"><img src=1 onerror=alert(/xss/)>, 插入后:value=""><img src=1 onerror=alert(/xss/)>", 插入一个img元素
142
+
135
143
// panel 中可包含多个 tab
136
144
tabs : [
137
145
{
@@ -143,14 +151,12 @@ export default function (editor: Editor, text: string, link: string): PanelConf
143
151
id="${ inputTextId } "
144
152
type="text"
145
153
class="block"
146
- value="${ text } "
147
154
placeholder="${ editor . i18next . t ( 'menus.panelMenus.link.链接文字' ) } "/>
148
155
</td>
149
156
<input
150
157
id="${ inputLinkId } "
151
158
type="text"
152
159
class="block"
153
- value="${ link } "
154
160
placeholder="${ editor . i18next . t ( '如' ) } https://..."/>
155
161
</td>
156
162
<div class="w-e-button-container">
@@ -222,6 +228,7 @@ export default function (editor: Editor, text: string, link: string): PanelConf
222
228
// 选区范围是a标签,直接替换href链接即可
223
229
if ( $elem ?. nodeName === 'A' ) {
224
230
$elem . setAttribute ( 'href' , link )
231
+ $elem . innerText = text
225
232
226
233
return true
227
234
}
@@ -232,8 +239,12 @@ export default function (editor: Editor, text: string, link: string): PanelConf
232
239
233
240
// 防止第一次设置就为特殊元素,这种情况应该为首次设置链接
234
241
if ( nodeA ) {
242
+ // 链接设置a
235
243
nodeA . setAttribute ( 'href' , link )
236
244
245
+ // 文案还是要设置刚开始的元素内的文字,比如加粗的元素,不然会将加粗替代
246
+ $elem . innerText = text
247
+
237
248
return true
238
249
}
239
250
}
@@ -261,6 +272,35 @@ export default function (editor: Editor, text: string, link: string): PanelConf
261
272
] ,
262
273
} , // tab end
263
274
] , // tabs end
275
+ /**
276
+ * 设置input的值,分别为文案和链接地址设置值
277
+ *
278
+ * 利用dom 设置链接文案的值,防止回填拼接引号问题, 出现xss攻击
279
+ *
280
+ * @param $container 对应上面生成的dom容器
281
+ * @param type text | link
282
+ */
283
+ setLinkValue ( $container : DomElement , type : string ) {
284
+ let inputId = ''
285
+ let inputValue = ''
286
+ let inputDom
287
+
288
+ // 设置链接文案
289
+ if ( type === 'text' ) {
290
+ inputId = `#${ inputTextId } `
291
+ inputValue = text
292
+ }
293
+
294
+ // 这只链接地址
295
+ if ( type === 'link' ) {
296
+ inputId = `#${ inputLinkId } `
297
+ inputValue = link
298
+ }
299
+
300
+ inputDom = $container . find ( inputId ) . elems [ 0 ] as HTMLInputElement
301
+
302
+ inputDom . value = inputValue
303
+ } ,
264
304
}
265
305
266
306
return conf
0 commit comments