Skip to content

armgit / Soup

Public Imported from GitHub
0

Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.

fix(data): sanitize dataset preview terminal output (#1822)

Emre K.1f7931217166
diff --git a/changelog.d/0.76.0/1822.fixed.md b/changelog.d/0.76.0/1822.fixed.md
new file mode 100644
index 00000000..2f281373
--- /dev/null
+++ b/changelog.d/0.76.0/1822.fixed.md
@@ -0,0 +1 @@
+- Sanitize dataset previews, review panels, paths, and image format labels before terminal rendering. (#1812 by @kocaemre in #1822)
diff --git a/src/soup_cli/commands/data.py b/src/soup_cli/commands/data.py
index b209acb7..a2a8b723 100644
--- a/src/soup_cli/commands/data.py
+++ b/src/soup_cli/commands/data.py
@@ -37,10 +37,10 @@ def inspect(
"""Inspect a dataset: show stats and sample rows."""
file_path = Path(path)
if not file_path.exists():
- console.print(f"[red]File not found: {file_path}[/]")
+ console.print(f"[red]File not found: {for_terminal(file_path)}[/]")
raise typer.Exit(1)
- console.print(f"[dim]Inspecting {file_path}...[/]\n")
+ console.print(f"[dim]Inspecting {for_terminal(file_path)}...[/]\n")
data = load_raw_data(file_path)
result = validate_and_stats(data)
@@ -63,18 +63,15 @@ def inspect(
# Print sample rows
if rows > 0 and len(data) > 0:
- # Escape dataset-derived cell content + column names: a stray '[/]' in
- # ordinary data crashes Rich with MarkupError; a crafted '[link=...]'
- # renders a phishing hyperlink. Mirrors `soup data review`.
- from rich.markup import escape as _escape
+ # Sanitize dataset-derived cells and column names before Rich renders them.
console.print(f"\n[bold]Sample rows ({min(rows, len(data))}):[/]")
sample_table = Table(show_lines=True)
for col in result["columns"][:5]: # max 5 columns
- sample_table.add_column(_escape(str(col)), max_width=60)
+ sample_table.add_column(for_terminal(col), max_width=60)
for row in data[: min(rows, len(data))]:
values = [
- _escape(str(row.get(col, ""))[:60])
+ for_terminal(str(row.get(col, ""))[:60])
for col in result["columns"][:5]
]
sample_table.add_row(*values)
@@ -728,7 +725,7 @@ def _show_vision_stats(data: list[dict], image_dir: Path | None = None) -> None:
ext_str = ", ".join(
f"{ext} ({count})" for ext, count in sorted(extensions.items())
)
- vision_table.add_row("Image formats", ext_str)
+ vision_table.add_row("Image formats", for_terminal(ext_str))
console.print(vision_table)
@@ -2259,7 +2256,6 @@ def review_cmd(
"""Preview preference pairs for manual review."""
import json as _json
- from rich.markup import escape as _escape
from rich.panel import Panel
sample = max(1, min(int(sample), 100))
@@ -2284,9 +2280,9 @@ def review_cmd(
chosen = str(entry.get("chosen", ""))
rejected = str(entry.get("rejected", ""))
console.print(Panel(
- f"[bold cyan]Prompt:[/] {_escape(prompt[:400])}\n\n"
- f"[green]Chosen:[/] {_escape(chosen[:400])}\n\n"
- f"[red]Rejected:[/] {_escape(rejected[:400])}",
+ f"[bold cyan]Prompt:[/] {for_terminal(prompt[:400])}\n\n"
+ f"[green]Chosen:[/] {for_terminal(chosen[:400])}\n\n"
+ f"[red]Rejected:[/] {for_terminal(rejected[:400])}",
title=f"Pair {shown + 1}",
border_style="blue",
))
diff --git a/tests/test_issue1812_data_terminal.py b/tests/test_issue1812_data_terminal.py
new file mode 100644
index 00000000..73f344b9
--- /dev/null
+++ b/tests/test_issue1812_data_terminal.py
@@ -0,0 +1,165 @@
+"""Dataset previews must not expose terminal control bytes from untrusted input."""
+
+import ast
+import json
+import re
+import sys
+from pathlib import Path
+
+import pytest
+from typer.testing import CliRunner
+
+from soup_cli.cli import app
+
+ANSI_SGR = re.compile(r"\x1b\[[0-9;]*m")
+
+
+def assert_no_data_control_bytes(output: str) -> None:
+ # Rich may emit its own SGR styling when FORCE_COLOR=1.
+ unstyled = ANSI_SGR.sub("", output)
+ assert "\x1b" not in unstyled
+ assert "\x07" not in unstyled
+
+
+def test_inspect_sample_rows_strip_control_bytes(tmp_path):
+ dataset = tmp_path / "sample.jsonl"
+ dataset.write_text(
+ json.dumps({"instruction": "hello\x1b]0;title\x07 [bold]world[/]", "output": "ok"}) + "\n",
+ encoding="utf-8",
+ )
+
+ result = CliRunner().invoke(app, ["data", "inspect", str(dataset), "--rows", "1"])
+
+ assert result.exit_code == 0, result.output
+ assert "hello" in result.output
+ assert "world" in result.output
+ assert_no_data_control_bytes(result.output)
+
+
+def test_inspect_sample_header_strips_control_bytes(tmp_path):
+ dataset = tmp_path / "columns.jsonl"
+ dataset.write_text(json.dumps({"col\x1b]0;title\x07": "safe"}) + "\n", encoding="utf-8")
+
+ result = CliRunner().invoke(app, ["data", "inspect", str(dataset), "--rows", "1"])
+
+ assert result.exit_code == 0, result.output
+ assert "safe" in result.output
+ # The separate stats Columns row is handled by #1602; guard the sample header here.
+ assert_no_data_control_bytes(ANSI_SGR.sub("", result.output).split("Sample rows (", 1)[1])
+
+
+def test_review_preview_strips_control_bytes(tmp_path):
+ dataset = tmp_path / "pairs.jsonl"
+ dataset.write_text(
+ json.dumps(
+ {
+ "prompt": "p\x1b]0;x\x07",
+ "chosen": "c\x1b]0;x\x07",
+ "rejected": "r\x1b]0;x\x07",
+ }
+ )
+ + "\n",
+ encoding="utf-8",
+ )
+
+ result = CliRunner().invoke(app, ["data", "review", str(dataset), "--sample", "1"])
+
+ assert result.exit_code == 0, result.output
+ assert "Prompt:" in result.output
+ assert "Chosen:" in result.output
+ assert "Rejected:" in result.output
+ assert_no_data_control_bytes(result.output)
+
+
[email protected](sys.platform == "win32", reason="Windows rejects control bytes in filenames")
+def test_inspect_sanitizes_dataset_path(tmp_path):
+ dataset = tmp_path / "data\x1b]0;title\x07.jsonl"
+ dataset.write_text(json.dumps({"instruction": "hello", "output": "ok"}) + "\n")
+
+ result = CliRunner().invoke(app, ["data", "inspect", str(dataset), "--rows", "0"])
+
+ assert result.exit_code == 0, result.output
+ assert "Inspecting" in result.output
+ assert_no_data_control_bytes(result.output)
+
+
+def test_inspect_sanitizes_missing_dataset_path(tmp_path):
+ dataset = tmp_path / "missing\x1b]0;title\x07.jsonl"
+
+ result = CliRunner().invoke(app, ["data", "inspect", str(dataset)])
+
+ assert result.exit_code == 1
+ assert "File not found" in result.output
+ assert_no_data_control_bytes(result.output)
+
+
+def test_inspect_sanitizes_vision_format(tmp_path):
+ image_name = "image.\x1b]0;title\x07png"
+ dataset = tmp_path / "vision.jsonl"
+ dataset.write_text(json.dumps({"image": image_name, "text": "hello"}) + "\n")
+
+ result = CliRunner().invoke(app, ["data", "inspect", str(dataset), "--rows", "0"])
+
+ assert result.exit_code == 0, result.output
+ assert "Image formats" in result.output
+ assert_no_data_control_bytes(result.output)
+
+
+def test_data_preview_sinks_keep_terminal_sanitizer():
+ """Catch new or reverted unsanitized dataset table/panel sinks in this scope."""
+ source = Path(__file__).resolve().parents[1] / "src/soup_cli/commands/data.py"
+ module = ast.parse(source.read_text(encoding="utf-8"))
+ functions = {node.name: node for node in module.body if isinstance(node, ast.FunctionDef)}
+
+ def calls(function, object_name, method):
+ return [
+ node
+ for node in ast.walk(function)
+ if isinstance(node, ast.Call)
+ and isinstance(node.func, ast.Attribute)
+ and isinstance(node.func.value, ast.Name)
+ and node.func.value.id == object_name
+ and node.func.attr == method
+ ]
+
+ def sanitized(expression):
+ return any(
+ isinstance(node, ast.Call)
+ and isinstance(node.func, ast.Name)
+ and node.func.id == "for_terminal"
+ for node in ast.walk(expression)
+ )
+
+ inspect = functions["inspect"]
+ headers = calls(inspect, "sample_table", "add_column")
+ assert len(headers) == 1 and sanitized(headers[0].args[0])
+ values = [
+ node
+ for node in ast.walk(inspect)
+ if isinstance(node, ast.ListComp)
+ and any(isinstance(child, ast.Name) and child.id == "row" for child in ast.walk(node))
+ ]
+ assert len(values) == 1 and sanitized(values[0].elt)
+ panels = [
+ node
+ for node in ast.walk(functions["review_cmd"])
+ if isinstance(node, ast.Call)
+ and isinstance(node.func, ast.Name)
+ and node.func.id == "Panel"
+ ]
+ assert len(panels) == 1
+ assert (
+ sum(
+ isinstance(node, ast.Call)
+ and isinstance(node.func, ast.Name)
+ and node.func.id == "for_terminal"
+ for node in ast.walk(panels[0].args[0])
+ )
+ == 3
+ )
+ formats = [
+ call
+ for call in calls(functions["_show_vision_stats"], "vision_table", "add_row")
+ if isinstance(call.args[0], ast.Constant) and call.args[0].value == "Image formats"
+ ]
+ assert len(formats) == 1 and sanitized(formats[0].args[1])