]> khamidou — GitHub mirrors - flanker.git/commitdiff
Implemented relaxed canonicalization
authorAlex Gaynor <alex.gaynor@gmail.com>
Thu, 10 Jul 2014 22:33:10 +0000 (15:33 -0700)
committerAlex Gaynor <alex.gaynor@gmail.com>
Thu, 10 Jul 2014 22:33:10 +0000 (15:33 -0700)
flanker/dkim.py
tests/dkim_test.py
tests/fixtures/messages/dkim/nofws.expected.01
tests/fixtures/messages/dkim/nofws.expected.02
tests/fixtures/messages/dkim/nofws.expected.03
tests/fixtures/messages/dkim/nofws.expected.24
tests/fixtures/messages/dkim/relaxed.expected.01
tests/fixtures/messages/dkim/relaxed.expected.02
tests/fixtures/messages/dkim/relaxed.expected.03

index 8702d5b917971ed0067ae4a34d3220ed41d5787b..e0d5416a56840e8d89194fac6c198f0f0dd114b8 100644 (file)
@@ -1,5 +1,5 @@
 import base64
-import re
+import regex as re
 import time
 
 from cryptography.hazmat.backends import default_backend
@@ -7,7 +7,8 @@ from cryptography.hazmat.primitives import hashes
 from cryptography.hazmat.primitives.asymmetric import padding
 
 
-_BODY_LINES_RE = re.compile(r"\r?\n")
+_BODY_TRAILING_WSP = re.compile(r"[\t ]+\r\n")
+_BODY_WSP_RE = re.compile(r"[\t ]+")
 
 
 class SimpleCanonicalization(object):
@@ -20,16 +21,30 @@ class SimpleCanonicalization(object):
         return body.rstrip("\r\n") + "\r\n"
 
 
+class RelaxedCanonicalization(object):
+    name = "relaxed"
+
+    def canonicalize_header(self, header, value):
+        header = header.lower()
+        value = _BODY_WSP_RE.sub(" ", value.replace("\r\n", ""))
+        return header, value.strip() + b"\r\n"
+
+    def canonicalize_body(self, body):
+        body = _BODY_TRAILING_WSP.sub("\r\n", body)
+        body = _BODY_WSP_RE.sub(" ", body)
+        body = body.rstrip("\r\n")
+        return body + b"\r\n" if body else b""
+
+
 class NoFWSCanonicalization(object):
     _header_fws_re = re.compile(r"[\t \r\n]+")
-    _body_wsp_re = re.compile(r"[\t ]+")
     _body_orphan_cr_re = re.compile(b"\r([^\n])")
 
     def canonicalize_header(self, header, value):
         return header, self._header_fws_re.sub("", value) + "\r\n"
 
     def canonicalize_body(self, body):
-        body = self._body_wsp_re.sub("", body)
+        body = _BODY_WSP_RE.sub("", body)
         body = self._body_orphan_cr_re.sub(r"\1", body)
         body = body.rstrip()
         return body + "\r\n" if body else ""
index 76b667001ddf404bd9c985cb4603ce32c67a0092..c72cb95a8056688ea58367126c47fb8f31eb3a38 100644 (file)
@@ -81,6 +81,8 @@ def test_canonicalization():
             nofws_contents = f.read()
         with open(path.replace("email", "simple.expected")) as f:
             simple_contents = f.read()
+        with open(path.replace("email", "relaxed.expected")) as f:
+            relaxed_contents = f.read()
 
         assert_equal(
             canonicalize_contents(dkim.NoFWSCanonicalization(), contents),
@@ -90,6 +92,10 @@ def test_canonicalization():
             canonicalize_contents(dkim.SimpleCanonicalization(), contents),
             simple_contents
         )
+        assert_equal(
+            canonicalize_contents(dkim.RelaxedCanonicalization(), contents),
+            relaxed_contents
+        )
 
 
 def canonicalize_contents(canonicalization_rule, contents):
@@ -100,7 +106,6 @@ def canonicalize_contents(canonicalization_rule, contents):
             header, value)
         output.write(b"{h}:{v}".format(h=header, v=value))
     body = canonicalization_rule.canonicalize_body(body)
-    if body:
-        output.write(b"\r\n")
-        output.write(body)
+    output.write(b"\r\n")
+    output.write(body)
     return output.getvalue()
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..d3f5a12faa99758192ecc4ed3fc22c9249232e86 100644 (file)
@@ -0,0 +1 @@
+\r
index e91711decd102eb7b406ea554d1331fb8f204d25..191c24f818f117b909eeefecb8ac6980038dbe9d 100644 (file)
@@ -1 +1,2 @@
 From:<joe@example.net>\r
+\r
index e91711decd102eb7b406ea554d1331fb8f204d25..191c24f818f117b909eeefecb8ac6980038dbe9d 100644 (file)
@@ -1 +1,2 @@
 From:<joe@example.net>\r
+\r
index d9d013123aeed377dc791020c3b521cfe72b669f..f5745c772dd9962fc69904dbcc3414f468c42a16 100644 (file)
@@ -3,3 +3,4 @@ Subject:Thissubjectthatwrapsontoanewlinewithaspaceandthenontoanotherlineindented
 To:<larry@example.com>\r
 Comment:Thislinehasleadingspacesandwrapstoasecondandathirdandafourthandafirthandlastlyasixed\r
 Message-Id:<12345.mail@example.net>\r
+\r
index 99a8091366e2ac7f301452706d5cfdff5e320a0d..d3f5a12faa99758192ecc4ed3fc22c9249232e86 100644 (file)
@@ -1,2 +1 @@
 \r
-\r
index a846b81ed6ce0473a21f936fe9fe394258baabee..19a6ce476eea77e14a56b61fee857ad03b2775ae 100644 (file)
@@ -1,3 +1,2 @@
 from:<joe@example.net>\r
 \r
-\r
index a846b81ed6ce0473a21f936fe9fe394258baabee..19a6ce476eea77e14a56b61fee857ad03b2775ae 100644 (file)
@@ -1,3 +1,2 @@
 from:<joe@example.net>\r
 \r
-\r