From 8c14c6fac8ec5e0fe8ebf79e919a2f6865f694c0 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Tue, 23 Dec 2025 10:15:30 +0100 Subject: [PATCH] relax printable checks (#21) * relax printable checks * format --- paranoia.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/paranoia.py b/paranoia.py index 5ebca7c..205debe 100644 --- a/paranoia.py +++ b/paranoia.py @@ -2,6 +2,12 @@ from loguru import logger ENABLE_HARDENING = True +WHITELISTED_NON_PRINTABLE_CHARS = { + "\n", # newline + "\r", # carriage return + "\t", # tab + "\xa0", # non-breaking space ( ) +} def panic(reason: str): @@ -17,8 +23,17 @@ def assert_printable(v: str): return if not isinstance(v, str): panic("not a string " + str(v)) - if not v.isprintable(): - panic("string contains non-printable characters") + for ch in v: + # 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