Skip to content

Commit 0734852

Browse files
committed
fix: prevent XSS
1 parent 6d5c5c6 commit 0734852

File tree

7 files changed

+22
-23
lines changed

7 files changed

+22
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mind-elixir",
3-
"version": "0.18.0",
3+
"version": "0.18.1",
44
"description": "Mind elixir is a free open source mind map core.",
55
"main": "dist/MindElixir.js",
66
"scripts": {

readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import MindElixir, { E } from 'mind-elixir'
5151
#### Script tag
5252

5353
```html
54-
<script src="https://cdn.jsdelivr.net/npm/regenerator-runtime"></script>
5554
<script src="https://cdn.jsdelivr.net/npm/mind-elixir/dist/mind-elixir.js"></script>
5655
```
5756

src/plugin/contextMenu.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import i18n from '../i18n'
2+
import { encodeHTML } from '../utils/index'
23

34
export default function(mind, option) {
45
const createTips = words => {
56
const div = document.createElement('div')
6-
div.innerHTML = words
7+
div.innerText = words
78
div.style.cssText = 'position:absolute;bottom:20px;left:50%;transform:translateX(-50%);'
89
return div
910
}
1011
const createLi = (id, name, keyname) => {
1112
const li = document.createElement('li')
1213
li.id = id
13-
li.innerHTML = `<span>${name}</span><span>${keyname}</span>`
14+
li.innerHTML = `<span>${encodeHTML(name)}</span><span>${encodeHTML(keyname)}</span>`
1415
return li
1516
}
1617
const locale = i18n[mind.locale] ? mind.locale : 'en'

src/plugin/nodeMenu.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import i18n from '../i18n'
22

3-
const createDiv = (id, name) => {
3+
const createDiv = (id) => {
44
const div = document.createElement('div')
55
div.id = id
6-
div.innerHTML = `<span>${name}</span>`
76
return div
87
}
98

@@ -31,9 +30,9 @@ const colorList = [
3130
export default function(mind) {
3231
const locale = i18n[mind.locale] ? mind.locale : 'en'
3332
let bgOrFont
34-
const styleDiv = createDiv('nm-style', 'style')
35-
const tagDiv = createDiv('nm-tag', 'tag')
36-
const iconDiv = createDiv('nm-icon', 'icon')
33+
const styleDiv = createDiv('nm-style')
34+
const tagDiv = createDiv('nm-tag')
35+
const iconDiv = createDiv('nm-icon')
3736

3837
styleDiv.innerHTML = `
3938
<div class="nm-fontsize-container">
@@ -60,12 +59,8 @@ export default function(mind) {
6059
<span class="background">${i18n[locale].background}</span>
6160
</div>
6261
`
63-
tagDiv.innerHTML = `
64-
${i18n[locale].tag}<input class="nm-tag" tabindex="-1" placeholder="${i18n[locale].tagsSeparate}" /><br>
65-
`
66-
iconDiv.innerHTML = `
67-
${i18n[locale].icon}<input class="nm-icon" tabindex="-1" placeholder="${i18n[locale].iconsSeparate}" /><br>
68-
`
62+
tagDiv.innerHTML = `${i18n[locale].tag}<input class="nm-tag" tabindex="-1" placeholder="${i18n[locale].tagsSeparate}" /><br>`
63+
iconDiv.innerHTML = `${i18n[locale].icon}<input class="nm-icon" tabindex="-1" placeholder="${i18n[locale].iconsSeparate}" /><br>`
6964

7065
const menuContainer = document.createElement('nmenu')
7166
menuContainer.innerHTML = `

src/plugin/toolBar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function createToolBarRBContainer(mind) {
1414
const zo = createButton('zoomout', 'move')
1515
const zi = createButton('zoomin', 'add')
1616
const percentage = document.createElement('span')
17-
percentage.innerHTML = '100%'
17+
percentage.innerText = '100%'
1818
toolBarRBContainer.appendChild(fc)
1919
toolBarRBContainer.appendChild(gc)
2020
toolBarRBContainer.appendChild(zo)

src/utils/dom.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LEFT, RIGHT, SIDE } from '../const'
22
import vari from '../var'
33
import { NodeObj } from '../index'
4-
4+
import { encodeHTML } from '../utils/index'
55
export type Top = HTMLElement
66

77
export type Group = HTMLElement
@@ -36,7 +36,7 @@ export const createGroup = function(nodeObj: NodeObj) {
3636
}
3737

3838
export const shapeTpc = function(tpc: Topic, nodeObj: NodeObj) {
39-
tpc.innerHTML = nodeObj.topic
39+
tpc.innerText = nodeObj.topic
4040

4141
if (nodeObj.style) {
4242
tpc.style.color = nodeObj.style.color || 'inherit'
@@ -56,23 +56,23 @@ export const shapeTpc = function(tpc: Topic, nodeObj: NodeObj) {
5656
const linkContainer = $d.createElement('a')
5757
linkContainer.className = 'hyper-link'
5858
linkContainer.target = '_blank'
59-
linkContainer.innerHTML = '🔗'
59+
linkContainer.innerText = '🔗'
6060
linkContainer.href = nodeObj.hyperLink
6161
tpc.appendChild(linkContainer)
6262
}
6363
if (nodeObj.icons) {
6464
const iconsContainer = $d.createElement('span')
6565
iconsContainer.className = 'icons'
6666
iconsContainer.innerHTML = nodeObj.icons
67-
.map(icon => `<span>${icon}</span>`)
67+
.map(icon => `<span>${encodeHTML(icon)}</span>`)
6868
.join('')
6969
tpc.appendChild(iconsContainer)
7070
}
7171
if (nodeObj.tags) {
7272
const tagsContainer = $d.createElement('div')
7373
tagsContainer.className = 'tags'
7474
tagsContainer.innerHTML = nodeObj.tags
75-
.map(tag => `<span>${tag}</span>`)
75+
.map(tag => `<span>${encodeHTML(tag)}</span>`)
7676
.join('')
7777
tpc.appendChild(tagsContainer)
7878
}
@@ -110,7 +110,7 @@ export function createInputDiv(tpc: Topic) {
110110
let div = $d.createElement('div')
111111
const origin = tpc.childNodes[0].textContent as string
112112
tpc.appendChild(div)
113-
div.innerHTML = origin
113+
div.innerText = origin
114114
div.contentEditable = 'true'
115115
div.spellcheck = false
116116
div.style.cssText = `min-width:${tpc.offsetWidth - 8}px;`
@@ -161,7 +161,7 @@ export function createInputDiv(tpc: Topic) {
161161
export const createExpander = function(expanded: boolean | undefined): Expander {
162162
const expander: Expander = $d.createElement('epd')
163163
// 包含未定义 expanded 的情况,未定义视为展开
164-
expander.innerHTML = expanded !== false ? '-' : '+'
164+
expander.innerText = expanded !== false ? '-' : '+'
165165
expander.expanded = expanded !== false
166166
expander.className = expanded !== false ? 'minus' : ''
167167
return expander

src/utils/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import vari from '../var'
22
import { NodeObj } from '../index'
33

4+
export function encodeHTML(s) {
5+
return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;')
6+
}
7+
48
export const isMobile = (): boolean =>
59
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
610
navigator.userAgent

0 commit comments

Comments
 (0)