diff --git a/AJAXRequest.js b/AJAXRequest.js index 0743163..5ede1b9 100644 --- a/AJAXRequest.js +++ b/AJAXRequest.js @@ -82,11 +82,24 @@ Object.defineProperties(AJAXRequest, { var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name 'localhost|' + // localhost (#76) - '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address + '((\\d{1,3}\\.){3}\\d{1,3})|' + // OR ip (v4) address + '\\[[a-f\\d:.]+\\])' + // OR bracketed IPv6 address '(\\:\\d+)?(\\/[-a-z\\d%_.=~+!]*)*' + // port and path '(\\?[;&a-z\\d%_.~+=/-]*)?' + // query string '(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator - return !!pattern.test(url); + if (!pattern.test(url)) { + return false; + } + var ipv6 = /\[[a-f\d:.]+\]/i.exec(url); + if (ipv6) { + // Validate only the IPv6 host; keep the existing URL rules above. + try { + return new URL('http://' + ipv6[0]).hostname[0] === '['; + } catch (_error) { + return false; + } + } + return true; } } }); diff --git a/eslint.config.mjs b/eslint.config.mjs index 4b2d286..f111fb7 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -12,6 +12,7 @@ export default [ document: 'readonly', console: 'readonly', XMLHttpRequest: 'readonly', + URL: 'readonly', FormData: 'readonly', File: 'readonly', Blob: 'readonly', diff --git a/tests/AJAXRequest.isvalidurl.test.js b/tests/AJAXRequest.isvalidurl.test.js index 752da96..c5559a4 100644 --- a/tests/AJAXRequest.isvalidurl.test.js +++ b/tests/AJAXRequest.isvalidurl.test.js @@ -11,6 +11,7 @@ describe('AJAXRequest.isValidURL', () => { resetContext(); AJAXRequest = loadAJAXRequest({ mocks: { + URL, console: { ...console, info: jest.fn(), warn: jest.fn() } } }); @@ -45,6 +46,23 @@ describe('AJAXRequest.isValidURL', () => { expect(AJAXRequest.isValidURL('http://192.168.1.1:8080/api')).toBe(true); }); + test.each([ + 'http://[::1]:8080/api', + 'https://[2001:db8::1]/api?q=test#result', + 'http://[2001:DB8:0:0:0:0:0:1]', + 'http://[::]', + 'http://[::ffff:192.0.2.1]', + '[::1]:8080/api' + ])('bracketed IPv6 URL (#77): %s', (url) => { + expect(AJAXRequest.isValidURL(url)).toBe(true); + }); + + test.each(['example.com/api', 'localhost:3000', '192.168.1.1/api'])( + 'URL without protocol: %s', (url) => { + expect(AJAXRequest.isValidURL(url)).toBe(true); + } + ); + // #76 — localhost was rejected, should be true test('localhost (#76)', () => { expect(AJAXRequest.isValidURL('http://localhost')).toBe(true); @@ -79,5 +97,27 @@ describe('AJAXRequest.isValidURL', () => { test('string with spaces', () => { expect(AJAXRequest.isValidURL('http://example .com')).toBe(false); }); + + test.each([ + 'http://::1:8080/api', + 'http://[::1', + 'http://[]', + 'http://[1234]', + 'http://[1:2:3:4:5:6:7]', + 'http://[1:2:3:4:5:6:7:8:9]', + 'http://[2001::db8::1]', + 'http://[12345::1]', + 'http://[gggg::1]', + 'http://[::ffff:999.0.2.1]', + 'http://[127.0.0.1]', + 'http://[::1]example.com', + 'http://[::1]:abc/api', + 'http://[::1]/a b', + 'ftp://[::1]', + 'ftp://example.com', + 'http://example.com/a b' + ])('malformed or unsupported URL: %s', (url) => { + expect(AJAXRequest.isValidURL(url)).toBe(false); + }); }); });