Skip to content

[AGENT:Claude] Parameterize SQL queries in database_cleanup_service.py — SQL injection fix #409

Description

@groupthinking

Task Specification

Branch: refactor/hybrid-infra-v2
Agent: Claude (Claude Sonnet 4.6)
Verification: @coderabbitai security review + bandit scan


Intent

Parameterize all raw SQL string concatenation in database_cleanup_service.py to eliminate SQL injection vulnerabilities. This is a critical security fix — the current code constructs SQL queries by f-string interpolation with user-controllable input.

Requirements

  1. Primary file: src/youtube_extension/backend/services/database_cleanup_service.py
  2. Pattern to find: f-strings or .format() used to build SQL queries
  3. Replace with: Parameterized queries using ? placeholders (SQLite) or %s (PostgreSQL/MySQL depending on driver)
  4. Check the DB driver: Determine which database library is used (sqlite3, asyncpg, psycopg2, sqlalchemy) and use its native parameterization
  5. Also scan: All files in src/youtube_extension/backend/services/ for the same pattern

Code Pattern (Before → After)

BEFORE (vulnerable):

query = f"DELETE FROM videos WHERE channel_id = '{channel_id}' AND created_at < '{cutoff_date}'"
cursor.execute(query)

AFTER (safe):

query = "DELETE FROM videos WHERE channel_id = ? AND created_at < ?"
cursor.execute(query, (channel_id, cutoff_date))

Files to Modify

  • src/youtube_extension/backend/services/database_cleanup_service.py (primary)
  • src/youtube_extension/backend/services/*.py (scan all for SQL injection patterns)
  • Add regression tests in tests/test_database_cleanup.py

Acceptance Criteria

# No f-string SQL construction
grep -rn 'f".*SELECT\|f".*INSERT\|f".*DELETE\|f".*UPDATE' src/youtube_extension/backend/ # must return empty

# Bandit security scan passes
bandit -r src/youtube_extension/backend/ -ll  # zero high-severity findings

# Existing tests still pass
pytest tests/ -k "database" -v  # all pass

Self-Correction Context

Security severity: CRITICAL

  • Any user-controllable input (channel_id, video_id, date ranges) that flows into these queries can be exploited
  • This was flagged by CodeRabbit in a previous review but never addressed
  • The fix must be backward-compatible (same query results, just parameterized)

IMPORTANT

  • Do NOT change query logic or add new features
  • Do NOT refactor the service architecture
  • ONLY parameterize the existing queries
  • Keep the same function signatures

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions