Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strictly require ASCII headers names and values #137

Merged
merged 1 commit into from
Aug 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions aiohttp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import http.server
import itertools
import re
import string
import sys
import zlib
from wsgiref.handlers import format_date_time
Expand All @@ -20,6 +21,7 @@
from aiohttp import multidict
from aiohttp.log import internal_log

ASCIISET = set(string.printable)
METHRE = re.compile('[A-Z0-9$-_.]+')
VERSRE = re.compile('HTTP/(\d+).(\d+)')
HDRRE = re.compile('[\x00-\x1F\x7F()<>@,;:\[\]={} \t\\\\\"]')
Expand Down Expand Up @@ -572,8 +574,12 @@ def add_header(self, name, value):
"""Analyze headers. Calculate content length,
removes hop headers, etc."""
assert not self.headers_sent, 'headers have been sent already'
assert isinstance(name, str), '{!r} is not a string'.format(name)
assert isinstance(value, str), '{!r} is not a string'.format(value)
assert isinstance(name, str), \
'Header name should be a string, got {!r}'.format(name)
assert set(name).issubset(ASCIISET), \
'Header name should contain ASCII chars, got {!r}'.format(name)
assert isinstance(value, str), \
'Header {!r} should have string value, got {!r}'.format(name, value)

name = name.strip().upper()
value = value.strip()
Expand Down
12 changes: 11 additions & 1 deletion tests/test_http_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,22 @@ def test_add_header_with_spaces(self):
self.assertEqual(
[('CONTENT-TYPE', 'plain/html')], list(msg.headers.items()))

def test_add_header_non_ascii(self):
msg = protocol.Response(self.transport, 200)
self.assertEqual([], list(msg.headers))

with self.assertRaises(AssertionError):
msg.add_header('тип-контента', 'текст/плейн')

def test_add_header_invalid_value_type(self):
msg = protocol.Response(self.transport, 200)
self.assertEqual([], list(msg.headers))

with self.assertRaises(AssertionError):
msg.add_header('content-type', b'value')
msg.add_header('content-type', {'test': 'plain'})

with self.assertRaises(AssertionError):
msg.add_header(list('content-type'), 'text/plain')

def test_add_headers(self):
msg = protocol.Response(self.transport, 200)
Expand Down