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
17 changes: 15 additions & 2 deletions AJAXRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
});
Expand Down
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default [
document: 'readonly',
console: 'readonly',
XMLHttpRequest: 'readonly',
URL: 'readonly',
FormData: 'readonly',
File: 'readonly',
Blob: 'readonly',
Expand Down
40 changes: 40 additions & 0 deletions tests/AJAXRequest.isvalidurl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('AJAXRequest.isValidURL', () => {
resetContext();
AJAXRequest = loadAJAXRequest({
mocks: {
URL,
console: { ...console, info: jest.fn(), warn: jest.fn() }
}
});
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
});
});