Skip to content

Commit

Permalink
only trust last x-forwarded-proto entry
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk committed Nov 10, 2017
1 parent 8d96557 commit 80959e4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions tornado/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ def _apply_xheaders(self, headers):
"X-Scheme", headers.get("X-Forwarded-Proto",
self.protocol))
if proto_header:
# use the outermost proto entry if there is more than one
proto_header = proto_header.split(',')[0].strip()
# use only the last proto entry if there is more than one
# TODO: support trusting mutiple layers of proxied protocol
proto_header = proto_header.split(',')[-1].strip()
if proto_header in ("http", "https"):
self.protocol = proto_header

Expand Down
4 changes: 2 additions & 2 deletions tornado/test/httpserver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,12 @@ def test_scheme_headers(self):
https_multi_forwarded = {"X-Forwarded-Proto": "https , http"}
self.assertEqual(
self.fetch_json("/", headers=https_multi_forwarded)["remote_protocol"],
"https")
"http")

http_multi_forwarded = {"X-Forwarded-Proto": "http,https"}
self.assertEqual(
self.fetch_json("/", headers=http_multi_forwarded)["remote_protocol"],
"http")
"https")

bad_forwarded = {"X-Forwarded-Proto": "unknown"}
self.assertEqual(
Expand Down

0 comments on commit 80959e4

Please sign in to comment.