Skip to content

Commit d1c5042

Browse files
committed
Finish FieldStorage migration: admin, admindb, edithtml.
Replace legacy cgi.FieldStorage [.value] access with getfirst/getlist and a shared _field_upload_text helper for multipart mass subscribe/unsub/memberlist uploads (Mailman.Utils.FieldStorage keeps file bodies in _files, not subscript values).
1 parent 6566538 commit d1c5042

3 files changed

Lines changed: 39 additions & 30 deletions

File tree

Mailman/Cgi/admin.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ def D_(s):
5656
AUTH_CONTEXTS = (mm_cfg.AuthListAdmin, mm_cfg.AuthSiteAdmin)
5757

5858

59+
def _field_upload_text(cgidata, fieldname):
60+
"""Decode multipart file upload text, or fall back to getfirst (FieldStorage)."""
61+
fn = getattr(cgidata, 'filename', None)
62+
if callable(fn) and fn(fieldname):
63+
fobj = cgidata.file(fieldname)
64+
if fobj is None:
65+
return cgidata.getfirst(fieldname, '') or ''
66+
try:
67+
raw = fobj.read()
68+
finally:
69+
try:
70+
fobj.close()
71+
except EnvironmentError:
72+
pass
73+
try:
74+
os.unlink(fobj.name)
75+
except OSError:
76+
pass
77+
enc = getattr(cgidata, 'encoding', None) or 'utf-8'
78+
if isinstance(raw, bytes):
79+
return raw.decode(enc, getattr(cgidata, 'errors', None) or 'replace')
80+
return raw or ''
81+
return cgidata.getfirst(fieldname, '') or ''
82+
83+
5984

6085
def main():
6186
# Try to find out which list is being administered
@@ -1459,10 +1484,7 @@ def safeint(formvar, defaultval=None):
14591484
# mass subscription, removal processing for members category
14601485
subscribers = ''
14611486
subscribers += str(cgidata.getfirst('subscribees', ''))
1462-
sub_uploads = cgidata.getfirst('subscribees_upload', '')
1463-
if isinstance(sub_uploads, bytes):
1464-
sub_uploads = sub_uploads.decode()
1465-
subscribers += sub_uploads
1487+
subscribers += _field_upload_text(cgidata, 'subscribees_upload')
14661488
if subscribers:
14671489
entries = [_f for _f in [n.strip() for n in subscribers.splitlines()] if _f]
14681490
send_welcome_msg = safeint('send_welcome_msg_to_this_batch',
@@ -1540,13 +1562,8 @@ def safeint(formvar, defaultval=None):
15401562
# Unsubscriptions
15411563
removals = ''
15421564
if 'unsubscribees' in cgidata:
1543-
removals += cgidata['unsubscribees'].value
1544-
if 'unsubscribees_upload' in cgidata and \
1545-
cgidata['unsubscribees_upload'].value:
1546-
unsub_upload = cgidata['unsubscribees_upload'].value
1547-
if isinstance(unsub_upload, bytes):
1548-
unsub_upload = unsub_upload.decode()
1549-
removals += unsub_upload
1565+
removals += cgidata.getfirst('unsubscribees', '') or ''
1566+
removals += _field_upload_text(cgidata, 'unsubscribees_upload')
15501567
if removals:
15511568
names = [_f for _f in [n.strip() for n in removals.splitlines()] if _f]
15521569
send_unsub_notifications = safeint(
@@ -1651,10 +1668,7 @@ def safeint(formvar, defaultval=None):
16511668
# sync operation
16521669
memberlist = ''
16531670
memberlist += cgidata.getvalue('memberlist', '')
1654-
upload = cgidata.getvalue('memberlist_upload', '')
1655-
if isinstance(upload, bytes):
1656-
upload = upload.decode()
1657-
memberlist += upload
1671+
memberlist += _field_upload_text(cgidata, 'memberlist_upload')
16581672
if memberlist:
16591673
# Browsers will convert special characters in the text box to HTML
16601674
# entities. We need to fix those.
@@ -1745,13 +1759,7 @@ def clean_input(x):
17451759
mlist.setMemberOption(member, mm_cfg.Moderate, val)
17461760
# do the user options for members category
17471761
if 'setmemberopts_btn' in cgidata and 'user' in cgidata:
1748-
user = cgidata['user']
1749-
if type(user) is list:
1750-
users = []
1751-
for ui in range(len(user)):
1752-
users.append(urllib.parse.unquote(user[ui].value))
1753-
else:
1754-
users = [urllib.parse.unquote(user.value)]
1762+
users = [urllib.parse.unquote(u) for u in cgidata.getlist('user')]
17551763
errors = []
17561764
removes = []
17571765
for user in users:

Mailman/Cgi/admindb.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -932,11 +932,12 @@ def process_form(mlist, doc, cgidata):
932932
banaddrs = []
933933
erroraddrs = []
934934
for k in list(cgidata.keys()):
935-
formv = cgidata[k]
936-
if type(formv) == list:
935+
vals = cgidata.getlist(k)
936+
if len(vals) != 1:
937937
continue
938+
formv = vals[0]
938939
try:
939-
v = int(formv.value)
940+
v = int(formv)
940941
request_id = int(k)
941942
except ValueError:
942943
continue
@@ -965,13 +966,13 @@ def process_form(mlist, doc, cgidata):
965966
forward = 0
966967
forwardaddr = ''
967968
if commentkey in cgidata:
968-
comment = cgidata[commentkey].value
969+
comment = cgidata.getfirst(commentkey)
969970
if preservekey in cgidata:
970-
preserve = cgidata[preservekey].value
971+
preserve = cgidata.getfirst(preservekey)
971972
if forwardkey in cgidata:
972-
forward = cgidata[forwardkey].value
973+
forward = cgidata.getfirst(forwardkey)
973974
if forwardaddrkey in cgidata:
974-
forwardaddr = cgidata[forwardaddrkey].value
975+
forwardaddr = cgidata.getfirst(forwardaddrkey)
975976
# Should we ban this address? Do this check before handling the
976977
# request id because that will evict the record.
977978
if cgidata.getfirst(bankey):

Mailman/Cgi/edithtml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def ChangeHTML(mlist, cgi_info, template_name, doc, lang=None):
240240
doc.AddItem(Header(3,_("HTML Unchanged.")))
241241
doc.AddItem('<hr>')
242242
return
243-
code = cgi_info['html_code'].value
243+
code = cgi_info.getfirst('html_code')
244244
if Utils.suspiciousHTML(code):
245245
doc.AddItem(Header(3,
246246
_(f"""The page you saved contains suspicious HTML that could

0 commit comments

Comments
 (0)