Skip to content

Commit eb27db8

Browse files
committed
fix: use validator.isURL to avoid regex catastrophic backtracking
Signed-off-by: Max Wu <jackymaxj@gmail.com>
1 parent 85e5d0a commit eb27db8

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

public/js/extra.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { saveAs } from 'file-saver'
99
import escapeHTML from 'lodash/escape'
1010
import unescapeHTML from 'lodash/unescape'
1111

12+
import isURL from 'validator/lib/isURL'
13+
1214
import { stripTags } from '../../utils/string'
1315

1416
import getUIElements from './lib/editor/ui-elements'
@@ -178,20 +180,6 @@ function slugifyWithUTF8 (text) {
178180
return newText
179181
}
180182

181-
export function isValidURL (str) {
182-
const pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
183-
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
184-
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
185-
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
186-
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
187-
'(\\#[-a-z\\d_]*)?$', 'i') // fragment locator
188-
if (!pattern.test(str)) {
189-
return false
190-
} else {
191-
return true
192-
}
193-
}
194-
195183
// parse meta
196184
export function parseMeta (md, edit, view, toc, tocAffix) {
197185
let lang = null
@@ -1300,7 +1288,7 @@ const pdfPlugin = new Plugin(
13001288

13011289
(match, utils) => {
13021290
const pdfurl = match[1]
1303-
if (!isValidURL(pdfurl)) return match[0]
1291+
if (!isURL(pdfurl)) return match[0]
13041292
const div = $('<div class="pdf raw"></div>')
13051293
div.attr('data-pdfurl', pdfurl)
13061294
return div[0].outerHTML

public/js/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import randomColor from 'randomcolor'
99
import store from 'store'
1010
import hljs from 'highlight.js'
1111

12+
import isURL from 'validator/lib/isURL'
13+
1214
import _ from 'lodash'
1315

1416
import wurl from 'wurl'
@@ -41,7 +43,6 @@ import {
4143
removeDOMEvents,
4244
finishView,
4345
generateToc,
44-
isValidURL,
4546
md,
4647
parseMeta,
4748
postProcess,
@@ -1400,7 +1401,7 @@ $('#gistImportModalConfirm').click(function () {
14001401
if (!gisturl) return
14011402
$('#gistImportModal').modal('hide')
14021403
$('#gistImportModalContent').val('')
1403-
if (!isValidURL(gisturl)) {
1404+
if (!isURL(gisturl)) {
14041405
showMessageModal('<i class="fa fa-github"></i> Import from Gist', 'Not a valid URL :(', '', '', false)
14051406
} else {
14061407
var hostname = wurl('hostname', gisturl)
@@ -1534,7 +1535,7 @@ function replaceAll (data) {
15341535
function importFromUrl (url) {
15351536
// console.log(url);
15361537
if (!url) return
1537-
if (!isValidURL(url)) {
1538+
if (!isURL(url)) {
15381539
showMessageModal('<i class="fa fa-cloud-download"></i> Import from URL', 'Not a valid URL :(', '', '', false)
15391540
return
15401541
}

0 commit comments

Comments
 (0)