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

Skip to content

Commit 2da4ace

Browse files
committed
[1.3.X] Fixed a security issue in get_host.
Full disclosure and new release forthcoming.
1 parent 1515eb4 commit 2da4ace

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

django/http/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ def __init__(self, *args, **kwargs):
129129
RESERVED_CHARS="!*'();:@&=+$,/?%#[]"
130130

131131
absolute_http_url_re = re.compile(r"^https?://", re.I)
132+
host_validation_re = re.compile(r"^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9:]+\])(:\d+)?$")
133+
132134

133135
class Http404(Exception):
134136
pass
@@ -167,7 +169,7 @@ def get_host(self):
167169
host = '%s:%s' % (host, server_port)
168170

169171
# Disallow potentially poisoned hostnames.
170-
if set(';/?@&=+$,').intersection(host):
172+
if not host_validation_re.match(host.lower()):
171173
raise SuspiciousOperation('Invalid HTTP_HOST header: %s' % host)
172174

173175
return host

tests/regressiontests/requests/tests.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
import time
23
from datetime import datetime, timedelta
34
from StringIO import StringIO
@@ -110,13 +111,15 @@ def test_http_get_host(self):
110111
'12.34.56.78:443',
111112
'[2001:19f0:feee::dead:beef:cafe]',
112113
'[2001:19f0:feee::dead:beef:cafe]:8080',
114+
'xn--4ca9at.com', # Punnycode for öäü.com
113115
]
114116

115117
poisoned_hosts = [
116118
117119
'example.com:[email protected]',
118-
'example.com:[email protected]:80',
119-
'example.com:80/badpath'
120+
'example.com:[email protected]:80',
121+
'example.com:80/badpath',
122+
'example.com: recovermypassword.com',
120123
]
121124

122125
for host in legit_hosts:
@@ -187,13 +190,15 @@ def test_http_get_host_with_x_forwarded_host(self):
187190
'12.34.56.78:443',
188191
'[2001:19f0:feee::dead:beef:cafe]',
189192
'[2001:19f0:feee::dead:beef:cafe]:8080',
193+
'xn--4ca9at.com', # Punnycode for öäü.com
190194
]
191195

192196
poisoned_hosts = [
193197
194198
'example.com:[email protected]',
195199
'example.com:[email protected]:80',
196-
'example.com:80/badpath'
200+
'example.com:80/badpath',
201+
'example.com: recovermypassword.com',
197202
]
198203

199204
for host in legit_hosts:

0 commit comments

Comments
 (0)