@@ -53,11 +53,16 @@ export default function (editor: Editor, text: string, link: string): PanelConf
5353 *
5454 * 同上,列表无法插入链接的原因,是因为在insertLink, 处理text时有问题。
5555 */
56+ const resultText = text . replace ( / < / g, '<' ) . replace ( / > / g, '>' ) // Link xss
5657
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
5860
5961 // 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
6166
6267 if ( isActive ( editor ) ) {
6368 // 选区处于链接中,则选中整个菜单,再执行 insertHTML
@@ -132,6 +137,9 @@ export default function (editor: Editor, text: string, link: string): PanelConf
132137 width : 300 ,
133138 height : 0 ,
134139
140+ // 拼接字符串的:xss 攻击:
141+ // 如值为:"><img src=1 onerror=alert(/xss/)>, 插入后:value=""><img src=1 onerror=alert(/xss/)>", 插入一个img元素
142+
135143 // panel 中可包含多个 tab
136144 tabs : [
137145 {
@@ -143,14 +151,12 @@ export default function (editor: Editor, text: string, link: string): PanelConf
143151 id="${ inputTextId } "
144152 type="text"
145153 class="block"
146- value="${ text } "
147154 placeholder="${ editor . i18next . t ( 'menus.panelMenus.link.链接文字' ) } "/>
148155 </td>
149156 <input
150157 id="${ inputLinkId } "
151158 type="text"
152159 class="block"
153- value="${ link } "
154160 placeholder="${ editor . i18next . t ( '如' ) } https://..."/>
155161 </td>
156162 <div class="w-e-button-container">
@@ -222,6 +228,7 @@ export default function (editor: Editor, text: string, link: string): PanelConf
222228 // 选区范围是a标签,直接替换href链接即可
223229 if ( $elem ?. nodeName === 'A' ) {
224230 $elem . setAttribute ( 'href' , link )
231+ $elem . innerText = text
225232
226233 return true
227234 }
@@ -232,8 +239,12 @@ export default function (editor: Editor, text: string, link: string): PanelConf
232239
233240 // 防止第一次设置就为特殊元素,这种情况应该为首次设置链接
234241 if ( nodeA ) {
242+ // 链接设置a
235243 nodeA . setAttribute ( 'href' , link )
236244
245+ // 文案还是要设置刚开始的元素内的文字,比如加粗的元素,不然会将加粗替代
246+ $elem . innerText = text
247+
237248 return true
238249 }
239250 }
@@ -261,6 +272,35 @@ export default function (editor: Editor, text: string, link: string): PanelConf
261272 ] ,
262273 } , // tab end
263274 ] , // 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+ } ,
264304 }
265305
266306 return conf
0 commit comments