From 3f9ab4b89c71612b11b8130a332ae83c1f87f5b5 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 28 Oct 2023 09:47:55 +0200 Subject: [PATCH] Web: Fix crash on trailing ';' in Content-Type --- plugins/Web/plugin.py | 9 +++++++-- plugins/Web/test.py | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/plugins/Web/plugin.py b/plugins/Web/plugin.py index a3e258e4d..9deb19fda 100644 --- a/plugins/Web/plugin.py +++ b/plugins/Web/plugin.py @@ -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'] diff --git a/plugins/Web/test.py b/plugins/Web/test.py index 88fd10cac..e8ecdff33 100644 --- a/plugins/Web/test.py +++ b/plugins/Web/test.py @@ -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)