Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

fix(python-sdk): strip colon-separated SGR escape codes in build logs (#1522)

Mish Ushakova6b1cf4bcf89
diff --git a/.changeset/python-strip-ansi-colon.md b/.changeset/python-strip-ansi-colon.md
new file mode 100644
index 000000000..77f1bf060
--- /dev/null
+++ b/.changeset/python-strip-ansi-colon.md
@@ -0,0 +1,11 @@
+---
+"@e2b/python-sdk": patch
+---
+
+fix(python-sdk): strip colon-separated SGR escape codes in build logs
+
+`strip_ansi_escape_codes` now strips CSI parameters that use colons as well as
+semicolons, matching the JavaScript SDK's `stripAnsi`. Modern terminals emit
+colon-separated SGR sequences (e.g. 256-color `\x1b[38:5:82m`, truecolor
+`\x1b[38:2::r:g:bm`, and curly underlines `\x1b[4:3m`), which previously leaked
+literal escape garbage into template build log messages on the Python side.
diff --git a/packages/python-sdk/e2b/template/utils.py b/packages/python-sdk/e2b/template/utils.py
index 09a7cfc17..fc54936b1 100644
--- a/packages/python-sdk/e2b/template/utils.py
+++ b/packages/python-sdk/e2b/template/utils.py
@@ -318,7 +318,7 @@ def strip_ansi_escape_codes(text: str) -> str:
st = r"(?:\u0007|\u001B\u005C|\u009C)"
pattern = [
rf"[\u001B\u009B][\[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d/#&.:=?%@~_]*)*)?{st})",
- r"(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))",
+ r"(?:(?:\d{1,4}(?:[;:]\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))",
]
ansi_escape = re.compile("|".join(pattern), re.UNICODE)
return ansi_escape.sub("", text)
diff --git a/packages/python-sdk/tests/shared/template/utils/test_strip_ansi_escape_codes.py b/packages/python-sdk/tests/shared/template/utils/test_strip_ansi_escape_codes.py
new file mode 100644
index 000000000..d0874ad6a
--- /dev/null
+++ b/packages/python-sdk/tests/shared/template/utils/test_strip_ansi_escape_codes.py
@@ -0,0 +1,29 @@
+from e2b.template.utils import strip_ansi_escape_codes
+
+
+def test_strips_basic_sgr_color():
+ assert strip_ansi_escape_codes("\x1b[31mred\x1b[0m") == "red"
+
+
+def test_strips_semicolon_separated_params():
+ assert strip_ansi_escape_codes("\x1b[1;31;42mhi\x1b[0m") == "hi"
+
+
+def test_strips_semicolon_256_color():
+ assert strip_ansi_escape_codes("\x1b[38;5;82mX\x1b[0m") == "X"
+
+
+def test_strips_colon_256_color():
+ assert strip_ansi_escape_codes("\x1b[38:5:82mX\x1b[0m") == "X"
+
+
+def test_strips_colon_truecolor():
+ assert strip_ansi_escape_codes("\x1b[38:2::255:0:0mRED\x1b[0m") == "RED"
+
+
+def test_strips_colon_curly_underline():
+ assert strip_ansi_escape_codes("\x1b[4:3mX\x1b[0m") == "X"
+
+
+def test_leaves_plain_text_unchanged():
+ assert strip_ansi_escape_codes("no escape codes here") == "no escape codes here"