Arize-ai/phoenix
Backslashes not escaped in sanitizePythonStr produce invalid/corrupted Python string literals
Summary
Context: The
sanitizePythonStrfunction inapp/src/utils/pythonUtils.tsconverts JavaScript strings to Python string literals for generating metadata filter conditions.Bug: The function fails to escape backslash characters when sanitizing strings.
Actual vs. expected: Backslashes are passed through unescaped (e.g.,
C:\\Users\\test→"C:\\Users\\test"), when they should be doubled (expected:"C:\\\\Users\\\\test").Impact: The function generates invalid Python code that raises SyntaxError or corrupts data through unintended escape sequence interpretation.
Code with bug
Evidence
The bug manifests in three distinct failure modes:
Test 1: Windows file path with \\U sequence
Test 2: Literal backslash-n interpreted as newline
Test 3: Backslash before quote causes unterminated string
Correct implementation
Recommended fix
Escape backslashes before other characters to prevent interference with subsequent escape sequences:
The order is critical: if backslashes are escaped after newlines, a literal \\n in the input would become \\\\n, then incorrectly become \\\\\\n.