From b85cd306aae65a8903faad972531a8ff4a87a412 Mon Sep 17 00:00:00 2001 From: "J. Patrick Avery, Jr" Date: Tue, 7 Feb 2017 16:09:41 -0700 Subject: [PATCH] tools.py: protect against crazy headers --- tools/moth.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tools/moth.py b/tools/moth.py index ae16cc7..428b396 100644 --- a/tools/moth.py +++ b/tools/moth.py @@ -4,6 +4,7 @@ import contextlib import glob import io import importlib.machinery +import logging import mistune import os import random @@ -95,9 +96,13 @@ class Puzzle: if not line: header = False continue - key, val = line.split(':', 1) - key = key.lower() - val = val.strip() + try: + key, val = line.split(':', 1) + key = key.lower() + val = val.strip() + except ValueError: + raise ValueError("Invalid header line: [%s]" % (line)) + if key == 'author': self.authors.append(val) elif key == 'summary': @@ -116,7 +121,8 @@ class Puzzle: pass self.files[name] = PuzzleFile(stream, name, not hidden) else: - raise ValueError("Unrecognized header field: {}".format(key)) + raise ValueError( + "Unrecognized header field: {}".format(key)) else: self.body.write(line) @@ -293,6 +299,7 @@ class Category: with pushd(self.path): self.catmod.make(points, puzzle) else: + logging.info("category.puzzle: %d" % (points)) puzzle.read_directory(path) return puzzle