Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
124 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// https://github.com/kevva/url-regex/blob/master/index.js | ||
|
||
const word = '[a-fA-F\\d:]'; | ||
const b = options => | ||
options && options.includeBoundaries | ||
? `(?:(?<=\\s|^)(?=${word})|(?<=${word})(?=\\s|$))` | ||
: ''; | ||
|
||
const v4 = | ||
'(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}'; | ||
|
||
const v6seg = '[a-fA-F\\d]{1,4}'; | ||
const v6 = ` | ||
(?: | ||
(?:${v6seg}:){7}(?:${v6seg}|:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8 | ||
(?:${v6seg}:){6}(?:${v4}|:${v6seg}|:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4 | ||
(?:${v6seg}:){5}(?::${v4}|(?::${v6seg}){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4 | ||
(?:${v6seg}:){4}(?:(?::${v6seg}){0,1}:${v4}|(?::${v6seg}){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4 | ||
(?:${v6seg}:){3}(?:(?::${v6seg}){0,2}:${v4}|(?::${v6seg}){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4 | ||
(?:${v6seg}:){2}(?:(?::${v6seg}){0,3}:${v4}|(?::${v6seg}){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4 | ||
(?:${v6seg}:){1}(?:(?::${v6seg}){0,4}:${v4}|(?::${v6seg}){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4 | ||
(?::(?:(?::${v6seg}){0,5}:${v4}|(?::${v6seg}){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4 | ||
)(?:%[0-9a-zA-Z]{1,})? // %eth0 %1 | ||
` | ||
.replace(/\s*\/\/.*$/gm, '') | ||
.replace(/\n/g, '') | ||
.trim(); | ||
|
||
// Pre-compile only the exact regexes because adding a global flag make regexes stateful | ||
const v46Exact = new RegExp(`(?:^${v4}$)|(?:^${v6}$)`); | ||
const v4exact = new RegExp(`^${v4}$`); | ||
const v6exact = new RegExp(`^${v6}$`); | ||
|
||
const ip = options => | ||
options && options.exact | ||
? v46Exact | ||
: new RegExp( | ||
`(?:${b(options)}${v4}${b(options)})|(?:${b(options)}${v6}${b( | ||
options, | ||
)})`, | ||
'g', | ||
); | ||
|
||
ip.v4 = options => | ||
options && options.exact | ||
? v4exact | ||
: new RegExp(`${b(options)}${v4}${b(options)}`, 'g'); | ||
ip.v6 = options => | ||
options && options.exact | ||
? v6exact | ||
: new RegExp(`${b(options)}${v6}${b(options)}`, 'g'); | ||
|
||
let reg; | ||
|
||
export default () => { | ||
if (reg) { | ||
return reg; | ||
} | ||
const protocol = `(?:(?:[a-z]+:)?//)`; | ||
const auth = '(?:\\S+(?::\\S*)?@)?'; | ||
const ipSource = ip.v4({}).source; | ||
const host = '(?:(?:[a-z\\u00a1-\\uffff0-9][-_]*)*[a-z\\u00a1-\\uffff0-9]+)'; | ||
const domain = | ||
'(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*'; | ||
const tld = `(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))`; | ||
const port = '(?::\\d{2,5})?'; | ||
const path = '(?:[/?#][^\\s"]*)?'; | ||
const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ipSource}|${host}${domain}${tld})${port}${path}`; | ||
reg = new RegExp(`(?:^${regex}$)`, 'i'); | ||
return reg; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import AsyncValidator from '../src'; | ||
|
||
const validator = new AsyncValidator({ | ||
v: { | ||
type: 'url', | ||
}, | ||
}); | ||
|
||
for (var i = 1; i <= 1000; i++) { | ||
var time = Date.now(); | ||
var attack_str = '//a.b' + 'c1'.repeat(i) + 'a'; | ||
validator.validate({ | ||
v: attack_str, | ||
}); | ||
var time_cost = Date.now() - time; | ||
console.log( | ||
'attack_str.length: ' + attack_str.length + ': ' + time_cost + ' ms', | ||
); | ||
} | ||
|
||
if (false) { | ||
console.log('*'.repeat(10)); | ||
|
||
for (var i = 1; i <= 50000; i++) { | ||
var time = Date.now(); | ||
var attack_str = '//' + ':'.repeat(i * 10000) + '@'; | ||
validator.validate({ | ||
v: attack_str, | ||
}); | ||
var time_cost = Date.now() - time; | ||
console.log( | ||
'attack_str.length: ' + attack_str.length + ': ' + time_cost + ' ms', | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters