Skip to content

Commit

Permalink
Fix insufficient validation for gremlin parser allowing any xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexeh committed Feb 29, 2024
1 parent 24bd8f5 commit 94f6626
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 94f6626

Please sign in to comment.