relax printable checks (#21)

* relax printable checks

* format
This commit is contained in:
Riccardo Balbo 2025-12-23 10:15:30 +01:00 • committed by GitHub
commit 8c14c6fac8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,6 +2,12 @@
from loguru import logger from loguru import logger
ENABLE_HARDENING = True ENABLE_HARDENING = True
WHITELISTED_NON_PRINTABLE_CHARS = {
"\n", # newline
"\r", # carriage return
"\t", # tab
"\xa0", # non-breaking space ( )
}
def panic(reason: str): def panic(reason: str):
@ -17,8 +23,17 @@ def assert_printable(v: str):
return return
if not isinstance(v, str): if not isinstance(v, str):
panic("not a string " + str(v)) panic("not a string " + str(v))
if not v.isprintable(): for ch in v:
panic("string contains non-printable characters") # check if printable
if ch.isprintable():
continue
# check if whitelisted non-printable
if ch in WHITELISTED_NON_PRINTABLE_CHARS:
continue
# Anything else is rejected
panic(f"string contains non-printable character: (0x{ord(ch):04X})")
# Check if number is valid int and not NaN # Check if number is valid int and not NaN