Skip to content

Commit

Permalink
Filter out uncommon URLs where the hostname is an IPv4 address.
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-classen committed Sep 30, 2024
1 parent 1d30dbc commit ddbcd91
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 11 additions & 3 deletions reporting/src/sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import logger from './logger';
import { isHash } from './hash-detector';
//import { isHash } from './hash-detector-new';

function isCharNumber(char) {
const code = char.charCodeAt(0);
Expand Down Expand Up @@ -221,11 +220,17 @@ function tryParseUrl(url) {
}
}

function checkForInternalIp(hostname) {
function isPrivateHostname(hostname) {
// TODO: this could be extended to detect more cases
return hostname === 'localhost' || hostname === '127.0.0.1';
}

// Note: This is a conservative implementation that detects all valid IPv4 addresses.
// It may produce false-positives, so consider this if using for other purposes.
function looksLikeIPv4Address(hostname) {
return /^[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}$/.test(hostname);
}

function looksLikeSafeUrlParameter(key, value) {
return value.length < 18 || /^[a-z-_]+$/.test(value);
}
Expand Down Expand Up @@ -346,9 +351,12 @@ export function sanitizeUrl(url, options = {}) {
if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {
return drop('URL has uncommon protocol');
}
if (checkForInternalIp(parsedUrl.hostname)) {
if (isPrivateHostname(parsedUrl.hostname)) {
return drop('URL is not public');
}
if (looksLikeIPv4Address(parsedUrl.hostname)) {
return drop('hostname is an ipv4 address');
}
if (urlLeaksExtensionId(url)) {
return drop('URL leaks extension ID');
}
Expand Down
16 changes: 16 additions & 0 deletions reporting/test/sanitizer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,22 @@ describe('#sanitizeUrl', function () {
});
});

describe('should drop URls where IP addresses are used as hostnames', function () {
[
'http://182.180.189.84/',
'http://192.168.0.119/',
'http://85.11.187.84/saff/index.php?topic=3221141.0',
'http://10.234.0.1/',
'https://10.234.0.1/',
'http://0.0.0.0/',
'http://1.1.1.1/',
].forEach((url) => {
it(`should drop URL: ${url}`, function () {
shouldBeDropped(url);
});
});
});

describe('should drop URLs with non-standard HTTP ports', function () {
['http://myserver.test:1234/', 'https://www.myserver.test:5678/'].forEach(
(url) => {
Expand Down

0 comments on commit ddbcd91

Please sign in to comment.