From db3bbb63e1919deac2e4ecbba062329c64812a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B3=E5=BB=B7=E5=AE=89?= <73953029+nrps9909@users.noreply.github.com> Date: Tue, 1 Dec 2026 00:41:16 +0800 Subject: [PATCH] fix: validate URL host and port formats --- src/rule/url.ts | 13 ++++++++----- tests/url.spec.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/rule/url.ts b/src/rule/url.ts index 5acc0a4..235c9c9 100644 --- a/src/rule/url.ts +++ b/src/rule/url.ts @@ -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}$)`); @@ -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'); diff --git a/tests/url.spec.ts b/tests/url.spec.ts index 49d7426..04c8f8c 100644 --- a/tests/url.spec.ts +++ b/tests/url.spec.ts @@ -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: {