From 94f66260b8d24655556bb52901b6942d9695211f Mon Sep 17 00:00:00 2001 From: Rob <5183487+Rexeh@users.noreply.github.com> Date: Thu, 29 Feb 2024 15:43:19 +0000 Subject: [PATCH] Fix insufficient validation for gremlin parser allowing any xml --- .../joystick_gremlin.py | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/joystick_diagrams/plugins/joystick_gremlin_plugin/joystick_gremlin.py b/joystick_diagrams/plugins/joystick_gremlin_plugin/joystick_gremlin.py index 2fc86f1..411cdee 100644 --- a/joystick_diagrams/plugins/joystick_gremlin_plugin/joystick_gremlin.py +++ b/joystick_diagrams/plugins/joystick_gremlin_plugin/joystick_gremlin.py @@ -7,6 +7,7 @@ from typing import Union from xml.dom import minidom +from joystick_diagrams.exceptions import JoystickDiagramsError from joystick_diagrams.input.button import Button from joystick_diagrams.input.hat import Hat, HatDirection from joystick_diagrams.input.profile_collection import ProfileCollection @@ -30,9 +31,22 @@ def __init__(self, filepath: Path): self.file = self.parse_xml_file(filepath) def parse_xml_file(self, xml_file: Path) -> minidom.Document: - file_path = xml_file.__str__() - ## TODO Improve loading of file, checks for validity etc - return minidom.parse(file_path) + file_path = str(xml_file) + parsed_xml = minidom.parse(file_path) + + valid = self.validate_xml(parsed_xml) + + if valid: + return parsed_xml + + raise JoystickDiagramsError("File was not a valid Joystick Gremlin XML") + + def validate_xml(self, data: minidom.Document) -> bool: + """Very basic check for validity""" + if len(data.getElementsByTagName("mode")) > 0: + return True + else: + return False def create_dictionary(self) -> ProfileCollection: """Creates a valid ProfileCollection from Joystick Gremlin XML