Commit e1def5c5 by Adam Palay

gracefully fail when parsing xml with non-compatible string (TNL-347)

parent e00b666d
...@@ -2111,7 +2111,13 @@ class CodeResponse(LoncapaResponse): ...@@ -2111,7 +2111,13 @@ class CodeResponse(LoncapaResponse):
except etree.XMLSyntaxError as _err: except etree.XMLSyntaxError as _err:
# If `html` contains attrs with no values, like `controls` in <audio controls src='smth'/>, # If `html` contains attrs with no values, like `controls` in <audio controls src='smth'/>,
# XML parser will raise exception, so wee fallback to html5parser, which will set empty "" values for such attrs. # XML parser will raise exception, so wee fallback to html5parser, which will set empty "" values for such attrs.
parsed = html5lib.parseFragment(msg, treebuilder='lxml', namespaceHTMLElements=False) try:
parsed = html5lib.parseFragment(msg, treebuilder='lxml', namespaceHTMLElements=False)
except ValueError:
# the parsed message might contain strings that are not
# xml compatible, in which case, throw the error message
parsed = False
if not parsed: if not parsed:
log.error("Unable to parse external grader message as valid" log.error("Unable to parse external grader message as valid"
" XML: score_msg['msg']=%s", msg) " XML: score_msg['msg']=%s", msg)
......
...@@ -1006,6 +1006,7 @@ class CodeResponseTest(ResponseTest): ...@@ -1006,6 +1006,7 @@ class CodeResponseTest(ResponseTest):
invalid_grader_msgs = [ invalid_grader_msgs = [
'<audio', # invalid XML and HTML5 '<audio', # invalid XML and HTML5
'<p>\b</p>', # invalid special character
] ]
answer_ids = sorted(self.problem.get_question_answers()) answer_ids = sorted(self.problem.get_question_answers())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment