Skip to content

Commit

Permalink
sysfs: always sanitize values
Browse files Browse the repository at this point in the history
  • Loading branch information
thiell committed Nov 8, 2024
1 parent c23b7c1 commit 5cf1221
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
11 changes: 2 additions & 9 deletions sasutils/cli/sas_discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@
def format_attrs(attrlist, attrs):
"""filter keys to avoid SysfsObject cache miss on all attrs"""
attr_fmt = ('%s: {%s}' % t for t in attrlist)
# sanitize values for display
iargs = dict((k, attrs.get(k, 'N/A') \
.strip('\x00') \
.encode('ascii', errors='replace') \
.decode()) for _, k in attrlist)
iargs = dict((k, attrs.get(k, 'N/A')) for _, k in attrlist)
return ', '.join(attr_fmt).format(**iargs)


Expand Down Expand Up @@ -199,10 +195,7 @@ def __str__(self):
'version_fw')

# sanitize values for display
iargs = dict((k, self.baseobj.scsi_host.attrs.get(k, 'N/A') \
.strip('\x00') \
.encode('ascii', errors='replace') \
.decode())
iargs = dict((k, self.baseobj.scsi_host.attrs.get(k, 'N/A'))
for k in ikeys)

return '%s %s' % (self.name, ', '.join(info_fmt).format(**iargs))
Expand Down
9 changes: 7 additions & 2 deletions sasutils/sysfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

SYSFS_ROOT = '/sys'

# Some VPDs contain weird characters...
def sanitize_sysfs_value(value):
return value.strip('\x00').encode('ascii', errors='replace').decode()


class SysfsNode(object):
def __init__(self, path=None):
Expand Down Expand Up @@ -121,7 +125,7 @@ def get(self, pathname, default=None, ignore_errors=False, printable=True,
raise KeyError('Not found: %s' % path)
result = default

return result
return sanitize_sysfs_value(result)

def readlink(self, pathname, default=None, absolute=False):
if absolute:
Expand Down Expand Up @@ -177,7 +181,7 @@ def load(self):
# The next five methods are requirements of the ABC.

def __setitem__(self, key, value):
self.values[key] = value
self.values[key] = sanitize_sysfs_value(value)

def get(self, key, default=None):
if not self.values.__contains__(key):
Expand All @@ -189,6 +193,7 @@ def get(self, key, default=None):
else:
raise AttributeError("%r object has no attribute %r" %
(self.__class__.__name__, key))

return self.values[key]

def __getitem__(self, key):
Expand Down

0 comments on commit 5cf1221

Please sign in to comment.