Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

harden(save): sanitize --save-suffix into a path-safe token (#736)

nate-avrae21e82e3f452
diff --git a/changelog.d/736.security.md b/changelog.d/736.security.md
new file mode 100644
index 00000000..897818f1
--- /dev/null
+++ b/changelog.d/736.security.md
@@ -0,0 +1 @@
+`--save-suffix` is sanitized to a path-safe token so traversal fragments cannot escape the save directory (including discover-mode saves).
diff --git a/skills/last30days/scripts/last30days.py b/skills/last30days/scripts/last30days.py
index a3dec51b..d13c03f6 100644
--- a/skills/last30days/scripts/last30days.py
+++ b/skills/last30days/scripts/last30days.py
@@ -281,6 +281,19 @@ def slugify(value: str, max_length: int = 180) -> str:
return slug or "last30days"
+def sanitize_suffix(suffix: str) -> str:
+ """Sanitize a user-provided ``--save-suffix`` into a path-safe token.
+
+ The suffix is glued directly into the saved-report filename, so restrict it
+ to the same ``[a-z0-9-]`` class as the topic slug. This neutralizes path
+ separators and parent refs (``/``, ``..``) so a suffix can never escape the
+ save directory, while leaving ordinary values ('v3', 'gemini', a client
+ slug) unchanged. Unlike ``slugify`` there is no fallback token: a suffix
+ that sanitizes to nothing simply drops, yielding no suffix part.
+ """
+ return re.sub(r"[^a-z0-9]+", "-", suffix.lower()).strip("-")
+
+
def _report_has_private_corpus(report: schema.Report) -> bool:
items_by_source = getattr(report, "items_by_source", {})
if isinstance(items_by_source, dict) and items_by_source.get("corpus"):
@@ -327,7 +340,8 @@ def save_output(
slug = slugify(topic_override or report.topic)
extension = "json" if emit == "json" else "html" if emit == "html" else "md"
raw_label = "raw-html" if emit == "html" else "raw"
- suffix_part = f"-{suffix}" if suffix else ""
+ safe_suffix = sanitize_suffix(suffix)
+ suffix_part = f"-{safe_suffix}" if safe_suffix else ""
base = path / f"{slug}-{raw_label}{suffix_part}.{extension}"
date_str = datetime.now().strftime('%Y-%m-%d')
candidates = [base]
@@ -578,7 +592,8 @@ def compute_save_path_display(save_dir: str, topic: str, suffix: str, emit: str)
slug = slugify(topic)
extension = "json" if emit == "json" else "html" if emit == "html" else "md"
raw_label = "raw-html" if emit == "html" else "raw"
- suffix_part = f"-{suffix}" if suffix else ""
+ safe_suffix = sanitize_suffix(suffix)
+ suffix_part = f"-{safe_suffix}" if safe_suffix else ""
raw = path / f"{slug}-{raw_label}{suffix_part}.{extension}"
try:
home = _Path.home().resolve()
@@ -1657,7 +1672,8 @@ def _save_discovery_output(
directory = Path(save_dir).expanduser().resolve()
directory.mkdir(parents=True, exist_ok=True)
extension = "json" if emit == "json" else "md"
- suffix_part = f"-{suffix}" if suffix else ""
+ safe_suffix = sanitize_suffix(suffix)
+ suffix_part = f"-{safe_suffix}" if safe_suffix else ""
stem = f"{slugify(domain)}-discover-raw{suffix_part}"
date_str = datetime.datetime.now().strftime("%Y-%m-%d")
candidates = [directory / f"{stem}.{extension}", directory / f"{stem}-{date_str}.{extension}"]
diff --git a/tests/test_html_render.py b/tests/test_html_render.py
index 6e1344e4..c7655c53 100644
--- a/tests/test_html_render.py
+++ b/tests/test_html_render.py
@@ -369,6 +369,35 @@ class HtmlCliIntegrationTests(unittest.TestCase):
with self.subTest("suffix"):
path = cli.compute_save_path_display("/tmp", report.topic, "v3", "html")
self.assertTrue(path.endswith("/ai-agent-frameworks-raw-html-v3.html"))
+ with self.subTest("suffix_traversal_sanitized"):
+ # A suffix carrying path separators / parent refs must be sanitized
+ # to a flat token so it can never escape the save directory.
+ path = cli.compute_save_path_display("/tmp", report.topic, "../../etc", "html")
+ self.assertNotIn("..", path)
+ self.assertTrue(path.endswith("/ai-agent-frameworks-raw-html-etc.html"))
+
+ def test_save_suffix_cannot_escape_save_directory(self):
+ report = _report("AI Agent Frameworks", [])
+ with tempfile.TemporaryDirectory() as tmpdir:
+ out = cli.save_output(report, "md", tmpdir, suffix="../../ESCAPED")
+ # The written file stays inside the intended directory, and the
+ # traversal fragment never lands in the filename.
+ self.assertEqual(Path(tmpdir).resolve(), out.parent)
+ self.assertNotIn("..", out.name)
+ self.assertTrue(out.name.startswith("ai-agent-frameworks-raw"))
+
+ def test_discover_save_suffix_cannot_escape_save_directory(self):
+ with tempfile.TemporaryDirectory() as tmpdir:
+ out = cli._save_discovery_output(
+ "# discovery\n",
+ domain="AI agents",
+ emit="md",
+ save_dir=tmpdir,
+ suffix="../../ESCAPED",
+ )
+ self.assertEqual(Path(tmpdir).resolve(), out.parent)
+ self.assertNotIn("..", out.name)
+ self.assertTrue(out.name.startswith("ai-agents-discover-raw"))
def test_save_output_can_persist_comparison_html(self):
reports = [