@prosopo/user-access-policy 3.14.14 → 3.14.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build$colon$cjs.log +2 -5
- package/.turbo/turbo-build$colon$tsc.log +14 -17
- package/.turbo/turbo-build.log +3 -9
- package/CHANGELOG.md +18 -0
- package/LICENSE +201 -0
- package/package.json +83 -81
- package/src/.export.ts +0 -65
- package/src/api/.export.ts +0 -25
- package/src/api/accessRulesApiClient.ts +0 -13
- package/src/api/delete/.export.ts +0 -18
- package/src/api/delete/deleteAllRules.ts +0 -47
- package/src/api/delete/deleteRuleGroups.ts +0 -96
- package/src/api/delete/deleteRules.ts +0 -87
- package/src/api/read/.export.ts +0 -25
- package/src/api/read/fetchRules.ts +0 -96
- package/src/api/read/findRuleIds.ts +0 -97
- package/src/api/read/getMissingIds.ts +0 -81
- package/src/api/ruleApiRoutes.ts +0 -146
- package/src/api/rulesApiClient.ts +0 -154
- package/src/api/write/.export.ts +0 -15
- package/src/api/write/insertRules.ts +0 -185
- package/src/api/write/rehashRules.ts +0 -85
- package/src/classifyBrowser.ts +0 -77
- package/src/classifyOs.ts +0 -74
- package/src/headerMatch.ts +0 -180
- package/src/matchedRule.ts +0 -99
- package/src/mongoose/.export.ts +0 -15
- package/src/mongoose/mongooseRuleSchema.ts +0 -75
- package/src/redis/.export.ts +0 -17
- package/src/redis/reader/redisAggregate.ts +0 -157
- package/src/redis/reader/redisRulesQuery.ts +0 -268
- package/src/redis/reader/redisRulesReader.ts +0 -738
- package/src/redis/reader/redisRulesSplitQuery.ts +0 -234
- package/src/redis/redisClient.ts +0 -125
- package/src/redis/redisRuleIndex.ts +0 -121
- package/src/redis/redisRulesStorage.ts +0 -68
- package/src/redis/redisRulesWriter.ts +0 -172
- package/src/rule.ts +0 -122
- package/src/ruleInput/.export.ts +0 -19
- package/src/ruleInput/policyInput.ts +0 -120
- package/src/ruleInput/ruleInput.ts +0 -123
- package/src/ruleInput/userScopeInput.ts +0 -142
- package/src/ruleRecord.ts +0 -77
- package/src/rulesStorage.ts +0 -90
- package/src/tests/blockingRuleShapes.unit.test.ts +0 -244
- package/src/tests/classifyBrowser.unit.test.ts +0 -138
- package/src/tests/classifyOs.unit.test.ts +0 -97
- package/src/tests/headerMatch.unit.test.ts +0 -350
- package/src/tests/insertRulesEndpoint.unit.test.ts +0 -89
- package/src/tests/matchedRule.unit.test.ts +0 -184
- package/src/tests/policyInput.unit.test.ts +0 -218
- package/src/tests/redis/deferredPoolClause.benchmark.ts +0 -249
- package/src/tests/redis/reader/redisAggregate.unit.test.ts +0 -121
- package/src/tests/redis/reader/redisRulesQuery.unit.test.ts +0 -349
- package/src/tests/redis/reader/redisRulesSplitQuery.unit.test.ts +0 -297
- package/src/tests/redis/redisRulesReaderLoad.benchmark.integration.test.ts +0 -520
- package/src/tests/redis/redisRulesReaderRank.benchmark.integration.test.ts +0 -350
- package/src/tests/redis/redisRulesStorage.integration.test.ts +0 -1811
- package/src/tests/testLogger.ts +0 -38
- package/src/tests/transformRule.unit.test.ts +0 -345
- package/src/transformRule.ts +0 -133
- package/tsconfig.cjs.json +0 -41
- package/tsconfig.json +0 -47
- package/tsconfig.tsbuildinfo +0 -1
- package/tsconfig.types.json +0 -9
package/src/tests/testLogger.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import type { Logger } from "@prosopo/logger";
|
|
16
|
-
import { vi } from "vitest";
|
|
17
|
-
|
|
18
|
-
const loggerMockedInstance: Logger = {
|
|
19
|
-
trace: vi.fn(),
|
|
20
|
-
debug: vi.fn(),
|
|
21
|
-
info: vi.fn(),
|
|
22
|
-
warn: vi.fn(),
|
|
23
|
-
error: vi.fn(),
|
|
24
|
-
fatal: vi.fn(),
|
|
25
|
-
log: vi.fn(),
|
|
26
|
-
setLogLevel: vi.fn(),
|
|
27
|
-
getLogLevel: vi.fn(),
|
|
28
|
-
with: vi.fn().mockReturnThis(),
|
|
29
|
-
getScope: vi.fn().mockReturnValue("test-scope"),
|
|
30
|
-
getPretty: vi.fn().mockReturnValue(false),
|
|
31
|
-
setPretty: vi.fn(),
|
|
32
|
-
getPrintStack: vi.fn().mockReturnValue(false),
|
|
33
|
-
setPrintStack: vi.fn(),
|
|
34
|
-
getFormat: vi.fn().mockReturnValue("json"),
|
|
35
|
-
setFormat: vi.fn(),
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export { loggerMockedInstance };
|
|
@@ -1,345 +0,0 @@
|
|
|
1
|
-
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import { CaptchaType, ResultReason } from "@prosopo/types";
|
|
16
|
-
import { Address4, Address6 } from "ip-address";
|
|
17
|
-
import { describe, expect, it } from "vitest";
|
|
18
|
-
import { AccessPolicyType, type AccessRule } from "#policy/rule.js";
|
|
19
|
-
import type { AccessRuleRecord } from "#policy/ruleRecord.js";
|
|
20
|
-
import {
|
|
21
|
-
getCidrFromNumericIpRange,
|
|
22
|
-
makeAccessRuleHash,
|
|
23
|
-
transformAccessRuleIntoRecord,
|
|
24
|
-
transformAccessRuleRecordIntoRule,
|
|
25
|
-
} from "#policy/transformRule.js";
|
|
26
|
-
|
|
27
|
-
describe("makeAccessRuleHash", () => {
|
|
28
|
-
it("should make md5 rule hash", () => {
|
|
29
|
-
const rule: AccessRule = {
|
|
30
|
-
type: AccessPolicyType.Restrict,
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const hash = makeAccessRuleHash(rule);
|
|
34
|
-
|
|
35
|
-
expect(hash).toEqual("755f7123e8f766b6692218b1014b85ca");
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("should handle bigint rule properties", () => {
|
|
39
|
-
const rule: AccessRule = {
|
|
40
|
-
type: AccessPolicyType.Restrict,
|
|
41
|
-
numericIp: 100n,
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const hash = makeAccessRuleHash(rule);
|
|
45
|
-
|
|
46
|
-
expect(hash).toEqual("ab3fea02b8ef3309fb3fa6f6f703c55a");
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it("should make same hash for same values regardless of properties order", () => {
|
|
50
|
-
const rule1: AccessRule = {
|
|
51
|
-
type: AccessPolicyType.Restrict,
|
|
52
|
-
description: "description",
|
|
53
|
-
};
|
|
54
|
-
const rule2: AccessRule = {
|
|
55
|
-
description: "description",
|
|
56
|
-
type: AccessPolicyType.Restrict,
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const hash1 = makeAccessRuleHash(rule1);
|
|
60
|
-
const hash2 = makeAccessRuleHash(rule2);
|
|
61
|
-
|
|
62
|
-
expect(hash1).toEqual("5ed2f7de7ba1c6acc617f9246f19d35b");
|
|
63
|
-
|
|
64
|
-
expect(hash1).toEqual(hash2);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("should include asn in the rule hash", () => {
|
|
68
|
-
const ruleWithoutAsn: AccessRule = {
|
|
69
|
-
type: AccessPolicyType.Block,
|
|
70
|
-
};
|
|
71
|
-
const ruleWithAsn: AccessRule = {
|
|
72
|
-
type: AccessPolicyType.Block,
|
|
73
|
-
asn: 205016,
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
expect(makeAccessRuleHash(ruleWithoutAsn)).not.toEqual(
|
|
77
|
-
makeAccessRuleHash(ruleWithAsn),
|
|
78
|
-
);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it("should make same hash for 'undefined' and missing properties", () => {
|
|
82
|
-
const rule1: AccessRule = {
|
|
83
|
-
type: AccessPolicyType.Restrict,
|
|
84
|
-
description: undefined,
|
|
85
|
-
};
|
|
86
|
-
const rule2: AccessRule = {
|
|
87
|
-
type: AccessPolicyType.Restrict,
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
const hash1 = makeAccessRuleHash(rule1);
|
|
91
|
-
const hash2 = makeAccessRuleHash(rule2);
|
|
92
|
-
|
|
93
|
-
expect(hash1).toEqual("755f7123e8f766b6692218b1014b85ca");
|
|
94
|
-
|
|
95
|
-
expect(hash1).toEqual(hash2);
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
describe("transformRule", () => {
|
|
100
|
-
const commonProperties = {
|
|
101
|
-
type: AccessPolicyType.Restrict,
|
|
102
|
-
captchaType: CaptchaType.frictionless,
|
|
103
|
-
description: "test",
|
|
104
|
-
solvedImagesCount: 1,
|
|
105
|
-
imageThreshold: 1,
|
|
106
|
-
powDifficulty: 1,
|
|
107
|
-
unsolvedImagesCount: 1,
|
|
108
|
-
frictionlessScore: 1,
|
|
109
|
-
deferToVerify: false,
|
|
110
|
-
messageKey: ResultReason.TOO_MANY_LOCALHOST,
|
|
111
|
-
headersHash: "headersHash",
|
|
112
|
-
ja4Hash: "js4Hash",
|
|
113
|
-
clientId: "client",
|
|
114
|
-
userId: "user",
|
|
115
|
-
headHash: "headHash",
|
|
116
|
-
coords: "[[1,2]]",
|
|
117
|
-
countryCode: "US",
|
|
118
|
-
asn: 205016,
|
|
119
|
-
os: "macos",
|
|
120
|
-
browser: "firefox",
|
|
121
|
-
headerMatch: "1",
|
|
122
|
-
headerName: "x-test",
|
|
123
|
-
headerValue: "test",
|
|
124
|
-
headerOperator: "equals",
|
|
125
|
-
webBotAuthAgent: "https://signatures.openai.com",
|
|
126
|
-
} satisfies AccessRule;
|
|
127
|
-
|
|
128
|
-
it("should transform access rule record into rule", () => {
|
|
129
|
-
const ruleRecord: Required<AccessRuleRecord> = {
|
|
130
|
-
...commonProperties,
|
|
131
|
-
ruleGroupId: "ruleGroupId",
|
|
132
|
-
ipMask: "127.0.0.0/20",
|
|
133
|
-
ip: "127.0.0.0",
|
|
134
|
-
userAgent: "test",
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
const accessRule = transformAccessRuleRecordIntoRule({
|
|
138
|
-
...ruleRecord,
|
|
139
|
-
unwantedProperty: "bloatware",
|
|
140
|
-
} as unknown as AccessRuleRecord);
|
|
141
|
-
|
|
142
|
-
expect(accessRule).toEqual({
|
|
143
|
-
...commonProperties,
|
|
144
|
-
groupId: "ruleGroupId",
|
|
145
|
-
numericIp: 2130706432n,
|
|
146
|
-
numericIpMaskMax: 2130710527n,
|
|
147
|
-
numericIpMaskMin: 2130706432n,
|
|
148
|
-
userAgentHash:
|
|
149
|
-
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it("should transform access rule into record", () => {
|
|
154
|
-
const accessRule: Required<AccessRule> = {
|
|
155
|
-
...commonProperties,
|
|
156
|
-
groupId: "ruleGroupId",
|
|
157
|
-
numericIp: 2130706432n,
|
|
158
|
-
numericIpMaskMax: 2130710527n,
|
|
159
|
-
numericIpMaskMin: 2130706432n,
|
|
160
|
-
userAgentHash:
|
|
161
|
-
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
const accessRuleRecord = transformAccessRuleIntoRecord({
|
|
165
|
-
...accessRule,
|
|
166
|
-
unwantedProperty: "bloatware",
|
|
167
|
-
} as unknown as AccessRule);
|
|
168
|
-
|
|
169
|
-
expect(accessRuleRecord).toEqual({
|
|
170
|
-
...commonProperties,
|
|
171
|
-
ruleGroupId: "ruleGroupId",
|
|
172
|
-
ipMask: "127.0.0.0/20",
|
|
173
|
-
ip: "127.0.0.0",
|
|
174
|
-
userAgent:
|
|
175
|
-
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
it("should throw error if cannot transform access rule record into rule", () => {
|
|
180
|
-
expect(() =>
|
|
181
|
-
// required "type" property is skipped
|
|
182
|
-
transformAccessRuleRecordIntoRule({
|
|
183
|
-
ip: "127.0.0.1",
|
|
184
|
-
userAgent: "test",
|
|
185
|
-
} as unknown as AccessRuleRecord),
|
|
186
|
-
).toThrow();
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it("should throw error if cannot transform access rule into record", () => {
|
|
190
|
-
expect(() =>
|
|
191
|
-
// required "type" property is skipped
|
|
192
|
-
transformAccessRuleIntoRecord({
|
|
193
|
-
ip: "127.0.0.1",
|
|
194
|
-
userAgent: "test",
|
|
195
|
-
} as unknown as AccessRule),
|
|
196
|
-
).toThrow();
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it("should round-trip an IPv6 address and CIDR mask", () => {
|
|
200
|
-
const ipv6 = "2001:db8::1";
|
|
201
|
-
const ipv6Cidr = "2001:db8::/32";
|
|
202
|
-
|
|
203
|
-
const expectedNumericIp = new Address6(ipv6).bigInt();
|
|
204
|
-
const expectedMaskMin = new Address6(ipv6Cidr).startAddress().bigInt();
|
|
205
|
-
const expectedMaskMax = new Address6(ipv6Cidr).endAddress().bigInt();
|
|
206
|
-
|
|
207
|
-
const ruleRecord: AccessRuleRecord = {
|
|
208
|
-
type: AccessPolicyType.Restrict,
|
|
209
|
-
ip: ipv6,
|
|
210
|
-
ipMask: ipv6Cidr,
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
const accessRule = transformAccessRuleRecordIntoRule(ruleRecord);
|
|
214
|
-
|
|
215
|
-
expect(accessRule.numericIp).toEqual(expectedNumericIp);
|
|
216
|
-
expect(accessRule.numericIpMaskMin).toEqual(expectedMaskMin);
|
|
217
|
-
expect(accessRule.numericIpMaskMax).toEqual(expectedMaskMax);
|
|
218
|
-
|
|
219
|
-
const reconstructed = transformAccessRuleIntoRecord(accessRule);
|
|
220
|
-
|
|
221
|
-
expect(reconstructed.ip).toEqual(new Address6(ipv6).correctForm());
|
|
222
|
-
expect(reconstructed.ipMask).toEqual(ipv6Cidr);
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
describe("getCidrFromNumericIpRange", () => {
|
|
227
|
-
type CirdExample = {
|
|
228
|
-
cidr: string;
|
|
229
|
-
startIp: string;
|
|
230
|
-
endIp: string;
|
|
231
|
-
description: string;
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
const cirdsSet: CirdExample[] = [
|
|
235
|
-
{
|
|
236
|
-
cidr: "127.0.0.0/20",
|
|
237
|
-
startIp: "127.0.0.0",
|
|
238
|
-
endIp: "127.0.15.255",
|
|
239
|
-
description: "/20 network - 4096 addresses",
|
|
240
|
-
},
|
|
241
|
-
{
|
|
242
|
-
cidr: "192.168.1.0/24",
|
|
243
|
-
startIp: "192.168.1.0",
|
|
244
|
-
endIp: "192.168.1.255",
|
|
245
|
-
description: "/24 network - 256 addresses (Class C)",
|
|
246
|
-
},
|
|
247
|
-
{
|
|
248
|
-
cidr: "10.0.0.0/8",
|
|
249
|
-
startIp: "10.0.0.0",
|
|
250
|
-
endIp: "10.255.255.255",
|
|
251
|
-
description: "/8 network - 16M addresses (Class A)",
|
|
252
|
-
},
|
|
253
|
-
{
|
|
254
|
-
cidr: "172.16.0.0/12",
|
|
255
|
-
startIp: "172.16.0.0",
|
|
256
|
-
endIp: "172.31.255.255",
|
|
257
|
-
description: "/12 network - 1M addresses",
|
|
258
|
-
},
|
|
259
|
-
{
|
|
260
|
-
cidr: "192.168.0.0/16",
|
|
261
|
-
startIp: "192.168.0.0",
|
|
262
|
-
endIp: "192.168.255.255",
|
|
263
|
-
description: "/16 network - 65536 addresses (Class B)",
|
|
264
|
-
},
|
|
265
|
-
{
|
|
266
|
-
cidr: "192.168.1.128/25",
|
|
267
|
-
startIp: "192.168.1.128",
|
|
268
|
-
endIp: "192.168.1.255",
|
|
269
|
-
description: "/25 network - 128 addresses (subnet)",
|
|
270
|
-
},
|
|
271
|
-
{
|
|
272
|
-
cidr: "10.20.30.0/28",
|
|
273
|
-
startIp: "10.20.30.0",
|
|
274
|
-
endIp: "10.20.30.15",
|
|
275
|
-
description: "/28 network - 16 addresses (small subnet)",
|
|
276
|
-
},
|
|
277
|
-
{
|
|
278
|
-
cidr: "8.8.8.8/32",
|
|
279
|
-
startIp: "8.8.8.8",
|
|
280
|
-
endIp: "8.8.8.8",
|
|
281
|
-
description: "/32 network - single IP address",
|
|
282
|
-
},
|
|
283
|
-
{
|
|
284
|
-
cidr: "203.0.113.0/30",
|
|
285
|
-
startIp: "203.0.113.0",
|
|
286
|
-
endIp: "203.0.113.3",
|
|
287
|
-
description: "/30 network - 4 addresses (point-to-point)",
|
|
288
|
-
},
|
|
289
|
-
{
|
|
290
|
-
cidr: "198.51.100.64/26",
|
|
291
|
-
startIp: "198.51.100.64",
|
|
292
|
-
endIp: "198.51.100.127",
|
|
293
|
-
description: "/26 network - 64 addresses",
|
|
294
|
-
},
|
|
295
|
-
];
|
|
296
|
-
|
|
297
|
-
it.each(cirdsSet)("should convert $description to $cidr", (cirdExample) => {
|
|
298
|
-
const cird = getCidrFromNumericIpRange(
|
|
299
|
-
new Address4(cirdExample.startIp).bigInt(),
|
|
300
|
-
new Address4(cirdExample.endIp).bigInt(),
|
|
301
|
-
);
|
|
302
|
-
|
|
303
|
-
expect(cird).toEqual(cirdExample.cidr);
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
type Ipv6CidrExample = {
|
|
307
|
-
cidr: string;
|
|
308
|
-
startIp: string;
|
|
309
|
-
endIp: string;
|
|
310
|
-
description: string;
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
const ipv6CirdsSet: Ipv6CidrExample[] = [
|
|
314
|
-
{
|
|
315
|
-
cidr: "2001:db8::/32",
|
|
316
|
-
startIp: "2001:db8::",
|
|
317
|
-
endIp: "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff",
|
|
318
|
-
description: "/32 IPv6 network",
|
|
319
|
-
},
|
|
320
|
-
{
|
|
321
|
-
cidr: "2001:db8::/64",
|
|
322
|
-
startIp: "2001:db8::",
|
|
323
|
-
endIp: "2001:db8::ffff:ffff:ffff:ffff",
|
|
324
|
-
description: "/64 IPv6 subnet",
|
|
325
|
-
},
|
|
326
|
-
{
|
|
327
|
-
cidr: "fe80::/10",
|
|
328
|
-
startIp: "fe80::",
|
|
329
|
-
endIp: "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
|
|
330
|
-
description: "/10 IPv6 link-local",
|
|
331
|
-
},
|
|
332
|
-
];
|
|
333
|
-
|
|
334
|
-
it.each(ipv6CirdsSet)(
|
|
335
|
-
"should convert $description to $cidr",
|
|
336
|
-
(cirdExample) => {
|
|
337
|
-
const cidr = getCidrFromNumericIpRange(
|
|
338
|
-
new Address6(cirdExample.startIp).bigInt(),
|
|
339
|
-
new Address6(cirdExample.endIp).bigInt(),
|
|
340
|
-
);
|
|
341
|
-
|
|
342
|
-
expect(cidr).toEqual(cirdExample.cidr);
|
|
343
|
-
},
|
|
344
|
-
);
|
|
345
|
-
});
|
package/src/transformRule.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import crypto from "node:crypto";
|
|
16
|
-
import { IpAddress, IpRange } from "cidr-calc";
|
|
17
|
-
import { Address4, Address6 } from "ip-address";
|
|
18
|
-
import { z } from "zod";
|
|
19
|
-
import type { AccessRule } from "./rule.js";
|
|
20
|
-
import {
|
|
21
|
-
accessPolicyInputShape,
|
|
22
|
-
policyScopeInput,
|
|
23
|
-
} from "./ruleInput/policyInput.js";
|
|
24
|
-
import { accessRuleInput } from "./ruleInput/ruleInput.js";
|
|
25
|
-
import { userScopeSchema } from "./ruleInput/userScopeInput.js";
|
|
26
|
-
import type { AccessRuleRecord } from "./ruleRecord.js";
|
|
27
|
-
|
|
28
|
-
const RULE_HASH_ALGORITHM = "md5";
|
|
29
|
-
|
|
30
|
-
// IPv4 numeric values occupy 0..2^32-1. Anything above is treated as IPv6.
|
|
31
|
-
const MAX_IPV4_NUMERIC = (1n << 32n) - 1n;
|
|
32
|
-
|
|
33
|
-
export const makeAccessRuleHash = (rule: AccessRule): string => {
|
|
34
|
-
// 1. exclude "undefined" values to ensure hash consistency
|
|
35
|
-
const valueProperties = Object.entries(rule).filter(
|
|
36
|
-
([key, value]) => "undefined" !== typeof value,
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
// 2. order alphabetically to ensure hash consistency
|
|
40
|
-
const orderedProperties = valueProperties.sort();
|
|
41
|
-
|
|
42
|
-
const objectToHash = Object.fromEntries(orderedProperties);
|
|
43
|
-
|
|
44
|
-
return hashObject(objectToHash, RULE_HASH_ALGORITHM);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export const transformAccessRuleRecordIntoRule = (
|
|
48
|
-
ruleRecord: AccessRuleRecord,
|
|
49
|
-
): AccessRule =>
|
|
50
|
-
// accessRuleInput does all the record field transformations
|
|
51
|
-
accessRuleInput.parse(ruleRecord);
|
|
52
|
-
|
|
53
|
-
export const transformAccessRuleIntoRecord = (
|
|
54
|
-
rule: AccessRule,
|
|
55
|
-
): AccessRuleRecord => accessRuleToRecordScheme.parse(rule);
|
|
56
|
-
|
|
57
|
-
const accessRuleToRecordScheme = z
|
|
58
|
-
.object({
|
|
59
|
-
...accessPolicyInputShape.shape,
|
|
60
|
-
...policyScopeInput.shape,
|
|
61
|
-
...userScopeSchema.shape,
|
|
62
|
-
groupId: z.coerce.string().optional(),
|
|
63
|
-
})
|
|
64
|
-
.transform((ruleInput: AccessRule): AccessRuleRecord => {
|
|
65
|
-
// extract groupId
|
|
66
|
-
const {
|
|
67
|
-
groupId,
|
|
68
|
-
numericIp,
|
|
69
|
-
numericIpMaskMin,
|
|
70
|
-
numericIpMaskMax,
|
|
71
|
-
userAgentHash,
|
|
72
|
-
...rule
|
|
73
|
-
} = ruleInput;
|
|
74
|
-
|
|
75
|
-
const record: AccessRuleRecord = rule;
|
|
76
|
-
|
|
77
|
-
if ("string" === typeof groupId) {
|
|
78
|
-
record.ruleGroupId = groupId;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if ("string" === typeof userAgentHash) {
|
|
82
|
-
record.userAgent = userAgentHash;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if ("bigint" === typeof numericIp) {
|
|
86
|
-
record.ip = getStringIpFromNumeric(numericIp);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (
|
|
90
|
-
"bigint" === typeof numericIpMaskMin &&
|
|
91
|
-
"bigint" === typeof numericIpMaskMax
|
|
92
|
-
) {
|
|
93
|
-
record.ipMask = getCidrFromNumericIpRange(
|
|
94
|
-
numericIpMaskMin,
|
|
95
|
-
numericIpMaskMax,
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return record;
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
const hashObject = (
|
|
103
|
-
object: Record<string, unknown>,
|
|
104
|
-
algorithm: string,
|
|
105
|
-
): string =>
|
|
106
|
-
crypto
|
|
107
|
-
.createHash(algorithm)
|
|
108
|
-
.update(
|
|
109
|
-
JSON.stringify(object, (key, value) =>
|
|
110
|
-
// JSON.stringify can't handle BigInt itself: throws "Do not know how to serialize a BigInt"
|
|
111
|
-
"bigint" === typeof value ? value.toString() : value,
|
|
112
|
-
),
|
|
113
|
-
)
|
|
114
|
-
.digest("hex");
|
|
115
|
-
|
|
116
|
-
const getStringIpFromNumeric = (numericIp: bigint): string =>
|
|
117
|
-
numericIp > MAX_IPV4_NUMERIC
|
|
118
|
-
? Address6.fromBigInt(numericIp).correctForm()
|
|
119
|
-
: Address4.fromInteger(Number(numericIp)).address;
|
|
120
|
-
|
|
121
|
-
export const getCidrFromNumericIpRange = (
|
|
122
|
-
startIp: bigint,
|
|
123
|
-
endIp: bigint,
|
|
124
|
-
): string | undefined => {
|
|
125
|
-
const ipRange = new IpRange(
|
|
126
|
-
IpAddress.of(getStringIpFromNumeric(startIp)),
|
|
127
|
-
IpAddress.of(getStringIpFromNumeric(endIp)),
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
const cidr = ipRange.toCidrs()[0];
|
|
131
|
-
|
|
132
|
-
return cidr ? `${cidr.prefix.toString()}/${cidr.prefixLen}` : undefined;
|
|
133
|
-
};
|
package/tsconfig.cjs.json
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.cjs.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"rootDir": "./src",
|
|
5
|
-
"outDir": "./dist/cjs",
|
|
6
|
-
"paths": {
|
|
7
|
-
"#policy/*": ["./src/*"]
|
|
8
|
-
},
|
|
9
|
-
"emitDeclarationOnly": true
|
|
10
|
-
},
|
|
11
|
-
"include": ["./src"],
|
|
12
|
-
"references": [
|
|
13
|
-
{
|
|
14
|
-
"path": "../../dev/config/tsconfig.cjs.json"
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
"path": "../common/tsconfig.cjs.json"
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
"path": "../logger/tsconfig.cjs.json"
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
"path": "../types/tsconfig.cjs.json"
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
"path": "../api-route/tsconfig.cjs.json"
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
"path": "../util/tsconfig.cjs.json"
|
|
30
|
-
},
|
|
31
|
-
{
|
|
32
|
-
"path": "../util-crypto/tsconfig.cjs.json"
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
"path": "../redis-client/tsconfig.cjs.json"
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
"path": "../api/tsconfig.cjs.json"
|
|
39
|
-
}
|
|
40
|
-
]
|
|
41
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.esm.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"rootDir": "./src",
|
|
5
|
-
"outDir": "./dist",
|
|
6
|
-
"paths": {
|
|
7
|
-
"#policy/*": ["./src/*"]
|
|
8
|
-
}
|
|
9
|
-
},
|
|
10
|
-
"include": [
|
|
11
|
-
"./src",
|
|
12
|
-
"src/**/*.ts",
|
|
13
|
-
"src/**/*.tsx",
|
|
14
|
-
"src/**/*.json",
|
|
15
|
-
"src/**/*.d.ts",
|
|
16
|
-
"src/**/.*.ts"
|
|
17
|
-
],
|
|
18
|
-
"references": [
|
|
19
|
-
{
|
|
20
|
-
"path": "../../dev/config/tsconfig.json"
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
"path": "../common"
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
"path": "../logger"
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
"path": "../types"
|
|
30
|
-
},
|
|
31
|
-
{
|
|
32
|
-
"path": "../api-route"
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
"path": "../util"
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
"path": "../util-crypto"
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
"path": "../redis-client"
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
"path": "../api"
|
|
45
|
-
}
|
|
46
|
-
]
|
|
47
|
-
}
|