Skip to content

Commit bc7bb3a

Browse files
committed
Co-authored-by: Copilot <[email protected]> Copilot-Session: 51629aea-67ec-442e-9b0f-9b0f88b74fea
1 parent 7614360 commit bc7bb3a

7 files changed

Lines changed: 370 additions & 42 deletions

File tree

‎node_modules/ip-address/dist/common.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
exports.isInSubnet = isInSubnet;
44
exports.isHostInSubnet = isHostInSubnet;
5+
exports.isGloballyReachable = isGloballyReachable;
6+
exports.offsetBigInt = offsetBigInt;
57
exports.isCorrect = isCorrect;
68
exports.prefixLengthFromMask = prefixLengthFromMask;
79
exports.assertByteArray = assertByteArray;
@@ -36,10 +38,56 @@ function isInSubnet(address) {
3638
* {@link isInSubnet} when classifying a single address — notably when the
3739
* address came from untrusted input and the result backs a trust-boundary
3840
* decision such as an SSRF allow/deny filter.
41+
*
42+
* An address of one family is never inside a network of the other, so an
43+
* `Address4` against an `Address6` (or the reverse) is `false`. To compare
44+
* across families, convert first: `Address6.fromAddress4()`, `to4()`, or
45+
* `toAddress4Nat64()`.
3946
*/
4047
function isHostInSubnet(address) {
48+
// mask() is a bit string of the family's width, and the leading bits of a
49+
// 32-bit string can coincide with those of a 128-bit one (a00::1 and
50+
// 10.0.0.0/8 both mask to 00001010), so the widths must agree before the
51+
// strings are compared.
52+
if (this.binaryZeroPad().length !== address.binaryZeroPad().length) {
53+
return false;
54+
}
4155
return this.mask(address.subnetMask) === address.mask();
4256
}
57+
/**
58+
* Returns whether the registry marks this address globally reachable: the
59+
* answer of the most specific entry containing it that has one, or `true`
60+
* when no entry contains it.
61+
*/
62+
function isGloballyReachable(entries) {
63+
let best = null;
64+
for (let i = 0; i < entries.length; i++) {
65+
const entry = entries[i];
66+
if (entry.reachable !== null &&
67+
isHostInSubnet.call(this, entry.subnet) &&
68+
(best === null || entry.subnet.subnetMask > best.subnet.subnetMask)) {
69+
best = entry;
70+
}
71+
}
72+
return best === null ? true : best.reachable;
73+
}
74+
/**
75+
* Adds `n` to `value` and returns the result, throwing `AddressError` unless
76+
* `n` is an integer and the result stays within `[0, 2**bits - 1]`.
77+
*/
78+
function offsetBigInt(value, n, bits, family) {
79+
if (typeof n === 'number' && !Number.isSafeInteger(n)) {
80+
throw new address_error_1.AddressError(`${family} offset must be an integer`);
81+
}
82+
if (typeof n !== 'number' && typeof n !== 'bigint') {
83+
throw new address_error_1.AddressError(`${family} offset must be an integer`);
84+
}
85+
const result = value + BigInt(n);
86+
if (result < BigInt(0) || result > (BigInt(1) << BigInt(bits)) - BigInt(1)) {
87+
throw new address_error_1.AddressError(`${family} offset leaves the address space`);
88+
}
89+
return result;
90+
}
4391
function isCorrect(defaultBits) {
4492
return function isCorrectForm() {
4593
if (this.addressMinusSuffix !== this.correctForm()) {

‎node_modules/ip-address/dist/ipv4.js‎

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class Address4 {
4848
*/
4949
this.isCorrect = isCorrect4;
5050
/**
51-
* Returns true if the given address is in the subnet of the current address
51+
* Returns true if the given address is in the subnet of the current address.
52+
* An `Address6` is never in the subnet of an `Address4`; convert with
53+
* `to4()` or `Address6.fromAddress4()` to compare across families.
5254
* @returns {boolean}
5355
*/
5456
this.isInSubnet = common.isInSubnet;
@@ -58,6 +60,8 @@ class Address4 {
5860
* when classifying a single address, so the answer doesn't change with the
5961
* CIDR suffix the caller happened to write — notably when the address came
6062
* from untrusted input and the result backs a trust-boundary decision.
63+
* An `Address6` is never in the subnet of an `Address4`; convert with
64+
* `to4()` or `Address6.fromAddress4()` to compare across families.
6165
* @returns {boolean}
6266
*/
6367
this.isHostInSubnet = common.isHostInSubnet;
@@ -72,6 +76,13 @@ class Address4 {
7276
}
7377
address = address.replace(constants.RE_SUBNET_STRING, '');
7478
}
79+
// Four three-digit octets and three dots: the longest well-formed address
80+
// is 15 characters. Longer input is rejected before parsing, as Address6
81+
// does at its own limit.
82+
const longest = constants.GROUPS * 4 - 1;
83+
if (address.length > longest) {
84+
throw new address_error_1.AddressError(`IPv4 addresses are at most ${longest} characters.`);
85+
}
7586
this.addressMinusSuffix = address;
7687
this.parsedAddress = this.parse(address);
7788
}
@@ -213,16 +224,20 @@ class Address4 {
213224
return Address4.fromHex(integer.toString(16).padStart(8, '0'));
214225
}
215226
/**
216-
* Return an address from in-addr.arpa form
227+
* Return an address from in-addr.arpa form: the four octets reversed, with
228+
* or without the `.in-addr.arpa` suffix and root dot, in any case. Throws
229+
* `AddressError` unless the reversed labels form a valid IPv4 address, so
230+
* `fromArpa(x.reverseForm())` round-trips {@link reverseForm}.
217231
* @param {string} arpaFormAddress - an 'in-addr.arpa' form ipv4 address
218232
* @returns {Adress4}
219233
* @example
220-
* var address = Address4.fromArpa(42.2.0.192.in-addr.arpa.)
234+
* var address = Address4.fromArpa('42.2.0.192.in-addr.arpa.')
221235
* address.correctForm(); // '192.0.2.42'
222236
*/
223237
static fromArpa(arpaFormAddress) {
224-
// remove ending ".in-addr.arpa." or just "."
225-
const leader = arpaFormAddress.replace(/(\.in-addr\.arpa)?\.$/, '');
238+
// remove an ending ".in-addr.arpa", in any case and with or without the
239+
// root dot, as Address6.fromArpa does for ".ip6.arpa"
240+
const leader = arpaFormAddress.replace(/(\.in-addr\.arpa)?\.?$/i, '');
226241
const address = leader.split('.').reverse().join('.');
227242
return new Address4(address);
228243
}
@@ -285,6 +300,33 @@ class Address4 {
285300
const adjust = BigInt('1');
286301
return Address4.fromBigInt(this._startAddress() + adjust);
287302
}
303+
/**
304+
* Returns the address `n` addresses after this one (or before, when `n` is
305+
* negative), keeping this address's subnet mask. Throws `AddressError` when
306+
* the result would fall outside the IPv4 address space or `n` is not an
307+
* integer.
308+
* @param {number | bigint} n
309+
* @returns {Address4}
310+
* @example
311+
* new Address4('10.0.0.0/24').offset(1).correctForm(); // '10.0.0.1'
312+
*/
313+
offset(n) {
314+
return Address4.fromBigInt(common.offsetBigInt(this.bigInt(), n, constants.BITS, 'IPv4')).withSubnetMask(this.subnetMask);
315+
}
316+
/**
317+
* Returns the network that follows this address's network: the address after
318+
* {@link endAddress}, with the same subnet mask. Throws `AddressError` when
319+
* this network is the last one in the address space.
320+
* @returns {Address4}
321+
* @example
322+
* new Address4('10.0.0.0/24').nextNetwork().networkForm(); // '10.0.1.0/24'
323+
*/
324+
nextNetwork() {
325+
return Address4.fromBigInt(common.offsetBigInt(this._endAddress(), 1, constants.BITS, 'IPv4')).withSubnetMask(this.subnetMask);
326+
}
327+
withSubnetMask(subnetMask) {
328+
return new Address4(`${this.correctForm()}/${subnetMask}`);
329+
}
288330
/**
289331
* Helper function getting end address.
290332
* @returns {bigint}
@@ -462,6 +504,43 @@ class Address4 {
462504
isCGNAT() {
463505
return this.isHostInSubnet(CGNAT_V4);
464506
}
507+
/**
508+
* Returns true if the address is in one of the documentation ranges
509+
* `192.0.2.0/24`, `198.51.100.0/24`, or `203.0.113.0/24` ([RFC 5737](https://datatracker.ietf.org/doc/html/rfc5737)).
510+
* @returns {boolean}
511+
*/
512+
isDocumentation() {
513+
return DOCUMENTATION_V4.some((subnet) => this.isHostInSubnet(subnet));
514+
}
515+
/**
516+
* Returns true if the address is in the benchmarking range `198.18.0.0/15` ([RFC 2544](https://datatracker.ietf.org/doc/html/rfc2544)).
517+
* @returns {boolean}
518+
*/
519+
isBenchmarking() {
520+
return this.isHostInSubnet(BENCHMARKING_V4);
521+
}
522+
/**
523+
* Returns true if the address is in the reserved range `240.0.0.0/4` ([RFC 1112](https://datatracker.ietf.org/doc/html/rfc1112)),
524+
* which includes the limited broadcast address.
525+
* @returns {boolean}
526+
*/
527+
isReserved() {
528+
return this.isHostInSubnet(RESERVED_V4);
529+
}
530+
/**
531+
* Returns true if the address is globally reachable: not multicast, and not
532+
* in any block the [IANA IPv4 Special-Purpose Address Registry](https://www.iana.org/assignments/iana-ipv4-special-registry/)
533+
* marks as not globally reachable. That covers everything the individual
534+
* classifiers name (private, loopback, link-local, CGNAT, unspecified,
535+
* broadcast, documentation, benchmarking, reserved) and the blocks they do
536+
* not, such as `0.0.0.0/8` and the IETF protocol assignments in
537+
* `192.0.0.0/24`. This is the single predicate to use where a request must
538+
* not reach an internal or special-purpose destination; see SECURITY.md.
539+
* @returns {boolean}
540+
*/
541+
isGlobal() {
542+
return !this.isMulticast() && common.isGloballyReachable.call(this, SPECIAL_PURPOSE_V4);
543+
}
465544
/**
466545
* Returns a zero-padded base-2 string representation of the address
467546
* @returns {string}
@@ -502,4 +581,15 @@ const LINK_LOCAL_V4 = new Address4('169.254.0.0/16');
502581
const UNSPECIFIED_V4 = new Address4('0.0.0.0/32');
503582
const BROADCAST_V4 = new Address4('255.255.255.255/32');
504583
const CGNAT_V4 = new Address4('100.64.0.0/10');
584+
const DOCUMENTATION_V4 = [
585+
new Address4('192.0.2.0/24'),
586+
new Address4('198.51.100.0/24'),
587+
new Address4('203.0.113.0/24'),
588+
];
589+
const BENCHMARKING_V4 = new Address4('198.18.0.0/15');
590+
const RESERVED_V4 = new Address4('240.0.0.0/4');
591+
const SPECIAL_PURPOSE_V4 = constants.SPECIAL_PURPOSE.map(([cidr, , reachable]) => ({
592+
subnet: new Address4(cidr),
593+
reachable,
594+
}));
505595
//# sourceMappingURL=ipv4.js.map

0 commit comments

Comments
 (0)