Merge ~jbfzs/dkimpy:re_backtrack into dkimpy:master

Proposed by Jonathan Bastien-Filiatrault
Status: Merged
Approved by: Scott Kitterman
Approved revision: 35c6a93f0c0d0bc1f1c1a175932b0ec9143e282c
Merge reported by: Scott Kitterman
Merged at revision: 35c6a93f0c0d0bc1f1c1a175932b0ec9143e282c
Proposed branch: ~jbfzs/dkimpy:re_backtrack
Merge into: dkimpy:master
Diff against target: 34 lines (+14/-8)
1 file modified
dkim/canonicalization.py (+14/-8)
Reviewer Review Type Date Requested Status
Scott Kitterman Approve
Review via email: mp+360726@code.launchpad.net

Commit message

Refactor canonicalization.py strip_trailing_lines to avoid using re for more consistent processing across python versions.

Description of the change

This fixes some really rare and pathological cases.

Thanks !
Jonathan

To post a comment you must log in.
Revision history for this message
Scott Kitterman (kitterman) wrote :

Thanks.

What python versions did you test this with? It needs testing with at least 2.7, 3.7, and one lower python3 version.

review: Needs Information
Revision history for this message
Jonathan Bastien-Filiatrault (jbfzs) wrote :

I am using Python 3.6.6. This is the version that had problems and that this patch has been tested on.

Revision history for this message
Scott Kitterman (kitterman) wrote :

OK. I'm going to go ahead and merge this and then do some additional testing.

review: Approve
Revision history for this message
Jonathan Bastien-Filiatrault (jbfzs) wrote :

Alright, thanks for the quick feedback and have a nice day !

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/dkim/canonicalization.py b/dkim/canonicalization.py
2index bb04b1c..8c9ffc1 100644
3--- a/dkim/canonicalization.py
4+++ b/dkim/canonicalization.py
5@@ -41,15 +41,21 @@ def compress_whitespace(content):
6
7
8 def strip_trailing_lines(content):
9- content = re.sub(b"(\r\n)*$", b"\r\n", content)
10- # Yes, this is horrible, but regex processing changed in python3.7 and it
11- # is the least horrible solution I came up with for python2.7, python3.7,
12- # and python3 << 3.7 combined support. Better solution welcome.
13- if len(content) >= 4:
14- if (content[len(content)-4:] == b'\r\n\r\n'):
15- content = content[:len(content)-2]
16- return content
17+ end = None
18+ while content[:end].endswith(b"\r\n"):
19+ if end is None:
20+ end = -2
21+ else:
22+ end -= 2
23+
24+ if end is None:
25+ return content + b"\r\n"
26+
27+ end += 2
28+ if end == 0:
29+ return content
30
31+ return content[:end]
32
33 def unfold_header_value(content):
34 return re.sub(b"\r\n", b"", content)

Subscribers

People subscribed via source and target branches