Skip to content

Commit ead174c

Browse files
doquanghuyclaude
andcommitted
refactor(workflows): inline control-char stripping, drop the helper
Reuse the existing _CONTROL_CHARS regex directly at the three display sites instead of wrapping it in a one-line helper. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 4f6f843 commit ead174c

1 file changed

Lines changed: 5 additions & 14 deletions

File tree

src/specify_cli/workflows/steps/gate/__init__.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@
1717
_CONTROL_CHARS = re.compile(r"[\x00-\x08\x0b-\x1f\x7f]")
1818

1919

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-
2920
class GateStep(StepBase):
3021
"""Interactive review gate.
3122
@@ -112,9 +103,9 @@ def _compose_prompt(cls, message: object, show_file: str | None) -> str:
112103
text = str(message)
113104
if not show_file:
114105
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,
116107
# 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)}:"
118109
body = "\n".join(
119110
[header, *(f" {line}" for line in cls._read_show_file(show_file))]
120111
)
@@ -170,10 +161,10 @@ def _read_show_file(show_file: str) -> list[str]:
170161
if len(lines) >= GateStep.MAX_SHOW_FILE_LINES:
171162
truncated = True
172163
break
173-
lines.append(_sanitize_for_display(line.rstrip("\n")))
164+
lines.append(_CONTROL_CHARS.sub("", line.rstrip("\n")))
174165
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})")]
177168
if not lines and not truncated:
178169
return ["(file is empty)"]
179170
if truncated:

0 commit comments

Comments
 (0)