Skip to content

Commit 4f5e6bf

Browse files
brandon93ssindresorhus
authored andcommitted
Validate that url is UTF-8 encoded (#441)
1 parent 841ab63 commit 4f5e6bf

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,13 @@ function normalizeArguments(url, opts) {
444444
throw new TypeError(`Parameter \`url\` must be a string or object, not ${is(url)}`);
445445
} else if (is.string(url)) {
446446
url = url.replace(/^unix:/, 'http://$&');
447+
448+
try {
449+
decodeURI(url);
450+
} catch (err) {
451+
throw new Error('Parameter `url` must contain valid UTF-8 character sequences');
452+
}
453+
447454
url = urlParseLax(url);
448455
if (url.auth) {
449456
throw new Error('Basic authentication must be done with the `auth` option');

test/arguments.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ test('url is required', async t => {
2929
t.regex(err.message, /Parameter `url` must be a string or object, not undefined/);
3030
});
3131

32+
test('url should be utf-8 encoded', async t => {
33+
const err = await t.throws(got(`${s.url}/%D2%E0%EB%EB%E8%ED`));
34+
t.regex(err.message, /Parameter `url` must contain valid UTF-8 character sequences/);
35+
});
36+
3237
test('options are optional', async t => {
3338
t.is((await got(`${s.url}/test`)).body, '/test');
3439
});

0 commit comments

Comments
 (0)