Skip to content

Commit 0033b0f

Browse files
committed
Check route level access before we check if IP is in bypass list
1 parent 38e9423 commit 0033b0f

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

end2end/tests/hono-xml-blocklists.test.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ t.beforeEach(async () => {
2121
Authorization: token,
2222
},
2323
body: JSON.stringify({
24-
allowedIPAddresses: ["1.3.2.1"],
24+
allowedIPAddresses: ["1.3.2.1", "1.3.2.2"],
25+
endpoints: [
26+
{
27+
route: "/admin",
28+
method: "GET",
29+
forceProtectionOff: false,
30+
allowedIPAddresses: ["1.3.2.1"],
31+
rateLimiting: {
32+
enabled: false,
33+
},
34+
},
35+
],
2536
}),
2637
});
2738
t.same(config.status, 200);
@@ -244,6 +255,24 @@ t.test("it does not block bypass IP if in blocklist", (t) => {
244255
signal: AbortSignal.timeout(5000),
245256
});
246257
t.same(resp1.status, 200);
258+
259+
const resp2 = await fetch("http://127.0.0.1:4004/admin", {
260+
headers: {
261+
"X-Forwarded-For": "1.3.2.1",
262+
},
263+
});
264+
t.same(resp2.status, 200);
265+
266+
const resp3 = await fetch("http://127.0.0.1:4004/admin", {
267+
headers: {
268+
"X-Forwarded-For": "1.3.2.2",
269+
},
270+
});
271+
t.same(resp3.status, 403);
272+
t.same(
273+
await resp3.text(),
274+
`Your IP address is not allowed to access this resource. (Your IP: 1.3.2.2)`
275+
);
247276
})
248277
.catch((error) => {
249278
t.fail(error);

library/sources/http-server/checkIfRequestIsBlocked.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ export function checkIfRequestIsBlocked(
2626
return false;
2727
}
2828

29+
if (!ipAllowedToAccessRoute(context, agent)) {
30+
res.statusCode = 403;
31+
res.setHeader("Content-Type", "text/plain");
32+
33+
let message = "Your IP address is not allowed to access this resource.";
34+
if (context.remoteAddress) {
35+
message += ` (Your IP: ${escapeHTML(context.remoteAddress)})`;
36+
}
37+
38+
res.end(message);
39+
40+
return true;
41+
}
42+
2943
const isAllowedIP =
3044
context.remoteAddress &&
3145
agent.getConfig().isAllowedIP(context.remoteAddress);
@@ -52,20 +66,6 @@ export function checkIfRequestIsBlocked(
5266
return true;
5367
}
5468

55-
if (!ipAllowedToAccessRoute(context, agent)) {
56-
res.statusCode = 403;
57-
res.setHeader("Content-Type", "text/plain");
58-
59-
let message = "Your IP address is not allowed to access this resource.";
60-
if (context.remoteAddress) {
61-
message += ` (Your IP: ${escapeHTML(context.remoteAddress)})`;
62-
}
63-
64-
res.end(message);
65-
66-
return true;
67-
}
68-
6969
const isUserAgentBlocked =
7070
context.headers && typeof context.headers["user-agent"] === "string"
7171
? agent.getConfig().isUserAgentBlocked(context.headers["user-agent"])

sample-apps/hono-xml/app.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ async function main() {
100100
return c.json({ success: true });
101101
});
102102

103+
app.get("/admin", async (c) => {
104+
return c.html(
105+
`<html lang="en">
106+
<body>
107+
<h1>Admin panel</h1>
108+
</body>
109+
</html>`
110+
);
111+
});
112+
103113
app.post("/add-fast", async (c) => {
104114
const body = await c.req.text();
105115

0 commit comments

Comments
 (0)