Code Monkey home page Code Monkey logo

django-basic-auth-ip-whitelist's Issues

Perhaps this needs to be improved, or at least documented

https://github.com/tm-kn/django-basic-auth-ip-whitelist/blob/318006b15b598c035cf52031c8fa63dbf241440f/baipw/utils.py#L23

This is the situation:

  • this default get_client_ip is called by self._get_client_ip()
  • which takes whatever this returns and passes it to ipaddress.ip_address()
  • which doesn't accept None as an input
  • so if this code path is hit, ipaddress.ip_address() will exception out
  • but baipw doesn't handle this exception, so it just escalates up, and the whole app just ends up 500-ing.

So this code path is problematic, if there isn't a saner return, then at least maybe warn others that if this code branch is hit, the app will blow up.

ValueError in Azure App Service

πŸ‘‹ Trying to run this on a Wagtail project in Azure App Service (containerised), I’m getting the following exception:

ValueError: '147.243.178.14:13250' does not appear to be an IPv4 or IPv6 address

Full stack trace:

[…]
  File "/usr/local/lib/python3.8/site-packages/baipw/middleware.py", line 18, in __call__
    response = self.process_request(request)
  File "/usr/local/lib/python3.8/site-packages/baipw/middleware.py", line 35, in process_request
    if self._is_ip_whitelisted(request):
  File "/usr/local/lib/python3.8/site-packages/baipw/middleware.py", line 128, in _is_ip_whitelisted
    ip_address = ipaddress.ip_address(self._get_client_ip(request))
  File "/usr/local/lib/python3.8/ipaddress.py", line 53, in ip_address
    raise ValueError('%r does not appear to be an IPv4 or IPv6 address' %
ValueError: '147.243.178.14:13250' does not appear to be an IPv4 or IPv6 address

This looks like it would be due to the port. It’s not too clear to me which HTTP header might contain it though. Here is my hacky workaround to remove the port:

Edit: removed as my workaround was only working locally when manually setting a few specific headers.

One-liner to try this out:

curl -I --header 'X-Forwarded-For: 147.243.178.14:13250' http://localhost:8000/

Plan out new ways of getting an IP

  • Make it more generic.
    • Third party libraries?
    • Pluggable backends?
    • Configurable rules for different providers?
  • Make user's IP address won't crash the server.

Related: #7, #4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.