]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
parser: catch failures in decoding headers
authorDaniel Axtens <dja@axtens.net>
Wed, 28 Jun 2017 07:48:46 +0000 (17:48 +1000)
committerStephen Finucane <stephen@that.guru>
Wed, 28 Jun 2017 20:41:28 +0000 (21:41 +0100)
Headers can fail to decode:
 - if a part cannot be encoded as ascii
 - if the coding hint names a codec that doesn't exist
 - if there's a null byte in the codec name

Catch these errors.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com>
Reviewed-by: Stephen Finucane <stephen@that.guru>
patchwork/parser.py

index 534e92c776429ce6706e193a1f4e8ec05fa88f7c..f9b5dc04d19b5e91695571dc91478c392369954f 100644 (file)
@@ -90,9 +90,11 @@ def sanitise_header(header_contents, header_name=None):
         header = make_header(value,
                              header_name=header_name,
                              continuation_ws='\t')
-    except UnicodeDecodeError:
-        # At least one of the parts cannot be encoded as ascii.
-        # Find out which one and fix it somehow.
+    except (UnicodeDecodeError, LookupError, ValueError, TypeError):
+        #  - a part cannot be encoded as ascii. (UnicodeDecodeError), or
+        #  - we don't have a codec matching the hint (LookupError)
+        #  - the codec has a null byte (Py3 ValueError/Py2 TypeError)
+        # Find out which part and fix it somehow.
         #
         # We get here under Py2 when there's non-7-bit chars in header,
         # or under Py2 or Py3 where decoding with the coding hint fails.