|
17 | 17 | _CONTROL_CHARS = re.compile(r"[\x00-\x08\x0b-\x1f\x7f]") |
18 | 18 |
|
19 | 19 |
|
20 | | -def _sanitize_for_display(text: str) -> str: |
21 | | - """Strip control characters so untrusted text cannot inject ANSI escapes. |
22 | | -
|
23 | | - Applied to every ``show_file``-derived string that reaches the terminal: |
24 | | - the displayed path, each file line, and the read-error notice. |
25 | | - """ |
26 | | - return _CONTROL_CHARS.sub("", text) |
27 | | - |
28 | | - |
29 | 20 | class GateStep(StepBase): |
30 | 21 | """Interactive review gate. |
31 | 22 |
|
@@ -112,9 +103,9 @@ def _compose_prompt(cls, message: object, show_file: str | None) -> str: |
112 | 103 | text = str(message) |
113 | 104 | if not show_file: |
114 | 105 | return text |
115 | | - # The path is opened with the original value but displayed sanitized, |
| 106 | + # The path is opened with the original value but displayed stripped, |
116 | 107 | # so a path that itself contains escapes cannot spoof the terminal. |
117 | | - header = f"{_sanitize_for_display(show_file)}:" |
| 108 | + header = f"{_CONTROL_CHARS.sub('', show_file)}:" |
118 | 109 | body = "\n".join( |
119 | 110 | [header, *(f" {line}" for line in cls._read_show_file(show_file))] |
120 | 111 | ) |
@@ -170,10 +161,10 @@ def _read_show_file(show_file: str) -> list[str]: |
170 | 161 | if len(lines) >= GateStep.MAX_SHOW_FILE_LINES: |
171 | 162 | truncated = True |
172 | 163 | break |
173 | | - lines.append(_sanitize_for_display(line.rstrip("\n"))) |
| 164 | + lines.append(_CONTROL_CHARS.sub("", line.rstrip("\n"))) |
174 | 165 | except (OSError, UnicodeDecodeError, ValueError) as exc: |
175 | | - # ``exc`` echoes the (possibly hostile) path, so sanitize it too. |
176 | | - return [_sanitize_for_display(f"(could not read file: {exc})")] |
| 166 | + # ``exc`` echoes the (possibly hostile) path, so strip it too. |
| 167 | + return [_CONTROL_CHARS.sub("", f"(could not read file: {exc})")] |
177 | 168 | if not lines and not truncated: |
178 | 169 | return ["(file is empty)"] |
179 | 170 | if truncated: |
|
0 commit comments