Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2fe731

Browse files
apollo13timgraham
authored andcommitted
[1.4.x] Prevented reverse() from generating URLs pointing to other hosts.
This is a security fix. Disclosure following shortly.
1 parent 4d5e972 commit c2fe731

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

django/core/urlresolvers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ def _reverse_with_prefix(self, lookup_view, _prefix, *args, **kwargs):
406406
unicode_kwargs = dict([(k, force_unicode(v)) for (k, v) in kwargs.items()])
407407
candidate = (prefix_norm + result) % unicode_kwargs
408408
if re.search(u'^%s%s' % (_prefix, pattern), candidate, re.UNICODE):
409+
if candidate.startswith('//'):
410+
candidate = '/%%2F%s' % candidate[2:]
409411
return candidate
410412
# lookup_view can be URL label, or dotted path, or callable, Any of
411413
# these can be passed in at the top, but callables are not friendly in

docs/releases/1.4.14.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ Django 1.4.14 release notes
55
*Under development*
66

77
Django 1.4.14 fixes several security issues in 1.4.13.
8+
9+
:func:`~django.core.urlresolvers.reverse()` could generate URLs pointing to other hosts
10+
=======================================================================================
11+
12+
In certain situations, URL reversing could generate scheme-relative URLs (URLs
13+
starting with two slashes), which could unexpectedly redirect a user to a
14+
different host. An attacker could exploit this, for example, by redirecting
15+
users to a phishing site designed to ask for user's passwords.
16+
17+
To remedy this, URL reversing now ensures that no URL starts with two slashes
18+
(//), replacing the second slash with its URL encoded counterpart (%2F). This
19+
approach ensures that semantics stay the same, while making the URL relative to
20+
the domain and not to the scheme.

tests/regressiontests/urlpatterns_reverse/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@
142142
('defaults', '/defaults_view2/3/', [], {'arg1': 3, 'arg2': 2}),
143143
('defaults', NoReverseMatch, [], {'arg1': 3, 'arg2': 3}),
144144
('defaults', NoReverseMatch, [], {'arg2': 1}),
145+
146+
# Security tests
147+
('security', '/%2Fexample.com/security/', ['/example.com'], {}),
145148
)
146149

147150
class NoURLPatternsTests(TestCase):

tests/regressiontests/urlpatterns_reverse/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,7 @@
7171
(r'defaults_view2/(?P<arg1>\d+)/', 'defaults_view', {'arg2': 2}, 'defaults'),
7272

7373
url('^includes/', include(other_patterns)),
74+
75+
# Security tests
76+
url('(.+)/security/$', empty_view, name='security'),
7477
)

0 commit comments

Comments
 (0)