From 419f1d98009f95932f181b8a3064f84cbda6b8e0 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 10 Jul 2014 15:33:10 -0700 Subject: [PATCH] Implemented relaxed canonicalization --- flanker/dkim.py | 23 +++++++++++++++---- tests/dkim_test.py | 11 ++++++--- .../fixtures/messages/dkim/nofws.expected.01 | 1 + .../fixtures/messages/dkim/nofws.expected.02 | 1 + .../fixtures/messages/dkim/nofws.expected.03 | 1 + .../fixtures/messages/dkim/nofws.expected.24 | 1 + .../messages/dkim/relaxed.expected.01 | 1 - .../messages/dkim/relaxed.expected.02 | 1 - .../messages/dkim/relaxed.expected.03 | 1 - 9 files changed, 31 insertions(+), 10 deletions(-) diff --git a/flanker/dkim.py b/flanker/dkim.py index 8702d5b..e0d5416 100644 --- a/flanker/dkim.py +++ b/flanker/dkim.py @@ -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 "" diff --git a/tests/dkim_test.py b/tests/dkim_test.py index 76b6670..c72cb95 100644 --- a/tests/dkim_test.py +++ b/tests/dkim_test.py @@ -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() diff --git a/tests/fixtures/messages/dkim/nofws.expected.01 b/tests/fixtures/messages/dkim/nofws.expected.01 index e69de29..d3f5a12 100644 --- a/tests/fixtures/messages/dkim/nofws.expected.01 +++ b/tests/fixtures/messages/dkim/nofws.expected.01 @@ -0,0 +1 @@ + diff --git a/tests/fixtures/messages/dkim/nofws.expected.02 b/tests/fixtures/messages/dkim/nofws.expected.02 index e91711d..191c24f 100644 --- a/tests/fixtures/messages/dkim/nofws.expected.02 +++ b/tests/fixtures/messages/dkim/nofws.expected.02 @@ -1 +1,2 @@ From: + diff --git a/tests/fixtures/messages/dkim/nofws.expected.03 b/tests/fixtures/messages/dkim/nofws.expected.03 index e91711d..191c24f 100644 --- a/tests/fixtures/messages/dkim/nofws.expected.03 +++ b/tests/fixtures/messages/dkim/nofws.expected.03 @@ -1 +1,2 @@ From: + diff --git a/tests/fixtures/messages/dkim/nofws.expected.24 b/tests/fixtures/messages/dkim/nofws.expected.24 index d9d0131..f5745c7 100644 --- a/tests/fixtures/messages/dkim/nofws.expected.24 +++ b/tests/fixtures/messages/dkim/nofws.expected.24 @@ -3,3 +3,4 @@ Subject:Thissubjectthatwrapsontoanewlinewithaspaceandthenontoanotherlineindented To: Comment:Thislinehasleadingspacesandwrapstoasecondandathirdandafourthandafirthandlastlyasixed Message-Id:<12345.mail@example.net> + diff --git a/tests/fixtures/messages/dkim/relaxed.expected.01 b/tests/fixtures/messages/dkim/relaxed.expected.01 index 99a8091..d3f5a12 100644 --- a/tests/fixtures/messages/dkim/relaxed.expected.01 +++ b/tests/fixtures/messages/dkim/relaxed.expected.01 @@ -1,2 +1 @@ - diff --git a/tests/fixtures/messages/dkim/relaxed.expected.02 b/tests/fixtures/messages/dkim/relaxed.expected.02 index a846b81..19a6ce4 100644 --- a/tests/fixtures/messages/dkim/relaxed.expected.02 +++ b/tests/fixtures/messages/dkim/relaxed.expected.02 @@ -1,3 +1,2 @@ from: - diff --git a/tests/fixtures/messages/dkim/relaxed.expected.03 b/tests/fixtures/messages/dkim/relaxed.expected.03 index a846b81..19a6ce4 100644 --- a/tests/fixtures/messages/dkim/relaxed.expected.03 +++ b/tests/fixtures/messages/dkim/relaxed.expected.03 @@ -1,3 +1,2 @@ from: - -- 2.43.0