Handle patch trailers with special characters
v2 cover Handle patch trailers with special characters
Fixes bug reported in https://github.com/mricon/b4/issues/50
Patch trailers that contain commas are not parsed correctly, this causes the trailer to be set to a blank string. The issue is special characters in emails are supposed to be quoted per RFC 2822. But it's fairly common for patch trailers to be hand-written and be improperly formatted.
The trailer tests were hiding some of the parsing issues, so the first few patches fix up the test.
While fixing this bug I discovered the trailer parser also breaks even when the trailer is properly quoted, so I fix that in this series as well.
---
Changes in v2:
- Fixed other tests that broke when the email parsing changed
- Link to v1: https://patch.msgid.link/[email protected]
---
Brandon Maier (6):
src/tests: check trailer extinfo
src/tests: fix trailer test hiding parser errors
b4: handle trailers with improper formatting src/tests: test for trailers with improper formatting
b4: handle trailers with special characters src/tests: test for trailers with special characters
src/b4/__init__.py | 6 +++--- .../samples/trailers-followup-bare-address-ref-defaults.txt | 2 +- .../samples/trailers-followup-name-parens-ref-defaults.txt | 2 +- src/tests/samples/trailers-test-extinfo.txt | 1 + src/tests/samples/trailers-test-simple.txt | 1 + src/tests/test___init__.py | 13 ++++++++++--- 6 files changed, 17 insertions(+), 8 deletions(-)
---
base-commit: 2a6338e451a0c1e81f214f48c820c1e52d76b2f1
change-id: 20241027-trailer-special-chars-78b97ff63ea9
Best regards,
Signature · 1 line
Brandon Maier <[email protected]>
PATCH v2 1/6 src/tests: check trailer extinfo
The b4.LoreTrailer.__eq__ ignores the 'extinfo' of a trailer. So the trailer test is not checking that extinfo is parsed correctly.
--- src/tests/test___init__.py | 1 + 1 file changed, 1 insertion(+)
src/tests/test___init__.py +1 -0
@@ -79,6 +79,7 @@ def test_parse_trailers(sampledir, source, expected): mytype, myname, myvalue, myextinfo = expected.pop(0) mytr = b4.LoreTrailer(name=myname, value=myvalue, extinfo=myextinfo) assert tr == mytr + assert tr.extinfo == mytr.extinfo assert tr.type == mytype
Signature · 1 line
2.47.0
PATCH v2 2/6 src/tests: fix trailer test hiding parser errors
In reply to Brandon MaierThe trailer parsing test compares the values of the b4.LoreTrailer object created from the test input against a b4.LoreTrailer object created from our expected test input.
This means if there is an error in how b4.LoreTrailer parses input, it won't be detected. This was discovered from a reported bug with parsing email trailers with embedded ','.
--- src/tests/test___init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
src/tests/test___init__.py +6 -2
@@ -57,7 +57,7 @@ def test_save_git_am_mbox(sampledir, tmp_path, source, regex, flags, ismbox): @pytest.mark.parametrize('source,expected', [ ('trailers-test-simple', - [('person', 'Reviewed-By', 'Bogus Bupkes <[email protected]>', None), + [('person', 'Reviewed-by', 'Bogus Bupkes <[email protected]>', None), ('utility', 'Fixes', 'abcdef01234567890', None), ('utility', 'Link', 'https://msgid.link/[email protected]', None), ]),
@@ -77,10 +77,14 @@ def test_parse_trailers(sampledir, source, expected): assert len(expected) == len(trs) for tr in trs: mytype, myname, myvalue, myextinfo = expected.pop(0) + assert tr.name == myname + assert tr.value == myvalue + assert tr.extinfo == myextinfo + assert tr.type == mytype + mytr = b4.LoreTrailer(name=myname, value=myvalue, extinfo=myextinfo) assert tr == mytr assert tr.extinfo == mytr.extinfo - assert tr.type == mytype @pytest.mark.parametrize('source,serargs,amargs,reference,b4cfg', [
Signature · 1 line
2.47.0
PATCH v2 3/6 b4: handle trailers with improper formatting
In reply to Brandon MaierA patch trailer with an improperly formatted email parses to an empty string. For example see the linked github issue, where a trailer's display name contains an unquoted comma.
As patch trailers are often hand written this can happen frequently. Try to gracefully handle this case by checking if an email parses correctly, and if it doesn't fallback to displaying it verbatim.
--- src/b4/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
src/b4/__init__.py +3 -1
@@ -1063,8 +1063,10 @@ def __init__(self, name: Optional[str] = None, value: Optional[str] = None, exti # Normalize the value with parsed data if self.addr[0]: self.value = f'{self.addr[0]} <{self.addr[1]}>' - else: + elif self.addr[1]: self.value = self.addr[1] + else: + self.type = 'unknown' else: self.type = 'unknown' self.lname = self.name.lower()
Signature · 1 line
2.47.0
PATCH v2 4/6 src/tests: test for trailers with improper formatting
In reply to Brandon MaierCheck that a trailer with an improperly formatted email address will still parse to something usable.
--- src/tests/samples/trailers-test-extinfo.txt | 1 + src/tests/test___init__.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)
src/tests/samples/trailers-test-extinfo.txt +1 -0
@@ -4,6 +4,7 @@ Date: Tue, 30 Aug 2022 11:19:07 -0400 This is a simple trailer parsing test. +Reported-by: Some, One <[email protected]> Reviewed-by: Bogus Bupkes <[email protected]> [for the parts that are bogus] Fixes: abcdef01234567890
src/tests/test___init__.py +2 -1
@@ -62,7 +62,8 @@ def test_save_git_am_mbox(sampledir, tmp_path, source, regex, flags, ismbox): ('utility', 'Link', 'https://msgid.link/[email protected]', None), ]), ('trailers-test-extinfo', - [('person', 'Reviewed-by', 'Bogus Bupkes <[email protected]>', '[for the parts that are bogus]'), + [('unknown', 'Reported-by', 'Some, One <[email protected]>', None), + ('person', 'Reviewed-by', 'Bogus Bupkes <[email protected]>', '[for the parts that are bogus]'), ('utility', 'Fixes', 'abcdef01234567890', None), ('person', 'Tested-by', 'Some Person <[email protected]>', ' [this person visually indented theirs]'), ('utility', 'Link', 'https://msgid.link/[email protected]', ' # initial submission'),
Signature · 1 line
2.47.0
PATCH v2 5/6 b4: handle trailers with special characters
In reply to Brandon MaierParsing an email trailer with special characters breaks the formatting. For example this trailer.
Quoted 1 line
Reviewed-by "Doe, Jane" <[email protected]>
Will get parsed into this.
Quoted 1 line
Reviewed-by Doe, Jane <[email protected]>
This is because b4.LoreTrailer attempts to reformat the trailer using a naive string format. Instead use the Python standard library formataddr function which correctly handles special characters.
--- src/b4/__init__.py | 6 ++---- src/tests/samples/trailers-followup-bare-address-ref-defaults.txt | 2 +- src/tests/samples/trailers-followup-name-parens-ref-defaults.txt | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-)
src/b4/__init__.py +2 -4
@@ -1061,10 +1061,8 @@ def __init__(self, name: Optional[str] = None, value: Optional[str] = None, exti self.type = 'person' self.addr = email.utils.parseaddr(value) # Normalize the value with parsed data - if self.addr[0]: - self.value = f'{self.addr[0]} <{self.addr[1]}>' - elif self.addr[1]: - self.value = self.addr[1] + if self.addr[0] or self.addr[1]: + self.value = email.utils.formataddr(self.addr) else: self.type = 'unknown' else:
src/tests/samples/trailers-followup-bare-address-ref-defaults.txt +1 -1
@@ -14,7 +14,7 @@ Cc: [email protected] Reviewed-by: Original Reviewer <[email protected]> Link: https://msgid.link/[email protected] Signed-off-by: Original Submitter <[email protected]> -Reviewed-by: Followup Reviewer1 (corporate) <[email protected]> +Reviewed-by: "Followup Reviewer1 (corporate)" <[email protected]> Tested-by: Followup Reviewer2 <[email protected]> diff --git a/b4/junk.py b/b4/junk.py
src/tests/samples/trailers-followup-name-parens-ref-defaults.txt +1 -1
@@ -13,7 +13,7 @@ Fixes: abcdef01234567890 Reviewed-by: Original Reviewer <[email protected]> Link: https://msgid.link/[email protected] Signed-off-by: Original Submitter <[email protected]> -Reviewed-by: Followup Reviewer1 (corporate) <[email protected]> +Reviewed-by: "Followup Reviewer1 (corporate)" <[email protected]> Tested-by: Followup Reviewer2 <[email protected]> diff --git a/b4/junk.py b/b4/junk.py
Signature · 1 line
2.47.0
PATCH v2 6/6 src/tests: test for trailers with special characters
In reply to Brandon MaierTest that email trailers with special characters are parsed correctly.
--- src/tests/samples/trailers-test-simple.txt | 1 + src/tests/test___init__.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)
src/tests/samples/trailers-test-simple.txt +1 -0
@@ -4,6 +4,7 @@ Date: Tue, 30 Aug 2022 11:19:07 -0400 This is a simple trailer parsing test. +Reported-by: "Doe, Jane" <[email protected]> Reviewed-by: Bogus Bupkes <[email protected]> Fixes: abcdef01234567890 Link: https://msgid.link/[email protected]
src/tests/test___init__.py +2 -1
@@ -57,7 +57,8 @@ def test_save_git_am_mbox(sampledir, tmp_path, source, regex, flags, ismbox): @pytest.mark.parametrize('source,expected', [ ('trailers-test-simple', - [('person', 'Reviewed-by', 'Bogus Bupkes <[email protected]>', None), + [('person', 'Reported-by', '"Doe, Jane" <[email protected]>', None), + ('person', 'Reviewed-by', 'Bogus Bupkes <[email protected]>', None), ('utility', 'Fixes', 'abcdef01234567890', None), ('utility', 'Link', 'https://msgid.link/[email protected]', None), ]),
Signature · 1 line
2.47.0
Konstantin Ryabitsev
AppliedQuoted 8 lines · Brandon Maier
Fixes bug reported in https://github.com/mricon/b4/issues/50
Patch trailers that contain commas are not parsed correctly, this causes the trailer to be set to a blank string. The issue is special characters in emails are supposed to be quoted per RFC 2822. But it's fairly common for patch trailers to be hand-written and be improperly formatted.
[...]
Applied, thanks!
[1/6] src/tests: check trailer extinfo
commit: 0d0d2e2e53013302d0625eefb4ee519166802c1b
[2/6] src/tests: fix trailer test hiding parser errors
commit: 285445c83c066f8ba39657d8bf360df729b60002
[3/6] b4: handle trailers with improper formatting
commit: cc2d6bfbc6d2b819d8d291c3b7470642893d69fa
[4/6] src/tests: test for trailers with improper formatting
commit: e1ba36d1a62be7972c46668c89214dda00b8d6f9
[5/6] b4: handle trailers with special characters
commit: 83327d2fbad9506afc2a3e30566025345e157eca
[6/6] src/tests: test for trailers with special characters
commit: f147f985fa6ad6558603b83fa0a7bf38aeefd436Best regards,
Signature · 1 line
Konstantin Ryabitsev <[email protected]>