From 36ca6fa5a78433d26ca8adea622afce56cc15bf7 Mon Sep 17 00:00:00 2001 From: Nicolas Iooss Date: Fri, 2 Jun 2023 21:04:55 +0000 Subject: [PATCH] Make defines.is_printable support bytes `uefi_search.py` calls `is_printable` with bytes, in: m = re.compile(bytes(rule['regexp'], 'utf-8')).search(efi.Image) if m: match_result |= MATCH_REGEXP _str = m.group(0) hexver = binascii.hexlify(_str) printver = f" ('{_str}')" if defines.is_printable(_str) else '' ... because `_str` has actually type `bytes`, not `str`. When `is_printable` is called with bytes, it always return `False`, because in set(seq).issubset(set(string.printable)) `set(seq)` is a set of integers and `set(string.printable)` is a set of strings. Fix this by always converting the parameter to string, using `bytestostring`. This is not the most efficient way of doing this (a more efficient would be `set(seq).issubset(set(string.printable.encode()))` with some caching of the second set) but it is simple and makes caller less likely to use the function in an unsupported way. Signed-off-by: Nicolas Iooss --- chipsec/defines.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chipsec/defines.py b/chipsec/defines.py index 3e3ce848..227b5d69 100644 --- a/chipsec/defines.py +++ b/chipsec/defines.py @@ -197,8 +197,8 @@ def os_version() -> Tuple[str, str, str, str]: return platform.system(), platform.release(), platform.version(), platform.machine() -def is_printable(seq) -> bool: - return set(seq).issubset(set(string.printable)) +def is_printable(seq: AnyStr) -> bool: + return set(bytestostring(seq)).issubset(set(string.printable)) def is_hex(maybe_hex: Iterable) -> bool: