-
-
Notifications
You must be signed in to change notification settings - Fork 615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(middleware): Introduce IP Restriction Middleware #2813
Conversation
I think it would be good to have |
Hi @EdamAme-x, thank you for comment and PR.
Yes, I agree.
I think that renaming is not needed. And what do you think about allowing to receive Also, your PR includes 2 suggestions about allowing to add |
thanks for reply |
nice suggestion! I like this. How about this? And, I think text of the denied error should be optional. What do you think? (Forbidden) |
@nakasyou I just ready to review |
* feat: if allow is empty, set allow at * by default * fix
Hi @nakasyou WildcardIs “Wildcard” an important feature of this middleware? This is because, although the use of wildcards is seen as customary, I do not believe that such a notation is defined as an official specification. Patterns with trailing If you have a special preference for “Wildcard," you may support It, but otherwise, I think that not supporting it will reduce the amount of code and make future maintenance easier. |
@usualoma |
@EdamAme-x |
@usualoma |
Hi @nakasyou. Nice PR :) How about reconsidering your naming conventions?
I have a few other opinions, I'll leave them later. |
Agreed on this, I think we should go with |
@usualoma, thank you for comment, I removed wildcard as you said. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## next #2813 +/- ##
==========================================
+ Coverage 96.14% 96.15% +0.01%
==========================================
Files 142 144 +2
Lines 14457 14748 +291
Branches 2622 2552 -70
==========================================
+ Hits 13899 14181 +282
- Misses 558 567 +9 ☔ View full report in Codecov by Sentry. |
Hi @ryuapp, thank you for review. Also I'd like to discuss about name of the middleware. |
I don't have other suggestions other than |
Why not just ip-deny-middleware? Those make more sense than an ip-rate-limit. |
Hi @johnforte, thank you for proposing.
It maybe suitable. However naming it |
Hi @nakasyou I think Another optionBy the way, I was thinking, what about turning this middleware into a middleware for "access control" that is not limited to IP addresses? Assuming that the first level of support is restriction by IP address, it also supports authorization by authorization headers and cookies. I envisage something that would also be renamed to "access-control" and could be used as follows. It could be used in production environment to "allow access if either the IP address or another authentication method matches". import { Hono } from 'hono'
import { basic as acBasic, cookie as acCookie, accessControl } from 'hono/access-control'
import { getConnInfo } from 'hono/...'
const app = new Hono()
app.use(
'*',
accessControl(getConnInfo, {
allow: [ // allow if satisfy any of the following
'192.168.0.2', // allow by IP address
acCookie('auth', secretValue), // allow by cookie
acBasic('user', 'password'), // allow by basic auth
],
})
)
app.get('/', (c) => c.text('Hello world!')) Although the functionality overlaps with existing "basic-auth" and "bearer-auth", it may be possible to co-operate reasonably while sharing code. If this option is adopted, control by cookies and request headers can be considered in a separate PR. In this PR, only the name should be changed and remain "middleware that restricts by IP address." |
…rf/prepare-ip-restriction-rule
perf(ip-restriction): optimize ip-restriction middleware by prepare matcher function in advance
Amazing middleware! A small suggestion I would like to add, can we have a custom error handler function in the options? |
@MathurAditya724, thank you. Yes, the middleware has. app.use('*', ipLimit(getConnInfo, {
deny: [],
allow: ['127.0.0.1', '::1']
}, c => {
// error handler
return c.text('Invalid IP address', 403)
})) |
Hi @nakasyou ! Sorry for my late reply. This is awesome middleware! I've left some comments. Check them! |
Great! It looks good to me. The CI is falling, but we can remove the error. Then, I'll merge this into the |
The time has come! Merging into the |
Recreated #2807
I created IP Limit Middleware.
You can limit request by IP Address.
For example, you can limit request, this server accepts local-only requests:
deny
takes precedence overallow
.Rules supported some syntax:
0.0.0.0
::1
192.168.2.1/24
abcd::ef01/64
192.*.2.*
The author should do the following, if applicable
bun denoify
to generate files for Denobun run format:fix && bun run lint:fix
to format the code