Skip to content

Commit 758b99b

Browse files
committed
parser: Strip NUL bytes
psycopg (v3) strictly rejects NUL bytes (0x00) in PostgreSQL text fields, unlike psycopg2 which handled them silently. Malformed emails (such as the codec-null.mbox fuzz fixture in our test suite) contain NUL bytes in headers and body content, causing DataError on insert. Strip NUL bytes from all text fields before they reach the database. This is safe for all fields: * Headers are restricted to printable ASCII by RFC 5322 meaning NUL bytes there always indicate corruption * Email body / commit message content is plain text so NUL bytes are equally invalid here * Unified diffs should also never contain NUL bytes. Git uses their presence as the heuristic to classify a file as binary, at which point it either emits 'Binary files ... differ' or, with --binary, a base85-encoded binary patch, both of which are entirely printable ASCII. A legitimate patch produced by git format-patch will therefore never carry NUL bytes in the diff text itself. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent de04e0a commit 758b99b

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

‎patchwork/parser.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def clean_header(header):
151151
return None
152152

153153
header_str = str(sane_header)
154+
header_str = header_str.replace('\x00', '')
154155

155156
return normalise_space(header_str)
156157

@@ -498,7 +499,7 @@ def find_headers(mail):
498499
if header is not None
499500
]
500501

501-
return '\n'.join(strings)
502+
return '\n'.join(strings).replace('\x00', '')
502503

503504

504505
def find_message_id(mail):
@@ -697,6 +698,9 @@ def find_patch_content(mail):
697698

698699
commentbuf = clean_content(commentbuf)
699700

701+
if patchbuf:
702+
patchbuf = patchbuf.replace('\x00', '')
703+
700704
return patchbuf, commentbuf
701705

702706

@@ -873,7 +877,7 @@ def clean_content(content):
873877
sig_re = re.compile(r'^(-- |_+)\n.*', re.S | re.M)
874878
content = sig_re.sub('', content)
875879

876-
return content.strip()
880+
return content.strip().replace('\x00', '')
877881

878882

879883
def parse_patch(content):

0 commit comments

Comments
 (0)