Use secure hash algorithm in WebConfiguration.sign_text
authorLaurent Peuch <cortex@worlddomination.be>
Fri, 12 Apr 2019 12:31:14 +0200
changeset 12584 6eba53763482
parent 12583 4f59a56e6d89
child 12585 933c2b3839ab
Use secure hash algorithm in WebConfiguration.sign_text Fix: PendingDeprecationWarning: HMAC() without an explicit digestmod argument is deprecated. The default hash algorithm used by hmac.new is md5. As of today, md5 is so weak that it's the equivalent of plaintext and can't be considered to be secured at all. Therefor, we switch to a secure hash algorithm. The rational for choosing sha3_512 is: * the recommended algorithm is at least sha_256 * the stronger, the more secured and sha3_512 is the stronger available * thinking about the future this should keep this part of the code safe long enough before people think about checking it again You can read more about choosing a secure hash algorithm in the NIST recommendations https://csrc.nist.gov/Projects/Hash-Functions/NIST-Policy-on-Hash-Functions This code modification should normally be transparent since check_text_sign is exactly this code 'self.sign_text(text) == signature' and that sign_text is only used in combination with it. The only impact is that the hash is going to move from 32 char to 128 which might make html page a bit bigger and that sha3_512 is slow to compute (which is a good thing for security)
cubicweb/web/test/unittest_views_errorform.py
cubicweb/web/webconfig.py
--- a/cubicweb/web/test/unittest_views_errorform.py	Tue Apr 23 09:33:52 2019 +0200
+++ b/cubicweb/web/test/unittest_views_errorform.py	Fri Apr 12 12:31:14 2019 +0200
@@ -51,7 +51,7 @@
                     req.data['ex'] = e
                     html = self.view('error', req=req)
                     self.assertTrue(re.search(b'^<input name="__signature" type="hidden" '
-                                              b'value="[0-9a-f]{32}" />$',
+                                              b'value="[0-9a-f]{128}" />$',
                                               html.source, re.M))
 
 
--- a/cubicweb/web/webconfig.py	Tue Apr 23 09:33:52 2019 +0200
+++ b/cubicweb/web/webconfig.py	Fri Apr 12 12:31:14 2019 +0200
@@ -302,7 +302,8 @@
         # replace \r\n so we do not depend on whether a browser "reencode"
         # original message using \r\n or not
         return hmac.new(self._instance_salt,
-                        text.strip().replace(b'\r\n', b'\n')).hexdigest()
+                        text.strip().replace(b'\r\n', b'\n'),
+                        digestmod="sha3_512").hexdigest()
 
     def check_text_sign(self, text, signature):
         """check the text signature is equal to the given signature"""