Skip to content

Commit 9b608af

Browse files
committed
🐛 fix < > are converted to &lt &gt
* linkify-string automatically escapes HTML input. So < is converted to &lt * replace linkify-string with linkify-html * and use js xss. this makes safe xss attack https://linkify.js.org/docs/linkify-string.html#usage-with-html https://github.com/leizongmin/js-xss
1 parent 286364b commit 9b608af

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"vue-tsc": "^0.32.0"
4545
},
4646
"dependencies": {
47-
"linkify-string": "^3.0.4",
48-
"linkifyjs": "^3.0.5"
47+
"linkify-html": "^3.0.5",
48+
"linkifyjs": "^3.0.5",
49+
"xss": "^1.0.11"
4950
}
5051
}

src/main.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { App, Directive } from "@vue/runtime-core";
2-
import linkifyStr from "linkify-string";
2+
import linkifyHtml from "linkify-html";
33
import { Options } from "linkifyjs";
4+
import xss from "xss";
5+
6+
const linkify = (rawHtml: string, options: Options): string => {
7+
const sanitized = xss(rawHtml);
8+
return linkifyHtml(sanitized, options);
9+
};
410

511
const directive: Directive = {
612
beforeMount(element: HTMLElement, binding) {
7-
const options = binding.value as Options;
8-
element.innerHTML = linkifyStr(element.innerHTML, options);
13+
element.innerHTML = linkify(element.innerHTML, binding.value);
914
},
1015
updated(element: HTMLElement, binding) {
11-
const options = binding.value as Options;
12-
element.innerHTML = linkifyStr(element.innerHTML, options);
16+
element.innerHTML = linkify(element.innerHTML, binding.value);
1317
},
1418
};
1519

yarn.lock

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ color-name@~1.1.4:
405405
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
406406
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==
407407

408+
commander@^2.20.3:
409+
version "2.20.3"
410+
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
411+
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
412+
408413
concat-map@0.0.1:
409414
version "0.0.1"
410415
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
@@ -427,6 +432,11 @@ cross-spawn@^7.0.2:
427432
shebang-command "^2.0.0"
428433
which "^2.0.1"
429434

435+
cssfilter@0.0.10:
436+
version "0.0.10"
437+
resolved "https://registry.yarnpkg.com/cssfilter/-/cssfilter-0.0.10.tgz#c6d2672632a2e5c83e013e6864a42ce8defd20ae"
438+
integrity sha1-xtJnJjKi5cg+AT5oZKQs6N79IK4=
439+
430440
debug@^4.1.1, debug@^4.3.2:
431441
version "4.3.3"
432442
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.3.tgz#04266e0b70a98d4462e6e288e38259213332b664"
@@ -947,10 +957,10 @@ levn@^0.4.1:
947957
prelude-ls "^1.2.1"
948958
type-check "~0.4.0"
949959

950-
linkify-string@^3.0.4:
951-
version "3.0.4"
952-
resolved "https://registry.yarnpkg.com/linkify-string/-/linkify-string-3.0.4.tgz#6abf1a5e436e800c729274ae08f5703484647f84"
953-
integrity sha512-OnNqqRjlYXaXipIAbBC8sDXsSumI1ftatzFg141Pw9HEXWjTVLFcMZoKbFupshqWRavtNJ6QHLa+u6AlxxgeRw==
960+
linkify-html@^3.0.5:
961+
version "3.0.5"
962+
resolved "https://registry.yarnpkg.com/linkify-html/-/linkify-html-3.0.5.tgz#317181f7603e17b7d38492b0f6fdf9cce14f1e6b"
963+
integrity sha512-3O7HEYjkugX+C/G2C2wyBmIt8Mt0pmeaHNIxRHodCFeQQeSxSoZHR+5hC1pi0WrmoEvfnSemyZyYTM8w3lo9cA==
954964

955965
linkifyjs@^3.0.5:
956966
version "3.0.5"
@@ -1514,6 +1524,14 @@ wrappy@1:
15141524
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
15151525
integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=
15161526

1527+
xss@^1.0.11:
1528+
version "1.0.11"
1529+
resolved "https://registry.yarnpkg.com/xss/-/xss-1.0.11.tgz#211cb82e95b5071d4c75d597283c021157ebe46a"
1530+
integrity sha512-EimjrjThZeK2MO7WKR9mN5ZC1CSqivSl55wvUK5EtU6acf0rzEE1pN+9ZDrFXJ82BRp3JL38pPE6S4o/rpp1zQ==
1531+
dependencies:
1532+
commander "^2.20.3"
1533+
cssfilter "0.0.10"
1534+
15171535
yallist@^4.0.0:
15181536
version "4.0.0"
15191537
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"

0 commit comments

Comments
 (0)