Skip to content

Commit

Permalink
Improve IPV6 check for URLs
Browse files Browse the repository at this point in the history
- While it's not rock solid, it's better than the current function.
Ultimately should be fixed by #2517
- Resolves #10160
  • Loading branch information
Gusted committed Oct 24, 2022
1 parent cc08290 commit 86107ae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/utils/ipv6.ts
@@ -1,10 +1,12 @@
const simpleIPV6Regex = /\[[0-9:a-zA-Z]*?\]/;

export function isIPV6(url: string) {
const openingBracketIndex = url.indexOf('[');
if (openingBracketIndex < 0) {
const openingBracketIndex = simpleIPV6Regex.exec(url);
if (!openingBracketIndex) {
return false;
}
const queryIndex = url.indexOf('?');
if (queryIndex >= 0 && openingBracketIndex > queryIndex) {
if (queryIndex >= 0 && openingBracketIndex.index > queryIndex) {
return false;
}
return true;
Expand Down
4 changes: 4 additions & 0 deletions tests/inject/utils/url.tests.ts
@@ -1,5 +1,6 @@
import {isURLEnabled, isURLMatched, isPDF, isFullyQualifiedDomain, getURLHostOrProtocol, getAbsoluteURL} from '../../../src/utils/url';
import type {UserSettings} from '../../../src/definitions';
import {isIPV6} from '../../../src/utils/ipv6';

it('URL is enabled', () => {
// Not invert listed
Expand Down Expand Up @@ -250,6 +251,9 @@ it('URL is enabled', () => {
'google.co.uk/order.php?bar=[foo]',
'[2001:4860:4860::8844]',
)).toEqual(false);
expect(isIPV6('file:///C:/[/test.html')).toEqual(false);
expect(isIPV6('file:///C:/[/test.html]')).toEqual(false);
expect(isIPV6('[2001:4860:4860::8844]')).toEqual(true);

// Temporary Dark Sites list fix
expect(isURLEnabled(
Expand Down

0 comments on commit 86107ae

Please sign in to comment.