Skip to content

Commit

Permalink
[1.4.x] Fixed #18979 -- Avoid endless loop caused by "val in PermLook…
Browse files Browse the repository at this point in the history
…upDict"

Fixed by defining __iter__ which raises TypeError. This was done to
PermWrapper earlier.

Backport of 50d573d
  • Loading branch information
akaariai committed Sep 27, 2012
1 parent bd514f2 commit 1f53733
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions django/contrib/auth/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def __repr__(self):
def __getitem__(self, perm_name):
return self.user.has_perm("%s.%s" % (self.module_name, perm_name))

def __iter__(self):
# To fix 'item in perms.someapp' and __getitem__ iteraction we need to
# define __iter__. See #18979 for details.
raise TypeError("PermLookupDict is not iterable.")

def __nonzero__(self):
return self.user.has_module_perms(self.module_name)

Expand Down
44 changes: 44 additions & 0 deletions django/contrib/auth/tests/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,56 @@

from django.conf import global_settings
from django.contrib.auth import authenticate
from django.contrib.auth.context_processors import PermWrapper, PermLookupDict
from django.db.models import Q
from django.template import context
from django.test import TestCase
from django.test.utils import override_settings


class MockUser(object):
def has_module_perm(self, perm):
if perm == 'mockapp.someapp':
return True
return False

def has_perm(self, perm):
if perm == 'someperm':
return True
return False


class PermWrapperTests(TestCase):
"""
Test some details of the PermWrapper implementation.
"""
class EQLimiterObject(object):
"""
This object makes sure __eq__ will not be called endlessly.
"""
def __init__(self):
self.eq_calls = 0

def __eq__(self, other):
if self.eq_calls > 0:
return True
self.eq_calls += 1
return False

def test_permwrapper_in(self):
"""
Test that 'something' in PermWrapper doesn't end up in endless loop.
"""
perms = PermWrapper(MockUser())
with self.assertRaises(TypeError):
self.EQLimiterObject() in perms

def test_permlookupdict_in(self):
pldict = PermLookupDict(MockUser(), 'mockapp')
with self.assertRaises(TypeError):
self.EQLimiterObject() in pldict


class AuthContextProcessorTests(TestCase):
"""
Tests for the ``django.contrib.auth.context_processors.auth`` processor
Expand Down

0 comments on commit 1f53733

Please sign in to comment.