Open
Conversation
mdipierro
reviewed
Jan 16, 2024
| if blocked_ip['count']< BLCOKED_IP_MAX_COUNT: | ||
| blocked_ip['count']+=1 | ||
| with open(FILE_BLCOKED_IP_COUNT, "w") as fp: | ||
| json.dump(host_ip_blocked, fp) |
Contributor
There was a problem hiding this comment.
unfortunately this is not thread safe.
| BLCOKED_IP_MAX_COUNT=5 | ||
| # timeout for blocked IP: if BLOCKED_IP_TIMEOUT=0 blocking never expired | ||
| BLOCKED_IP_TIMEOUT=5*60 # reenable blocked ip after this time in seconds | ||
| FILE_BLCOKED_IP_COUNT="host_ip_blocked.txt" |
Contributor
There was a problem hiding this comment.
Look into core.py for DatabaseErrorLogger. It creates a global database .service/service.storage. Perhaps make auth create a new "web2py_managed_ips" in there with fields "ip, allowed, blocked, counter" and use those.
| try: | ||
| with open(FILE_HOST_IP_ALLOW, "r") as fp: | ||
| host_ip_allow = json.load(fp) | ||
| except: |
Contributor
There was a problem hiding this comment.
pylint will not like this
| client_ip=request.remote_addr | ||
| is_this_ip_allowd=True | ||
| if host_ip_blocked: | ||
| blocked_ip =host_ip_blocked[client_ip] |
Contributor
There was a problem hiding this comment.
This is nice. Even if reading from DB make sure all blocked ips are read at statup to minimize disk IO at every request.
Contributor
|
I have not forgotten about this. On my TODO list. :-) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR contains the following additions to the login process (all implemented in the py4web/utils/auth.py):
Notes:
In addition to these restrictions, in case of a failed login (because of wrong credential ), the client IP will be registered in a file name: host_ip_blocked.txt.
After a predefined number of failures, the client IP will be blocked for predefined number of seconds. Which means that even a a login with correct credentials will be denied until the timeout period is over.
A successful login after the timeout period will cause a removal of the ip from the host_ip_blocked.txt.
Issues:
The path to the host_ip_allow.txt and the host_ip_deny.txt files are not specified in the PR.
Since the host_ip_blocked.txt file is created by the framework and not by the user, the path happened to be '/' on an ubuntu installation with the machine-setup.sh script from the py4web\deployment_tools\ubuntu