Skip to content

Commit 171abe6

Browse files
views: correctly handle escaping of substitutions
1 parent 7568519 commit 171abe6

1 file changed

Lines changed: 33 additions & 20 deletions

File tree

Evtx/Views.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def to_xml_string(s):
7070
return escape(s)
7171

7272

73-
def render_root_node(root_node, subs):
73+
def render_root_node_with_subs(root_node, subs):
7474
"""
7575
render the given root node using the given substitutions into XML.
7676
@@ -92,6 +92,7 @@ def rec(node, acc):
9292
acc.append(" ")
9393
acc.append(to_xml_string(child.attribute_name().string()))
9494
acc.append("=\"")
95+
# TODO: should use xml.sax.saxutils.quoteattr here
9596
rec(child.attribute_value(), acc)
9697
acc.append("\"")
9798
acc.append(">")
@@ -123,9 +124,23 @@ def rec(node, acc):
123124
elif isinstance(node, e_nodes.TemplateInstanceNode):
124125
raise UnexpectedElementException("TemplateInstanceNode")
125126
elif isinstance(node, e_nodes.NormalSubstitutionNode):
126-
acc.append(subs[node.index()])
127+
sub = subs[node.index()]
128+
129+
if isinstance(sub, e_nodes.BXmlTypeNode):
130+
sub = render_root_node(sub.root())
131+
else:
132+
sub = to_xml_string(sub.string())
133+
134+
acc.append(sub)
127135
elif isinstance(node, e_nodes.ConditionalSubstitutionNode):
128-
acc.append(subs[node.index()])
136+
sub = subs[node.index()]
137+
138+
if isinstance(sub, e_nodes.BXmlTypeNode):
139+
sub = render_root_node(sub.root())
140+
else:
141+
sub = to_xml_string(sub.string())
142+
143+
acc.append(sub)
129144
elif isinstance(node, e_nodes.StreamStartNode):
130145
pass # intended
131146

@@ -135,6 +150,20 @@ def rec(node, acc):
135150
return "".join(acc)
136151

137152

153+
def render_root_node(root_node):
154+
subs = []
155+
for sub in root_node.substitutions():
156+
if isinstance(sub, six.string_types):
157+
raise RuntimeError('string sub?')
158+
159+
if sub is None:
160+
raise RuntimeError('null sub?')
161+
162+
subs.append(sub)
163+
164+
return render_root_node_with_subs(root_node, subs)
165+
166+
138167
def evtx_record_xml_view(record, cache=None):
139168
'''
140169
render the given record into an XML document.
@@ -145,23 +174,7 @@ def evtx_record_xml_view(record, cache=None):
145174
Returns:
146175
str: the rendered XML document.
147176
'''
148-
def rec(root_node):
149-
subs = []
150-
for sub in root_node.substitutions():
151-
if isinstance(sub, six.string_types):
152-
raise RuntimeError('string sub?')
153-
154-
if sub is None:
155-
raise RuntimeError('null sub?')
156-
157-
if isinstance(sub, e_nodes.BXmlTypeNode):
158-
subs.append(rec(sub.root()))
159-
else:
160-
subs.append(sub.string())
161-
162-
return render_root_node(root_node, subs)
163-
164-
return rec(record.root())
177+
return render_root_node(record.root())
165178

166179

167180
def evtx_chunk_xml_view(chunk):

0 commit comments

Comments
 (0)