Skip to content

Commit 0c11c08

Browse files
committed
Do not merge allowed ip addresses
1 parent 38a7ae1 commit 0c11c08

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

library/agent/Agent.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,6 @@ t.test("it only allows some IP addresses", async () => {
11451145
});
11461146

11471147
t.same(agent.getConfig().shouldOnlyAllowSomeIPAddresses(), true);
1148-
t.same(agent.getConfig().isOnlyAllowedIPAddress("1.2.3.4"), false);
1149-
t.same(agent.getConfig().isOnlyAllowedIPAddress("4.3.2.1"), true);
1148+
t.same(agent.getConfig().isOnlyAllowedIPAddress("1.2.3.4").allowed, false);
1149+
t.same(agent.getConfig().isOnlyAllowedIPAddress("4.3.2.1").allowed, true);
11501150
});

library/agent/ServiceConfig.test.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,32 @@ t.test("restricting access to some ips", async () => {
174174

175175
t.same(config.shouldOnlyAllowSomeIPAddresses(), true);
176176

177-
t.same(config.isOnlyAllowedIPAddress("1.2.3.4"), true);
178-
t.same(config.isOnlyAllowedIPAddress("4.3.2.1"), false);
177+
t.same(config.isOnlyAllowedIPAddress("1.2.3.4").allowed, true);
178+
t.same(config.isOnlyAllowedIPAddress("4.3.2.1").allowed, false);
179179

180180
config.updateOnlyAllowedIPAddresses([]);
181-
t.same(config.isOnlyAllowedIPAddress("1.2.3.4"), false);
181+
t.same(config.isOnlyAllowedIPAddress("1.2.3.4").allowed, false);
182+
});
183+
184+
t.test("only allow some ips: empty list", async () => {
185+
const config = new ServiceConfig(
186+
[],
187+
0,
188+
[],
189+
[],
190+
true,
191+
[],
192+
[
193+
{
194+
source: "geoip",
195+
description: "description",
196+
ips: [],
197+
},
198+
]
199+
);
200+
201+
t.same(config.shouldOnlyAllowSomeIPAddresses(), false);
202+
203+
t.same(config.isOnlyAllowedIPAddress("1.2.3.4").allowed, false);
204+
t.same(config.isOnlyAllowedIPAddress("4.3.2.1").allowed, false);
182205
});

library/agent/ServiceConfig.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ export class ServiceConfig {
1111
private blockedIPAddresses: { blocklist: IPMatcher; description: string }[] =
1212
[];
1313
private blockedUserAgentRegex: RegExp | undefined;
14-
private onlyAllowedIPAddresses: IPMatcher | undefined;
14+
private onlyAllowedIPAddresses: {
15+
allowlist: IPMatcher;
16+
description: string;
17+
}[] = [];
1518

1619
constructor(
1720
endpoints: Endpoint[],
@@ -127,15 +130,18 @@ export class ServiceConfig {
127130
}
128131

129132
private setOnlyAllowedIPAddresses(ipAddresses: IPList[]) {
130-
this.onlyAllowedIPAddresses = undefined;
131-
132-
if (ipAddresses.length === 0) {
133-
return;
133+
this.onlyAllowedIPAddresses = [];
134+
135+
for (const source of ipAddresses) {
136+
// Skip empty allowlists
137+
if (source.ips.length === 0) {
138+
continue;
139+
}
140+
this.onlyAllowedIPAddresses.push({
141+
allowlist: new IPMatcher(source.ips),
142+
description: source.description,
143+
});
134144
}
135-
136-
const ips = ipAddresses.map((source) => source.ips).flat();
137-
138-
this.onlyAllowedIPAddresses = new IPMatcher(ips);
139145
}
140146

141147
updateOnlyAllowedIPAddresses(ipAddresses: IPList[]) {
@@ -146,13 +152,15 @@ export class ServiceConfig {
146152
* Returns true if only some IP addresses are allowed to access the service, e.g. if a geoip country allowlist is set.
147153
*/
148154
shouldOnlyAllowSomeIPAddresses() {
149-
return this.onlyAllowedIPAddresses !== undefined;
155+
return this.onlyAllowedIPAddresses.length > 0;
150156
}
151157

152-
isOnlyAllowedIPAddress(ip: string) {
153-
return this.onlyAllowedIPAddresses
154-
? this.onlyAllowedIPAddresses.has(ip)
155-
: false;
158+
isOnlyAllowedIPAddress(ip: string): { allowed: boolean } {
159+
const allowlist = this.onlyAllowedIPAddresses.find((list) =>
160+
list.allowlist.has(ip)
161+
);
162+
163+
return { allowed: !!allowlist };
156164
}
157165

158166
updateConfig(

library/sources/http-server/checkIfRequestIsBlocked.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function checkIfRequestIsBlocked(
5353
context.remoteAddress &&
5454
agent.getConfig().shouldOnlyAllowSomeIPAddresses() &&
5555
!isPrivateIP(context.remoteAddress) &&
56-
!agent.getConfig().isOnlyAllowedIPAddress(context.remoteAddress)
56+
!agent.getConfig().isOnlyAllowedIPAddress(context.remoteAddress).allowed
5757
) {
5858
res.statusCode = 403;
5959
res.setHeader("Content-Type", "text/plain");

0 commit comments

Comments
 (0)