diff --git a/README.rst b/README.rst index a0c391f..1c16267 100644 --- a/README.rst +++ b/README.rst @@ -166,13 +166,15 @@ locked. For example:: LOCKDOWN_REMOTE_ADDR_EXCEPTIONS ------------------------------- -An optional list addresses to be matched agains incomming - `requests.META['REMOTE_ADDR']`. If IP is in this list, it will not be locked. For example:: - LOCKDOWN_REMOTE_ADDR = ( - '192.168.0.1', +An optional list of IP-addresses to be matched against the requesting +IP-address (from `requests.META['REMOTE_ADDR']`). If the requesting IP-address +is in this list, it will not be locked. For example:: + + LOCKDOWN_REMOTE_ADDR_EXCEPTIONS = [ '127.0.0.1', - ) + '::1', + ] LOCKDOWN_UNTIL -------------- diff --git a/lockdown/middleware.py b/lockdown/middleware.py index 748ff9a..314c21c 100644 --- a/lockdown/middleware.py +++ b/lockdown/middleware.py @@ -82,15 +82,16 @@ def process_request(self, request): if settings.ENABLED is False: return None - # Don't lock down if the client REMOTE_ADDR matched and exception + # Don't lock down if the client REMOTE_ADDR matched and is part of the + # exception list. if self.remote_addr_exceptions: remote_addr_exceptions = self.remote_addr_exceptions else: remote_addr_exceptions = settings.REMOTE_ADDR_EXCEPTIONS - if remote_addr_exceptions: - if request.META.get('REMOTE_ADDR') in remote_addr_exceptions: - return None + if remote_addr_exceptions and \ + request.META.get('REMOTE_ADDR') in remote_addr_exceptions: + return None # Don't lock down if the URL matches an exception pattern. if self.url_exceptions: diff --git a/lockdown/tests/tests.py b/lockdown/tests/tests.py index edb99d7..3785b2d 100644 --- a/lockdown/tests/tests.py +++ b/lockdown/tests/tests.py @@ -47,7 +47,7 @@ def test_global_disable(self): self.assertEqual(response.content, self.locked_contents) @patch('lockdown.tests.tests.middleware.settings.URL_EXCEPTIONS', - [r'/view/$']) + (r'/view/$',)) def test_url_exceptions(self): """Test that a page isn't locked when its URL is in the exception list. @@ -65,7 +65,8 @@ def test_remote_addr_exception_lock(self): The excepted IP are determined by the LOCKDOWN_REMOTE_ADDR_EXCEPTIONS setting """ - response = self.client.get(self.locked_url, REMOTE_ADDR='192.168.0.100') + response = self.client.get(self.locked_url, + REMOTE_ADDR='192.168.0.100') self.assertNotEqual(response.content, self.locked_contents) @patch('lockdown.tests.tests.middleware.settings.REMOTE_ADDR_EXCEPTIONS', diff --git a/lockdown/tests/views.py b/lockdown/tests/views.py index a9c987b..a71caeb 100644 --- a/lockdown/tests/views.py +++ b/lockdown/tests/views.py @@ -32,8 +32,9 @@ def locked_view_with_exception(request): return HttpResponse('A locked view.') -@lockdown(remote_addr_exceptions=('192.168.0.1',)) +@lockdown(remote_addr_exceptions=['192.168.0.1']) def locked_view_with_ip_exception(request): + """View, locked except for the configured IP-address.""" return HttpResponse('A locked view.')