Skip to content

Commit

Permalink
Don't throw away port number when parsing the Forwarded header aio-li…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Carneiro committed May 18, 2018
1 parent e57ca49 commit 693423d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/3009.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When parsing the Forwarded header, the optional port number is now preserved.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Georges Dubus
Greg Holt
Gregory Haynes
Günther Jena
Gustavo Carneiro
Hu Bo
Hugo Herter
Hynek Schlawack
Expand Down
6 changes: 4 additions & 2 deletions aiohttp/web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FileField:
qdtext=_QDTEXT, quoted_pair=_QUOTED_PAIR)

_FORWARDED_PAIR = (
r'({token})=({token}|{quoted_string})'.format(
r'({token})=({token}|{quoted_string})(:\d{{1,4}})?'.format(
token=_TOKEN,
quoted_string=_QUOTED_STRING))

Expand Down Expand Up @@ -247,11 +247,13 @@ def forwarded(self):
# bad syntax here, skip to next comma
pos = field_value.find(',', pos)
else:
(name, value) = match.groups()
name, value, port = match.groups()
if value[0] == '"':
# quoted string: remove quotes and unescape
value = _QUOTED_PAIR_REPLACE_RE.sub(r'\1',
value[1:-1])
if port:
value += port
elem[name.lower()] = value
pos += len(match.group(0))
need_separator = True
Expand Down
15 changes: 15 additions & 0 deletions tests/test_web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,21 @@ def test_single_forwarded_header():
assert req.forwarded[0]['proto'] == 'identifier'


@pytest.mark.parametrize(
"forward_for_in, forward_for_out",
[
("1.2.3.4:1234", "1.2.3.4:1234"),
("1.2.3.4", "1.2.3.4"),
('"[2001:db8:cafe::17]:1234"', '[2001:db8:cafe::17]:1234'),
('"[2001:db8:cafe::17]"', '[2001:db8:cafe::17]'),
])
def test_forwarded_node_identifier(forward_for_in, forward_for_out):
header = 'for={}'.format(forward_for_in)
req = make_mocked_request('GET', '/',
headers=CIMultiDict({'Forwarded': header}))
assert req.forwarded == ({'for': forward_for_out},)


def test_single_forwarded_header_camelcase():
header = 'bY=identifier;fOr=identifier;HOst=identifier;pRoTO=identifier'
req = make_mocked_request('GET', '/',
Expand Down

0 comments on commit 693423d

Please sign in to comment.