Skip to content

Commit b97b9fb

Browse files
committed
Fixed django#15493 - csrf_migration_helper.py parsing fix.
Thanks to 'john' for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15647 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent c250b88 commit b97b9fb

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

extras/csrf_migration_helper.py

100644100755
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@
4141
# loaders are out of the picture, because there is no way to ask them to
4242
# return all templates.
4343
#
44-
# - If you put the {% csrf_token %} tag on the same line as the <form> tag it
45-
# will be detected, otherwise it will be assumed that the form does not have
46-
# the token.
47-
#
4844
# - It's impossible to programmatically determine which forms should and should
4945
# not have the token added. The developer must decide when to do this,
5046
# ensuring that the token is only added to internally targetted forms.
@@ -138,6 +134,7 @@
138134

139135
_POST_FORM_RE = \
140136
re.compile(r'(<form\W[^>]*\bmethod\s*=\s*(\'|"|)POST(\'|"|)\b[^>]*>)', re.IGNORECASE)
137+
_FORM_CLOSE_RE = re.compile(r'</form\s*>')
141138
_TOKEN_RE = re.compile('\{% csrf_token')
142139

143140
def get_template_dirs():
@@ -190,12 +187,22 @@ def post_form_info(self):
190187
Get information about any POST forms in the template.
191188
Returns [(linenumber, csrf_token added)]
192189
"""
193-
matches = []
190+
forms = {}
191+
form_line = 0
194192
for ln, line in enumerate(self.content.split("\n")):
195-
m = _POST_FORM_RE.search(line)
196-
if m is not None:
197-
matches.append((ln + 1, _TOKEN_RE.search(line) is not None))
198-
return matches
193+
if not form_line and _POST_FORM_RE.search(line):
194+
# record the form with no CSRF token yet
195+
form_line = ln + 1
196+
forms[form_line] = False
197+
if form_line and _TOKEN_RE.search(line):
198+
# found the CSRF token
199+
forms[form_line] = True
200+
form_line = 0
201+
if form_line and _FORM_CLOSE_RE.search(line):
202+
# no token found by form closing tag
203+
form_line = 0
204+
205+
return forms.items()
199206

200207
def includes_template(self, t):
201208
"""

0 commit comments

Comments
 (0)