Commit ef875721 authored by Laurent Peuch's avatar Laurent Peuch
Browse files

fix(security): drop md5 in favor of sha512 because it's 2020 god damnit

parent 190415376fd5
......@@ -40,7 +40,7 @@ request. This header is computed as a HMAC hash with:
By default, the `method` is the `Cubicweb` string, and the signed
headers are 'Content-MD5', 'Content-Type' and 'Date'.
headers are 'Content-SHA512', 'Content-Type' and 'Date'.
The `doc/sreq_example.py` script is an simple python script showing how to
forge such a HTTP GET request using `urllib2`.
......
......@@ -57,15 +57,15 @@ from email.utils import formatdate
def sign(req, token):
headers_to_sign = ('Content-MD5', 'Content-Type', 'Date')
headers_to_sign = ('Content-SHA512', 'Content-Type', 'Date')
to_sign = (req.method + req.url +
''.join(req.headers.get(field, '')
for field in headers_to_sign))
return hmac.new(token, to_sign).hexdigest()
def md5(data):
h = hashlib.md5(data)
def sha512(data):
h = hashlib.sha512(data)
return h.hexdigest()
......@@ -76,7 +76,7 @@ class SignedRequestAuth(requests.auth.AuthBase):
def __call__(self, r):
if r.method in ('PUT', 'POST'):
r.headers['Content-MD5'] = md5(r.body or '')
r.headers['Content-SHA512'] = sha512(r.body or '')
r.headers['Authorization'] = 'Cubicweb %s:%s' % (
self.token_id, sign(r, self.secret))
return r
......
......@@ -30,11 +30,11 @@ logger = logging.getLogger(__name__)
def body_hash_tween_factory(handler, registry):
"""pyramid tween that add a body_hash attribute to the request with
the md5 sum of the body. This tween must be insterted before any
the sha512 sum of the body. This tween must be insterted before any
body rewrite logic, otherwise we cannot check request's signature"""
def body_hash(request):
"compute and attach the md5 sum of the body of a request"
"compute and attach the sha512 sum of the body of a request"
# code to be executed for each request before
# the actual application code goes here
request.body_hash = hash_content(request.body_file_seekable)
......@@ -50,7 +50,7 @@ class SignedRequestAuthPolicy(object):
Authentication header.
"""
headers_to_sign = ('Content-MD5', 'Content-Type', 'Date')
headers_to_sign = ('Content-SHA512', 'Content-Type', 'Date')
def unauthenticated_userid(self, request):
return None
......
......@@ -47,7 +47,7 @@ except ImportError:
log = logging.getLogger(__name__)
HEADERS_TO_SIGN = ('Content-MD5', 'Content-Type', 'Date')
HEADERS_TO_SIGN = ('Content-SHA512', 'Content-Type', 'Date')
ALTERNATE_HEADERS = {'Date': ['X-Cubicweb-Date', 'Date']}
......@@ -74,20 +74,20 @@ def get_replaceable_header_value(request, header_name, default=None):
def hash_content(content):
"""compute the md5 sum of a file-like object's content
"""compute the sha512 sum of a file-like object's content
Does NOT put the file's pos at the original position
"""
md5 = hashlib.md5()
sha512 = hashlib.sha512()
while True:
data = content.read(4096)
if not data:
break
md5.update(data)
return md5.hexdigest()
sha512.update(data)
return sha512.hexdigest()
def get_credentials_from_headers(request, content_md5):
def get_credentials_from_headers(request, content_sha512):
"""Parse the request headers to retrieve the authentication credentials
Returns the parsed credentials as a string '<tokenid>:<signature>' where
......@@ -110,10 +110,10 @@ def get_credentials_from_headers(request, content_md5):
log.debug('SIGNED REQUEST: method is not Cubicweb')
return
if request.http_method() != 'GET':
if content_md5 != get_replaceable_header_value(request, 'Content-MD5'):
log.error('SIGNED REQUEST: wrong md5, %s != %s' % (
content_md5,
get_replaceable_header_value(request, 'Content-MD5')))
if content_sha512 != get_replaceable_header_value(request, 'Content-SHA512'):
log.error('SIGNED REQUEST: wrong sha512, %s != %s' % (
content_sha512,
get_replaceable_header_value(request, 'Content-SHA512')))
raise AuthenticationError()
date_header = get_replaceable_header_value(request, 'Date')
if date_header is None:
......@@ -183,7 +183,7 @@ def authenticate_user(session, tokenid, signed_content, signature):
assert len(rset) == 1
user_eid, secret_key = rset[0]
expected_signature = hmac.new(secret_key.encode('utf-8'),
signed_content, digestmod="md5").hexdigest()
signed_content, digestmod="sha512").hexdigest()
if compare_digest(expected_signature, signature):
return user_eid
else:
......
......@@ -55,9 +55,9 @@ class HttpRESTAuthRetriever(WebAuthInfoRetriever):
# XXX cw 3.15 compat
content = req._twreq.content
content.seek(0)
md5 = hash_content(content)
sha512 = hash_content(content)
content.seek(0)
credentials = get_credentials_from_headers(req, md5)
credentials = get_credentials_from_headers(req, sha512)
if credentials is None:
raise NoAuthInfo()
return credentials.split(':', 1)
......
......@@ -66,7 +66,7 @@ class SignedRequestBaseTC(object):
{'id': id})
assert rset
return hmac.new(rset[0][0].encode('utf-8'),
string_to_sign, digestmod="md5").hexdigest()
string_to_sign, digestmod="sha512").hexdigest()
def _test_header_format(self, method, login, http_method='GET',
signature=None, headers=None,
......@@ -76,7 +76,9 @@ class SignedRequestBaseTC(object):
def get_valid_authdata(self, headers=None):
if headers is None:
headers = {}
headers.setdefault('Content-MD5', 'd41d8cd98f00b204e9800998ecf8427e')
headers.setdefault('Content-SHA512',
'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13'
'c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e')
headers.setdefault('Content-Type', 'application/xhtml+xml')
headers.setdefault('Date', formatdate(usegmt=True))
return headers
......@@ -133,7 +135,8 @@ class SignedRequestBaseTC(object):
self._build_string_to_sign = orig
def test_post_http_request_signature(self):
headers = {'Content-MD5': '43115f65c182069f76b56df967e5c3fd',
headers = {'Content-SHA512': 'f1bb758ab06b3b4e6c5b0545d827cbc4958c2b0d0b242bdcae2562a517220'
'0f1c603b69964b99c108ca04a3e4f670f8bba2c4cb8f1490112f33438520ea5e3f7',
'Content-Type': 'application/x-www-form-urlencoded',
'Date': formatdate(usegmt=True)}
result, req = self._test_header_format(method='Cubicweb',
......@@ -145,7 +148,8 @@ class SignedRequestBaseTC(object):
def test_post_http_request_signature_with_multipart(self):
date = formatdate(usegmt=True)
headers = {
'Content-MD5': 'f47787068b27ee123d28392f2d21cf70',
'Content-SHA512': '28a505eb101c411f40a36b19d24c008b7f8d4945ec30e17b7fd6d60fd0b27fbf80bb'
'69039ab64e6940f09bf293901b363650195a9437c110ab8825381f1ae9cf',
'Content-Type': 'multipart/form-data; '
'boundary=a71da6eca73a45459439dd288f8185a4',
'Date': date}
......@@ -172,7 +176,8 @@ class SignedRequestBaseTC(object):
def test_post_http_request_signature_with_data(self):
date = formatdate(usegmt=True)
headers = {'Content-MD5': '9893532233caff98cd083a116b013c0b',
headers = {'Content-SHA512': '65c256c639bd6dd483be341831c19a3996954901bb2a07f79593f3e3af569'
'2559bdb124d099c2b92ced7e59b7ed02d3b7f42d50740d999bebd91983db2842762',
'Date': date}
# string_to_sign = ('POSThttp://testing.fr/cubicweb/testauth?'
# 'key1=value19893532233caff98cd083a116b013c0b%s'%date)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment