Replace control characters to prevent TSV parse breakage and terminal injection.
(value: str)
| 159 | |
| 160 | |
| 161 | def sanitize_tsv(value: str) -> str: |
| 162 | """Replace control characters to prevent TSV parse breakage and terminal injection.""" |
| 163 | # Replace common whitespace control chars with space, strip all other C0 controls |
| 164 | result = value.replace("\t", " ").replace("\n", " ").replace("\r", " ") |
| 165 | # Remove remaining C0 control characters (0x00-0x1F except already handled) and DEL (0x7F) |
| 166 | return result.translate(str.maketrans("", "", "".join(chr(c) for c in range(0x20) if chr(c) not in " ") + "\x7f")) |
| 167 | |
| 168 | |
| 169 | # Keep alias for backward compatibility |
no outgoing calls