Ticket #10188: 10188.diff

File 10188.diff, 2.2 KB (added by Bob Thomas, 16 years ago)

Patch with doc and tests

  • django/http/__init__.py

     
    263263        cookiedict[key] = c.get(key).value
    264264    return cookiedict
    265265
     266class BadHeaderError(ValueError):
     267    pass
     268
    266269class HttpResponse(object):
    267270    """A basic HTTP response, with content and dictionary-accessed headers."""
    268271
     
    301304    def _convert_to_ascii(self, *values):
    302305        """Converts all values to ascii strings."""
    303306        for value in values:
     307            if '\n' in value or '\r' in value:
     308                raise BadHeaderError("Header values can't contain newlines (got %r)" % (value))
    304309            if isinstance(value, unicode):
    305310                try:
    306311                    yield value.encode('us-ascii')
  • tests/regressiontests/httpwrappers/tests.py

     
    444444...
    445445UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
    446446
     447# Bug #10188: Do not allow newlines in headers (CR or LF)
     448>>> r['test\\rstr'] = 'test'
     449Traceback (most recent call last):
     450...
     451BadHeaderError: Header values can't contain newlines (got 'test\\rstr')
     452
     453>>> r['test\\nstr'] = 'test'
     454Traceback (most recent call last):
     455...
     456BadHeaderError: Header values can't contain newlines (got 'test\\nstr')
     457
    447458#
    448459# Regression test for #8278: QueryDict.update(QueryDict)
    449460#
  • docs/ref/request-response.txt

     
    445445    >>> response = HttpResponse()
    446446    >>> response['Pragma'] = 'no-cache'
    447447
     448.. versionadded:: 1.1
     449
     450HTTP headers cannot contain newlines. An attempt to set a header containing a
     451newline character (CR or LF) will raise ``BadHeaderError``
     452
    448453Telling the browser to treat the response as a file attachment
    449454~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    450455
Back to Top