Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/rule/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export default () => {
`(?:${v6seg}:){1}(?:(?::${v6seg}){0,4}:${v4}|(?::${v6seg}){1,6}|:)`, // 1:: 1::3:4:5:6:7:8 1::8 1::
`(?::(?:(?::${v6seg}){0,5}:${v4}|(?::${v6seg}){1,7}|:))`, // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::
];
const v6Address = `(?:${v6List.join('|')})`;
const v6Eth0 = `(?:%[0-9a-zA-Z]{1,})?`; // %eth0 %1

const v6 = `(?:${v6List.join('|')})${v6Eth0}`;
const v6 = `${v6Address}${v6Eth0}`;

// Pre-compile only the exact regexes because adding a global flag make regexes stateful
const v46Exact = new RegExp(`(?:^${v4}$)|(?:^${v6}$)`);
Expand All @@ -47,11 +47,14 @@ export default () => {
const protocol = `(?:(?:[a-z]+:)?//)`;
const auth = '(?:\\S+(?::\\S*)?@)?';
const ipv4 = ip.v4().source;
const ipv6 = ip.v6().source;
// RFC 6874 section 2 uses %25 to delimit a zone in an IPv6 URL host.
const urlZone = '(?:%25(?:[a-z0-9._~-]|%[a-f0-9]{2})+)?';
const ipv6 = `\\[${v6Address}${urlZone}\\]`;
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 tld = '(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}|xn--[a-z0-9](?:[a-z0-9-]{0,57}[a-z0-9])?))';
// One to five decimal digits, with a maximum value of 65535.
const port = '(?::(?:\\d{1,4}|[0-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5]))?';
const path = '(?:[/?#][^\\s"]*)?';
const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ipv4}|${ipv6}|${host}(?:${domain}${tld})?)${port}${path}`;
urlReg = new RegExp(`(?:^${regex}$)`, 'i');
Expand Down
28 changes: 28 additions & 0 deletions tests/url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ describe('url', () => {
);
});

it.each([
'http://[::1]:8080/',
'http://[fe80::1%25eth0]:8/',
'http://[fe80::1%25eth%2D0]/',
'http://example.com:8/',
'http://example.com:65535/',
'http://example.xn--p1ai/',
'http://example.рф/',
])('accepts URL %s', async v => {
const schema = new Schema({ v: { type: 'url' } });
await expect(schema.validate({ v })).resolves.toEqual({ v });
});

it.each([
'http://::1/',
'http://fe80::1%eth0/',
'http://[fe80::1%eth0]/',
'http://[fe80::1%25]/',
'http://[fe80::1%25eth%2G0]/',
'http://example.com:65536/',
'http://example.com:99999/',
])('rejects URL %s', async v => {
const schema = new Schema({ v: { type: 'url' } });
await expect(schema.validate({ v })).rejects.toMatchObject({
errors: [{ message: 'v is not a valid url' }],
});
});

it('works for single-label host url', done => {
new Schema({
v: {
Expand Down
Loading