Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions boundier/storage/sqlite_store.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import sqlite3
import logging
from datetime import datetime
Expand All @@ -18,6 +19,12 @@ def _get_conn(self):
conn.row_factory = sqlite3.Row
return conn

@staticmethod
def _safe_name(name: str) -> str:
"""Sanitize a channel name for safe use as a filename (prevents path traversal)."""
cleaned = re.sub(r"[^a-zA-Z0-9_-]+", "-", name).strip("-")
return cleaned[:64] or "channel"

def _init_db(self):
"""Initializes database schema if missing and creates memory backup folders."""
logger.info(f"Initializing SQLite database at: {self.db_path}")
Expand Down Expand Up @@ -84,7 +91,7 @@ def sync_markdown_files(self):
for row in cursor.fetchall():
ch_name = row["channel_name"]
ch_summary = row["channel_summary"] or ""
file_path = os.path.join(self.memory_dir, f"{ch_name}.md")
file_path = os.path.join(self.memory_dir, f"{self._safe_name(ch_name)}.md")

# Check if file needs to be updated
write_file = True
Expand Down Expand Up @@ -121,7 +128,7 @@ def save_channel(self, channel_id: int, channel_name: str, category_id: Optional
(channel_id, channel_name, category_id, category_name, summary)
)
# Sync directly to local markdown file
file_path = os.path.join(self.memory_dir, f"{channel_name}.md")
file_path = os.path.join(self.memory_dir, f"{self._safe_name(channel_name)}.md")
with open(file_path, "w", encoding="utf-8") as f:
f.write(summary)
finally:
Expand Down