Skip to content

Commit

Permalink
Fix class DBFilter().output() to pass its unit test with Python3
Browse files Browse the repository at this point in the history
- Fix Element() takes at least 1 argument (0 given)
- Fix for sax.parseString() arg: TypeError: a bytes-like object is required, not 'str'
- Fix NameError: name 'unicode' is not defined when converting unicode
- Decode bytes to str before using `re` to matching using str patterns

Signed-off-by: Bernhard Kaindl <[email protected]>
  • Loading branch information
bernhardkaindl committed Dec 22, 2023
1 parent 6397254 commit 82b8121
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions xen-bugtool
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@ import defusedxml.sax
if sys.version_info.major == 2:
from commands import getoutput # pyright: ignore[reportMissingImports]
from urllib import urlopen # type:ignore[attr-defined]

unicode_type = unicode # pyright:ignore[reportUndefinedVariable] # pylint: disable=unicode-builtin
else:
from subprocess import getoutput
from urllib.request import urlopen

unicode_type = str

# Fixed in 3.7: https://github.com/python/cpython/pull/12628
# Monkey-patch zipfile's __del__ function to be less stupid
# Specifically, it calls close which further writes to the file, which
Expand Down Expand Up @@ -474,7 +478,7 @@ data = {}
dev_null = open('/dev/null', 'r+')

def no_unicode(x):
if isinstance(x, unicode):
if isinstance(x, unicode_type):
return x.encode('utf-8')
return x

Expand Down Expand Up @@ -1344,7 +1348,7 @@ class XapiDBContentHandler(xml.sax.ContentHandler):
if name == "row":
self._filter(attrs._attrs)

element = Element(tag=name, attrib=attrs._attrs)
element = Element(name, attrib=attrs._attrs)
self.elements_stack.append(element)

def endElement(self, name):
Expand All @@ -1370,11 +1374,12 @@ class DBFilter:
# remove values for any keys containing the word 'password'
# example input snippet to filter:
# '%.(\'incoming_chappassword\'%.\'TC12818outgoingpasswd\'))" host="Opaq'
raw_xml = raw_xml if isinstance(raw_xml, str) else raw_xml.decode()
nopass_xml = re.sub(r"\('(\w*(?:password)\w*)'%\.'\w*'\)", r"('\1'%.'REMOVED')", raw_xml)

self.content_handler = XapiDBContentHandler()
try:
defusedxml.sax.parseString(nopass_xml, self.content_handler)
defusedxml.sax.parseString(no_unicode(nopass_xml), self.content_handler)
except xml.sax._exceptions.SAXParseException:
pass # parse command output line by line, first line just headers

Expand All @@ -1388,7 +1393,7 @@ def filter_db_pii(s, state):

clipboard_match = re.compile(r'^/local/domain/(\d+)/data/((set)|(report))_clipboard')
def filter_xenstore_secrets(s, state):
match = clipboard_match.search(s)
match = clipboard_match.search(s.decode())
if match:
return '/local/domain/%s/data/%s_clipboard = <filtered for security>\n' % (match.group(1), match.group(2))
else:
Expand Down

0 comments on commit 82b8121

Please sign in to comment.