Skip to content

Commit fd233f4

Browse files
committed
Fixed #12070. Fixed a case where var._whatever wasn't raising a TemplateSyntaxError.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12539 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 7352238 commit fd233f4

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

django/template/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,6 @@ def __init__(self, token, parser):
538538
var_obj = None
539539
elif var is None:
540540
raise TemplateSyntaxError("Could not find variable at start of %s." % token)
541-
elif var.find(VARIABLE_ATTRIBUTE_SEPARATOR + '_') > -1 or var[0] == '_':
542-
raise TemplateSyntaxError("Variables and attributes may not begin with underscores: '%s'" % var)
543541
else:
544542
var_obj = Variable(var)
545543
else:
@@ -698,6 +696,8 @@ def __init__(self, var):
698696
except ValueError:
699697
# Otherwise we'll set self.lookups so that resolve() knows we're
700698
# dealing with a bonafide variable
699+
if var.find(VARIABLE_ATTRIBUTE_SEPARATOR + '_') > -1 or var[0] == '_':
700+
raise TemplateSyntaxError("Variables and attributes may not begin with underscores: '%s'" % var)
701701
self.lookups = tuple(var.split(VARIABLE_ATTRIBUTE_SEPARATOR))
702702

703703
def resolve(self, context):

tests/regressiontests/templates/parser.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@
7676
[]
7777
>>> fe.var
7878
u'Some "Good" News'
79+
80+
Filtered variables should reject access of attributes beginning with underscores.
81+
82+
>>> FilterExpression('article._hidden|upper', p)
83+
Traceback (most recent call last):
84+
...
85+
TemplateSyntaxError: Variables and attributes may not begin with underscores: 'article._hidden'
7986
"""
8087

8188
variable_parsing = r"""
@@ -105,4 +112,10 @@
105112
>>> Variable(ur"'Some \'Better\' News'").resolve(c)
106113
u"Some 'Better' News"
107114
115+
Variables should reject access of attributes beginning with underscores.
116+
117+
>>> Variable('article._hidden')
118+
Traceback (most recent call last):
119+
...
120+
TemplateSyntaxError: Variables and attributes may not begin with underscores: 'article._hidden'
108121
"""

0 commit comments

Comments
 (0)