Skip to content

Commit 26a3fc2

Browse files
committed
Add test case for use case
1 parent 72d82ee commit 26a3fc2

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

library/sources/http-server/ipAllowedToAccessRoute.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,81 @@ t.test(
345345
);
346346
}
347347
);
348+
349+
t.test("allows all IPs for /api/routes/authorize but restricts /api/routes/* to 1.1.1.1", async () => {
350+
const agent = createTestAgent({
351+
token: new Token("123"),
352+
api: new ReportingAPIForTesting({
353+
success: true,
354+
allowedIPAddresses: [],
355+
configUpdatedAt: 0,
356+
blockedUserIds: [],
357+
heartbeatIntervalInMS: 10 * 1000,
358+
endpoints: [
359+
{
360+
route: "/api/routes/*",
361+
// @ts-expect-error Test
362+
rateLimiting: undefined,
363+
method: "GET",
364+
allowedIPAddresses: ["1.1.1.1"],
365+
forceProtectionOff: false,
366+
},
367+
{
368+
route: "/api/routes/authorize",
369+
// @ts-expect-error Test
370+
rateLimiting: undefined,
371+
method: "GET",
372+
allowedIPAddresses: ["0.0.0.0/0", "::/0"],
373+
forceProtectionOff: false,
374+
},
375+
],
376+
block: true,
377+
}),
378+
});
379+
380+
agent.start([]);
381+
await new Promise((resolve) => setTimeout(resolve, 0));
382+
383+
// /api/routes/authorize allowed from any IP
384+
t.same(
385+
ipAllowedToAccessRoute(
386+
{
387+
...context,
388+
url: "/api/routes/authorize",
389+
route: "/api/routes/authorize",
390+
method: "GET",
391+
remoteAddress: "8.8.8.8",
392+
},
393+
agent
394+
),
395+
true
396+
);
397+
398+
// /api/routes/foo only allowed from 1.1.1.1
399+
t.same(
400+
ipAllowedToAccessRoute(
401+
{
402+
...context,
403+
url: "/api/routes/foo",
404+
route: "/api/routes/foo",
405+
method: "GET",
406+
remoteAddress: "1.1.1.1",
407+
},
408+
agent
409+
),
410+
true
411+
);
412+
t.same(
413+
ipAllowedToAccessRoute(
414+
{
415+
...context,
416+
url: "/api/routes/foo",
417+
route: "/api/routes/foo",
418+
method: "GET",
419+
remoteAddress: "8.8.8.8",
420+
},
421+
agent
422+
),
423+
false
424+
);
425+
});

0 commit comments

Comments
 (0)