Skip to content

Commit

Permalink
fix: use ip-address for remote-strategy (#8380)
Browse files Browse the repository at this point in the history
Removed direct dependency on ip in favor of maintained `ip-address`.

Copied from unleash-client-node.
  • Loading branch information
chriswk authored Oct 7, 2024
1 parent 864984e commit 942555b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"hash-sum": "^2.0.0",
"helmet": "^6.0.0",
"http-errors": "^2.0.0",
"ip": "^2.0.1",
"ip-address": "^10.0.0",
"joi": "^17.13.3",
"js-sha256": "^0.11.0",
"js-yaml": "^4.1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Strategy } from './strategy';
import type { Context } from '../context';
import ip from 'ip';
import { Address4 } from 'ip-address';

export default class RemoteAddressStrategy extends Strategy {
constructor() {
Expand All @@ -16,16 +16,17 @@ export default class RemoteAddressStrategy extends Strategy {
if (range === context.remoteAddress) {
return true;
}
if (!ip.isV6Format(range)) {
if (Address4.isValid(range)) {
try {
return ip
.cidrSubnet(range)
.contains(context.remoteAddress);
const subnetRange = new Address4(range);
const remoteAddress = new Address4(
context.remoteAddress || '',
);
return remoteAddress.isInSubnet(subnetRange);
} catch (err) {
return false;
}
}
return false;
},
);
}
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5129,6 +5129,13 @@ __metadata:
languageName: node
linkType: hard

"ip-address@npm:^10.0.0":
version: 10.0.0
resolution: "ip-address@npm:10.0.0"
checksum: 10c0/de28efeac726bce4a088ad2b31a3eefaf8d0f586ece6808366aef68029c0ad1387c1db98de23ddb1a888caf914de80424c1baf548e580dae65e0a9b24073f542
languageName: node
linkType: hard

"ip-address@npm:^9.0.5":
version: 9.0.5
resolution: "ip-address@npm:9.0.5"
Expand Down Expand Up @@ -9461,7 +9468,7 @@ __metadata:
helmet: "npm:^6.0.0"
http-errors: "npm:^2.0.0"
husky: "npm:^9.0.11"
ip: "npm:^2.0.1"
ip-address: "npm:^10.0.0"
jest: "npm:29.7.0"
jest-junit: "npm:^16.0.0"
joi: "npm:^17.13.3"
Expand Down

0 comments on commit 942555b

Please sign in to comment.