Skip to content

Commit 7e6ee3c

Browse files
authored
Merge pull request #89 from teacoder-team/dev
fix: ip whitelist matcher
2 parents 1c813f8 + 6d291c4 commit 7e6ee3c

5 files changed

Lines changed: 36 additions & 71 deletions

File tree

packages/nestjs-yookassa/dist/webhook/utils/ip-matcher.util.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
exports.isIpAllowed = isIpAllowed;
4-
const cidr_matcher_1 = require("cidr-matcher");
5-
const matcher = new cidr_matcher_1.default();
4+
const ipaddr = require("ipaddr.js");
65
/**
76
* Проверяет, входит ли IP-адрес в список разрешённых CIDR / IP.
87
*
@@ -16,7 +15,14 @@ function isIpAllowed(clientIp, whitelist) {
1615
if (!clientIp)
1716
return false;
1817
try {
19-
return matcher.match(clientIp, whitelist);
18+
const ip = ipaddr.parse(clientIp);
19+
return whitelist.some(rule => {
20+
if (rule.includes('/')) {
21+
const [range, prefix] = ipaddr.parseCIDR(rule);
22+
return ip.match(range, prefix);
23+
}
24+
return ip.toString() === ipaddr.parse(rule).toString();
25+
});
2026
}
2127
catch (_a) {
2228
return false;

packages/nestjs-yookassa/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nestjs-yookassa",
3-
"version": "2.3.1",
3+
"version": "2.3.2",
44
"description": "A NestJS library for integrating with YooKassa API",
55
"keywords": [
66
"nest",
@@ -44,8 +44,8 @@
4444
"dependencies": {
4545
"@nestjs/axios": "^4.0.1",
4646
"axios": "^1.11.0",
47-
"cidr-matcher": "^2.1.1",
4847
"https-proxy-agent": "^7.0.6",
48+
"ipaddr.js": "^2.3.0",
4949
"rxjs": "^7.8.2",
5050
"undici": "^7.16.0"
5151
},

packages/nestjs-yookassa/src/webhook/enums/notification-event.enum.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ export enum NotificationEventEnum {
2020
/**
2121
* Возврат успешно завершен.
2222
*/
23-
REFUND_SUCCEEDED = 'refund.succeeded',
23+
REFUND_SUCCEEDED = 'refund.succeeded'
2424
}
25-

packages/nestjs-yookassa/src/webhook/utils/ip-matcher.util.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import CidrMatcher from 'cidr-matcher'
2-
3-
const matcher = new CidrMatcher()
1+
import * as ipaddr from 'ipaddr.js'
42

53
/**
64
* Проверяет, входит ли IP-адрес в список разрешённых CIDR / IP.
@@ -18,7 +16,16 @@ export function isIpAllowed(
1816
if (!clientIp) return false
1917

2018
try {
21-
return matcher.match(clientIp, whitelist as string[])
19+
const ip = ipaddr.parse(clientIp)
20+
21+
return whitelist.some(rule => {
22+
if (rule.includes('/')) {
23+
const [range, prefix] = ipaddr.parseCIDR(rule)
24+
return ip.match(range, prefix)
25+
}
26+
27+
return ip.toString() === ipaddr.parse(rule).toString()
28+
})
2229
} catch {
2330
return false
2431
}

pnpm-lock.yaml

Lines changed: 13 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)