Skip to content

Commit

Permalink
Merge pull request #4343 from pmac/fix-redirect-unicode-error
Browse files Browse the repository at this point in the history
Fix unicode error in redirects
  • Loading branch information
jgmize committed Sep 14, 2016
2 parents 2fb16c0 + 5e60788 commit fa2ad34
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 19 additions & 0 deletions bedrock/redirects/tests/test_util.py
Expand Up @@ -338,3 +338,22 @@ def test_match_flags(self):
resp = middleware.process_request(self.rf.get('/iam/the/walrus/'))
eq_(resp.status_code, 301)
eq_(resp['Location'], '/coo/coo/cachoo/')

def test_non_ascii_strip_tags(self):
"""
Should deal with non-ascii characters when there's a substitution as well as strip tags.
This is from errors that happened in prod. The following URL caused a 500:
https://www.mozilla.org/editor/midasdemo/securityprefs.html%3C/span%3E%3C/a%3E%C2%A0
https://sentry.prod.mozaws.net/marketing/bedrock-prod-eu-west/issues/349078/
"""
resolver = get_resolver([redirect(r'^editor/(?P<page>.*)$',
'http://www-archive.mozilla.org/editor/{page}')])
middleware = RedirectsMiddleware(resolver)
resp = middleware.process_request(self.rf.get('/editor/midasdemo/securityprefs.html'
'%3C/span%3E%3C/a%3E%C2%A0'))
eq_(resp.status_code, 301)
eq_(resp['Location'],
'http://www-archive.mozilla.org/editor/midasdemo/securityprefs.html%C2%A0')
4 changes: 3 additions & 1 deletion bedrock/redirects/util.py
Expand Up @@ -9,6 +9,8 @@
from django.core.urlresolvers import NoReverseMatch, RegexURLResolver, reverse
from django.conf.urls import url
from django.http import HttpResponsePermanentRedirect, HttpResponseRedirect, HttpResponseGone
from django.utils.encoding import force_text
from django.utils.html import strip_tags
from django.views.decorators.vary import vary_on_headers

import commonware.log
Expand Down Expand Up @@ -178,7 +180,7 @@ def _view(request, *args, **kwargs):

# use info from url captures.
if args or kwargs:
redirect_url = redirect_url.format(*args, **kwargs)
redirect_url = strip_tags(force_text(redirect_url).format(*args, **kwargs))

if query:
if merge_query:
Expand Down

0 comments on commit fa2ad34

Please sign in to comment.