Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.

Commit d0b4b0f

Browse files
authored
Fix unexpected arithmetic operations on strings (#20043)
* Update ip_utils.js * Update ip_utils.js * Update ip_utils.js
1 parent 04cf5ee commit d0b4b0f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

chromium/background-scripts/ip_utils.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
(function (exports) {
44

5+
/**
6+
* Parse and convert literal IP address into numerical IP address.
7+
* @param {string} ip
8+
* @returns {number}
9+
*/
510
const parseIp = ip => {
611
if (!/^[0-9.]+$/.test(ip)) {
712
return -1;
813
}
914

15+
/** @type {string[]} */
1016
const octets = ip.split('.');
1117

1218
if (octets.length !== 4) {
@@ -26,14 +32,21 @@ const parseIp = ip => {
2632
return -1;
2733
}
2834

29-
ipN = (ipN << 8) | octet;
35+
ipN = (ipN << 8) | octetN;
3036
}
3137

3238
return ipN >>> 0;
3339
};
3440

41+
/**
42+
* Check if the numeric IP address is within a certain range.
43+
* @param {number} ip
44+
* @param {number[]} range
45+
* @returns {boolean}
46+
*/
3547
const isIpInRange = (ip, [rangeIp, mask]) => (ip & mask) >>> 0 === rangeIp;
3648

49+
// A list of local IP address ranges
3750
const localRanges = [
3851
[/* 0.0.0.0 */ 0x00000000, /* 255.255.255.255 */ 0xffffffff],
3952
[/* 127.0.0.0 */ 0x7f000000, /* 255.0.0.0 */ 0xff000000],
@@ -42,6 +55,11 @@ const localRanges = [
4255
[/* 192.168.0.0 */ 0xc0a80000, /* 255.255.0.0 */ 0xffff0000],
4356
];
4457

58+
/**
59+
* Check if the numeric IP address is inside the local IP address ranges.
60+
* @param {number} ip
61+
* @returns {boolean}
62+
*/
4563
const isLocalIp = ip => localRanges.some(range => isIpInRange(ip, range));
4664

4765
Object.assign(exports, {

0 commit comments

Comments
 (0)