Skip to content

Commit 40cbff9

Browse files
committed
proxy to be able to deny request to invalid hostnames
Change-Id: I974f729da60e5ab9453daf9e52466b3e1af5c69b
1 parent a77cbc2 commit 40cbff9

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Diff for: etc/proxy-server.conf-sample

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ use = egg:swift#proxy
5757
# This is a comma separated list of account hashes that ignore the
5858
# max_containers_per_account cap.
5959
# max_containers_whitelist =
60+
# comma separated list of Host headers the proxy will be deny requests to
61+
# deny_host_headers =
6062

6163
[filter:tempauth]
6264
use = egg:swift#tempauth

Diff for: swift/proxy/server.py

+5
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,8 @@ def __init__(self, conf, memcache=None, logger=None, account_ring=None,
18411841
self.max_containers_whitelist = [a.strip()
18421842
for a in conf.get('max_containers_whitelist', '').split(',')
18431843
if a.strip()]
1844+
self.deny_host_headers = [host.strip() for host in
1845+
conf.get('deny_host_headers', '').split(',') if host.strip()]
18441846

18451847
def get_controller(self, path):
18461848
"""
@@ -1925,6 +1927,9 @@ def handle_request(self, req):
19251927
return HTTPPreconditionFailed(request=req, body='Invalid UTF8')
19261928
if not controller:
19271929
return HTTPPreconditionFailed(request=req, body='Bad URL')
1930+
if self.deny_host_headers and \
1931+
req.host.split(':')[0] in self.deny_host_headers:
1932+
return HTTPForbidden(request=req, body='Invalid host header')
19281933

19291934
controller = controller(self, **path_parts)
19301935
if 'swift.trans_id' not in req.environ:

Diff for: test/unit/proxy/test_server.py

+13
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,19 @@ def test_negative_content_length(self):
741741
finally:
742742
rmtree(swift_dir, ignore_errors=True)
743743

744+
def test_denied_host_header(self):
745+
swift_dir = mkdtemp()
746+
try:
747+
baseapp = proxy_server.BaseApplication({'swift_dir': swift_dir,
748+
'deny_host_headers': 'invalid_host.com'},
749+
FakeMemcache(), NullLoggingHandler(), FakeRing(), FakeRing(),
750+
FakeRing())
751+
resp = baseapp.handle_request(
752+
Request.blank('/v1/a/c/o',
753+
environ={'HTTP_HOST': 'invalid_host.com'}))
754+
self.assertEquals(resp.status, '403 Forbidden')
755+
finally:
756+
rmtree(swift_dir, ignore_errors=True)
744757

745758
class TestObjectController(unittest.TestCase):
746759

0 commit comments

Comments
 (0)