Skip to content
This repository was archived by the owner on Apr 30, 2021. It is now read-only.

Commit 4087bbe

Browse files
author
Yevgeny Pats
authored
Merge pull request #13 from bookmoons/url
Add url fuzzer
2 parents 8f8c020 + 2c856f5 commit 4087bbe

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

examples/url/fuzz.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const url = require('url')
2+
3+
async function fuzz (bytes) {
4+
const string = String.fromCodePoint(...bytes)
5+
try {
6+
whatwg(string)
7+
params(string)
8+
legacy(string)
9+
} catch (error) {
10+
if (!acceptable(error)) throw error
11+
}
12+
}
13+
14+
function whatwg (string) {
15+
const parsed = new url.URL(string)
16+
parsed.toString()
17+
parsed.toJSON()
18+
}
19+
20+
function params (string) {
21+
const parsed = new url.URLSearchParams(string)
22+
parsed.toString()
23+
}
24+
25+
function legacy (string) {
26+
/* eslint-disable-next-line node/no-deprecated-api */
27+
const parsed = url.parse(string)
28+
url.format(parsed)
29+
}
30+
31+
function acceptable (error) {
32+
return (
33+
error instanceof URIError ||
34+
expected.code.includes(error.code)
35+
)
36+
}
37+
38+
const expected = {
39+
code: [
40+
'ERR_INVALID_URL'
41+
]
42+
}
43+
44+
exports.fuzz = fuzz

examples/url/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "url-fuzz",
3+
"version": "1.0.0",
4+
"main": "fuzz.js",
5+
"license": "ISC"
6+
}

0 commit comments

Comments
 (0)