Skip to content

Commit

Permalink
Clean up CRLF injection protection
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Jun 29, 2015
1 parent a32cbe5 commit 02109fe
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions aspen/http/baseheaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@


from aspen.backcompat import CookieError, SimpleCookie

from aspen.http.mapping import CaseInsensitiveMapping
from aspen.utils import typecheck


def _check_for_CRLF(value):
"""CRLF injection allows an attacker to insert arbitrary headers.
http://www.acunetix.com/websitesecurity/crlf-injection/
https://github.com/gratipay/security-qf35us/issues/1
"""
if b'\r' in value or b'\n' in value:
from aspen.exceptions import CRLFInjection
raise CRLFInjection()


class BaseHeaders(CaseInsensitiveMapping):
"""Represent the headers in an HTTP Request or Response message.
http://stackoverflow.com/questions/5423223/how-to-send-non-english-unicode-string-using-http-header
Expand Down Expand Up @@ -54,16 +65,13 @@ def genheaders():


def __setitem__(self, name, value):
"""Extend to protect against CRLF injection:
http://www.acunetix.com/websitesecurity/crlf-injection/
"""
if b'\n' in value:
from aspen.exceptions import CRLFInjection
raise CRLFInjection()
_check_for_CRLF(value)
super(BaseHeaders, self).__setitem__(name, value)

def add(self, name, value):
_check_for_CRLF(value)
super(BaseHeaders, self).add(name, value)


def raw(self):
"""Return the headers as a string, formatted for an HTTP message.
Expand Down

0 comments on commit 02109fe

Please sign in to comment.