Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.
Python cliconsumer-gpudpofine-tuningggufhuggingfacellmllmopslocal-ailocal-llmloralow-vrammachine-learningollamapeftpythonpytorchqlorasfttransformers
fix(terminal): strip bidi controls and invisible separators in for_terminal (#1861) (#1953)
Aaditya Desai47cb5612fd37
diff --git a/changelog.d/0.76.0/1861.security.md b/changelog.d/0.76.0/1861.security.mdnew file mode 100644index 00000000..6faf5d34--- /dev/null+++ b/changelog.d/0.76.0/1861.security.md@@ -0,0 +1 @@+- `for_terminal` and `strip_control` now also remove bidi controls (U+202A-U+202E, U+2066-U+2069, U+061C, U+200E, U+200F) and invisible separators (U+200B, U+2028, U+2029, U+FEFF) from dataset, adapter and evidence text before it is printed, so a crafted row can no longer reorder or hide what a preview shows; ZWJ and ZWNJ are kept for Arabic, Persian, Indic and emoji text (#1861).diff --git a/src/soup_cli/utils/terminal.py b/src/soup_cli/utils/terminal.pyindex c18bc075..4da5e778 100644--- a/src/soup_cli/utils/terminal.py+++ b/src/soup_cli/utils/terminal.py@@ -29,9 +29,31 @@ _CONTROL_STRIP_TABLE = {i: None for i in range(0x20) if i not in (0x09, 0x0A, 0x_CONTROL_STRIP_TABLE[0x7F] = None_CONTROL_STRIP_TABLE.update(dict.fromkeys(range(0x80, 0xA0)))+#: Unicode format characters that reorder or hide the text around them (the+#: "Trojan Source" class, CVE-2021-42574): the bidi embeddings, overrides and+#: isolates, the LRM / RLM / ALM marks, zero-width space, the line and+#: paragraph separators, and the byte-order mark. ZWJ (U+200D) and ZWNJ+#: (U+200C) are deliberately NOT here: Arabic, Persian, Indic and emoji+#: sequences need them to render correctly.+_INVISIBLE_FORMAT_CODES = (+ 0x061C,+ 0x200B,+ 0x200E,+ 0x200F,+ 0x2028,+ 0x2029,+ *range(0x202A, 0x202F),+ *range(0x2066, 0x206A),+ 0xFEFF,+)+_CONTROL_STRIP_TABLE.update(dict.fromkeys(_INVISIBLE_FORMAT_CODES))+def strip_control(text: object) -> str:- """Strip C0 (except TAB/LF/CR), DEL and C1 controls; leave Rich markup alone."""+ """Strip C0 (except TAB/LF/CR), DEL, C1 and bidi/invisible format characters.++ ZWJ and ZWNJ are kept. Rich markup is left alone.+ """return str(text).translate(_CONTROL_STRIP_TABLE)diff --git a/tests/test_terminal_c1_controls.py b/tests/test_terminal_c1_controls.pyindex 1e59cf72..706e2c5b 100644--- a/tests/test_terminal_c1_controls.py+++ b/tests/test_terminal_c1_controls.py@@ -213,3 +213,67 @@ class TestManifestAuthorRefusesControlCharacters:out = buffer.getvalue()assert "Cannot inspect can" in out and "author" in out, outassert _c1_found(out) == [], repr(out)+++BIDI_AND_INVISIBLE_CODES = (+ 0x061C,+ 0x200B,+ 0x200E,+ 0x200F,+ 0x2028,+ 0x2029,+ 0x202A,+ 0x202B,+ 0x202C,+ 0x202D,+ 0x202E,+ 0x2066,+ 0x2067,+ 0x2068,+ 0x2069,+ 0xFEFF,+)+++class TestTheHelpersStripBidiAndInvisibleFormatCharacters:+ """Bidi controls and invisible separators reorder or hide what a preview shows."""++ @pytest.mark.parametrize(+ "code", BIDI_AND_INVISIBLE_CODES, ids=[f"{code:#06x}" for code in BIDI_AND_INVISIBLE_CODES]+ )+ def test_every_listed_code_point_is_stripped(self, code: int) -> None:+ from soup_cli.utils.terminal import for_terminal, strip_control++ assert strip_control(f"a{chr(code)}b") == "ab"+ assert for_terminal(f"a{chr(code)}b") == "ab"++ def test_a_right_to_left_override_cannot_reorder_the_text(self) -> None:+ from soup_cli.utils.terminal import for_terminal++ assert for_terminal("say " + chr(0x202E) + "evil" + chr(0x202C)) == "say evil"++ @pytest.mark.parametrize("code", [0x200C, 0x200D], ids=["zwnj", "zwj"])+ def test_zero_width_joiners_are_kept(self, code: int) -> None:+ from soup_cli.utils.terminal import for_terminal, strip_control++ assert strip_control(f"a{chr(code)}b") == f"a{chr(code)}b"+ assert for_terminal(f"a{chr(code)}b") == f"a{chr(code)}b"++ def test_a_zwj_emoji_sequence_is_left_intact(self) -> None:+ from soup_cli.utils.terminal import for_terminal++ family = chr(0x1F468) + chr(0x200D) + chr(0x1F469) + chr(0x200D) + chr(0x1F467)+ assert for_terminal(family) == family++ def test_a_persian_word_with_zwnj_is_left_intact(self) -> None:+ from soup_cli.utils.terminal import for_terminal++ codes = (0x645, 0x6CC, 0x200C, 0x62E, 0x648, 0x627, 0x647, 0x645)+ word = "".join(chr(code) for code in codes)+ assert for_terminal(word) == word++ @pytest.mark.parametrize("code", [0x200A, 0x2030, 0x2060, 0x2065, 0x206A], ids=hex)+ def test_neighbouring_code_points_are_not_swept_up(self, code: int) -> None:+ from soup_cli.utils.terminal import strip_control++ assert strip_control(f"a{chr(code)}b") == f"a{chr(code)}b"