Web: Fix crash on trailing ';' in Content-Type

This commit is contained in:
Valentin Lorentz 2023-10-28 09:47:55 +02:00
parent faa6474271
commit 3f9ab4b89c
2 changed files with 13 additions and 2 deletions

View File

@ -186,9 +186,14 @@ class Web(callbacks.PluginRegexp):
encoding = None
if 'Content-Type' in fd.headers:
mime_params = [p.split('=', 1)
# using p.partition('=') instead of 'p.split('=', 1)' because,
# unlike RFC 7231, RFC 9110 allows an empty parameter list
# after ';':
# * https://www.rfc-editor.org/rfc/rfc9110.html#name-media-type
# * https://www.rfc-editor.org/rfc/rfc9110.html#parameter
mime_params = [p.partition('=')
for p in fd.headers['Content-Type'].split(';')[1:]]
mime_params = {k.strip(): v.strip() for (k, v) in mime_params}
mime_params = {k.strip(): v.strip() for (k, sep, v) in mime_params}
if mime_params.get('charset'):
encoding = mime_params['charset']

View File

@ -85,6 +85,12 @@ class WebTestCase(ChannelPluginTestCase):
'title https://www.reddit.com/r/irc/',
'Internet Relay Chat')
def testTitleMarcinfo(self):
# Checks that we don't crash on 'Content-Type: text/html;'
self.assertResponse(
'title https://marc.info/?l=openbsd-tech&m=169841790407370&w=2',
"'Removing syscall(2) from libc and kernel' - MARC")
def testTitleSnarfer(self):
try:
conf.supybot.plugins.Web.titleSnarfer.setValue(True)