Armen-Jean-Andreasian / fastapi-request-limiter

Revolutionize your FastAPI applications with this middleware!
https://pypi.org/project/fastapi-request-limiter/
BSD 3-Clause "New" or "Revised" License
2 stars 1 forks source link

KeyError in `RequestsArchive.add_ip()` #1

Open johakoch opened 4 months ago

johakoch commented 4 months ago

Hi

Using fastapi-request-limiter 1.0.1, fastapi 0.110.3, Python 3.12.3.

from request_limiter_middleware import RequestLimitMiddleware
...
app = FastAPI()
...
app.add_middleware(RequestLimitMiddleware, seconds=1, whitelisted_ip_addresses=())
...

An initial request (empty history) to the FastAPI raises the following error:

  File "/home/vscode/.local/lib/python3.12/site-packages/request_limiter_middleware/requests_archive.py", line 31, in add_ip
    if type(self.history[ip]) is not bool:  # if the IP is permanently banned
            │    │       └ '172.22.0.5'
            │    └ {}
            └ <request_limiter_middleware.requests_archive.RequestsArchive object at 0x7f8b9f6ed5b0>

KeyError: '172.22.0.5'
Armen-Jean-Andreasian commented 4 months ago

Hi, this error occurs as here uses the direct key access, try using if self.history.get(ip) not True instead. I'd appreciate it if you implement it and open a pull request. I don't have time to support the package.

On Wed, Jun 19, 2024 at 7:38 PM Johannes Koch @.***> wrote:

Hi

Using fastapi-request-limiter 1.0.1, fastapi 0.110.3, Python 3.12.3.

from request_limiter_middleware import RequestLimitMiddleware ...app = FastAPI() ...app.add_middleware(RequestLimitMiddleware, seconds=1, whitelisted_ip_addresses=()) ...

An initial request (empty history) to the FastAPI raises the following error:

File "/home/vscode/.local/lib/python3.12/site-packages/request_limiter_middleware/requests_archive.py", line 31, in add_ip if type(self.history[ip]) is not bool: # if the IP is permanently banned │ │ └ '172.22.0.5' │ └ {} └ <request_limiter_middleware.requests_archive.RequestsArchive object at 0x7f8b9f6ed5b0>

KeyError: '172.22.0.5'

— Reply to this email directly, view it on GitHub https://github.com/Armen-Jean-Andreasian/fastapi-request-limiter/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFN3MU7VE5N62JMZK35LK5TZIF3TNAVCNFSM6AAAAABJR7L2DCVHI2DSMVQWIX3LMV43ASLTON2WKOZSGM3DEMRYGA2DOMY . You are receiving this because you are subscribed to this thread.Message ID: @.***>