Skip to content

Commit e092585

Browse files
jared mauchjared mauch
authored andcommitted
Fix TypeError: find() argument 1 must be str, not bytes
Decode payload to string before processing in Approve handler. The get_payload(decode=True) can return bytes or strings, but the code was using line.find(b':') which assumes bytes. Now we ensure the payload is decoded to a string first, then use string operations consistently. Also fix similar issue with regex operations on payload.
1 parent a56df58 commit e092585

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

Mailman/Handlers/Approve.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,17 @@ def process(mlist, msg, msgdata):
7979
# XXX I'm not entirely sure why, but it is possible for the payload of
8080
# the part to be None, and you can't splitlines() on None.
8181
if part is not None and part.get_payload() is not None:
82-
lines = part.get_payload(decode=True).splitlines()
82+
payload = part.get_payload(decode=True)
83+
# Ensure we have bytes, then decode to string for processing
84+
if isinstance(payload, bytes):
85+
payload = payload.decode('utf-8', errors='replace')
86+
lines = payload.splitlines()
8387
line = ''
8488
for lineno, line in zip(list(range(len(lines))), lines):
8589
if line.strip():
8690
break
8791

88-
i = line.find(b':')
92+
i = line.find(':')
8993
if i >= 0:
9094
name = line[:i]
9195
value = line[i+1:]
@@ -126,6 +130,9 @@ def process(mlist, msg, msgdata):
126130
for part in typed_subpart_iterator(msg, 'text'):
127131
if part is not None and part.get_payload() is not None:
128132
lines = part.get_payload(decode=True)
133+
# Ensure we have a string for regex operations
134+
if isinstance(lines, bytes):
135+
lines = lines.decode('utf-8', errors='replace')
129136
if re.search(pattern, lines):
130137
reset_payload(part, re.sub(pattern, '', lines))
131138
elif re.search(pattern, re.sub('(?s)<.*?>', '', lines)):

0 commit comments

Comments
 (0)