don't crash and burn if someone sends something that's not LDraw

Wed, 31 Jan 2018 14:50:19 +0200

author
Santeri Piippo
date
Wed, 31 Jan 2018 14:50:19 +0200
changeset 41
4d87bc126368
parent 40
9200664daf87
child 42
be50d98d3668

don't crash and burn if someone sends something that's not LDraw

parse.py file | annotate | diff | comparison | revisions
webfront.py file | annotate | diff | comparison | revisions
--- a/parse.py	Wed Jan 31 14:45:31 2018 +0200
+++ b/parse.py	Wed Jan 31 14:50:19 2018 +0200
@@ -8,7 +8,10 @@
 
 def parse_ldraw_code(line):
     if isinstance(line, bytes):
-        line = line.decode()
+        try:
+            line = line.decode()
+        except UnicodeDecodeError:
+            raise BadLdrawLine("bad unicode")
     line = line.strip()
     if not line:
         return linetypes.EmptyLine()
--- a/webfront.py	Wed Jan 31 14:45:31 2018 +0200
+++ b/webfront.py	Wed Jan 31 14:50:19 2018 +0200
@@ -1,5 +1,5 @@
 #!/usr/bin/env python3
-from flask import Flask, render_template, redirect, request
+from flask import Flask, render_template, redirect, request, abort
 from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths
 from ldcheck import read_ldraw
 from testsuite import load_tests, check_model, problem_text
@@ -17,11 +17,14 @@
         for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
             with ldconfig_ldr_path.open() as ldconfig_ldr:
                 load_colours(ldconfig_ldr)
-        model = read_ldraw(
-            file.stream,
-            name = file.filename,
-            config = config,
-        )
+        try:
+            model = read_ldraw(
+                file.stream,
+                name = file.filename,
+                config = config,
+            )
+        except:
+            abort(400)
         test_suite = load_tests()
         report = check_model(model, test_suite)
 

mercurial