Merge pull request #201 from tmchow/fix/90-store-sql-column-whitelist
fix(store): validate updatable columns in update_run and update_finding
This commit is contained in:
@@ -132,6 +132,32 @@ INSERT OR IGNORE INTO settings (key, value) VALUES ('briefing_format', 'concise'
|
||||
INSERT OR IGNORE INTO settings (key, value) VALUES ('default_schedule', '0 8 * * *');
|
||||
"""
|
||||
|
||||
_UPDATABLE_RUN_COLUMNS = frozenset({
|
||||
"source_mode",
|
||||
"prompt_tokens",
|
||||
"completion_tokens",
|
||||
"token_cost",
|
||||
"duration_seconds",
|
||||
"status",
|
||||
"error_message",
|
||||
"findings_new",
|
||||
"findings_updated",
|
||||
})
|
||||
|
||||
_UPDATABLE_FINDING_COLUMNS = frozenset({
|
||||
"source",
|
||||
"source_url",
|
||||
"source_title",
|
||||
"author",
|
||||
"content",
|
||||
"summary",
|
||||
"engagement_score",
|
||||
"relevance_score",
|
||||
"last_seen",
|
||||
"sighting_count",
|
||||
"dismissed",
|
||||
})
|
||||
|
||||
# Future migrations keyed by version number
|
||||
MIGRATIONS: Dict[int, str] = {}
|
||||
|
||||
@@ -298,6 +324,11 @@ def update_run(run_id: int, **kwargs):
|
||||
"""Update a research run's fields."""
|
||||
conn = _connect()
|
||||
try:
|
||||
invalid_columns = sorted(set(kwargs) - _UPDATABLE_RUN_COLUMNS)
|
||||
if invalid_columns:
|
||||
raise ValueError(
|
||||
f"Invalid run update fields: {', '.join(invalid_columns)}"
|
||||
)
|
||||
sets = ", ".join(f"{k} = ?" for k in kwargs)
|
||||
values = list(kwargs.values()) + [run_id]
|
||||
conn.execute(f"UPDATE research_runs SET {sets} WHERE id = ?", values)
|
||||
@@ -430,6 +461,11 @@ def update_finding(finding_id: int, **kwargs):
|
||||
"""Update a finding's fields."""
|
||||
conn = _connect()
|
||||
try:
|
||||
invalid_columns = sorted(set(kwargs) - _UPDATABLE_FINDING_COLUMNS)
|
||||
if invalid_columns:
|
||||
raise ValueError(
|
||||
f"Invalid finding update fields: {', '.join(invalid_columns)}"
|
||||
)
|
||||
sets = ", ".join(f"{k} = ?" for k in kwargs)
|
||||
values = list(kwargs.values()) + [finding_id]
|
||||
conn.execute(f"UPDATE findings SET {sets} WHERE id = ?", values)
|
||||
|
||||
Reference in New Issue
Block a user