AI coding agents can edit files, run shell commands, and touch infrastructure. One bad rm -rf, force-push, or docker compose down -v can wipe hours of work or production data.
| 1 | # SPDX-License-Identifier: MIT |
| 2 | """Database destructive-operation detection (static only).""" |
| 3 | |
| 4 | from __future__ import annotations |
| 5 | |
| 6 | import re |
| 7 | |
| 8 | from agent_guard.config import GuardConfig |
| 9 | from agent_guard.models import Decision, Finding, ParsedCommand, RiskLevel |
| 10 | from agent_guard.risk import SCORE_DATABASE_DESTRUCTIVE, SCORE_PRODUCTION_DATABASE |
| 11 | from agent_guard.rules import RuleDefinition, register_rule |
| 12 | |
| 13 | _DESTRUCTIVE_SQL = re.compile( |
| 14 | r"(?i)\b(DROP\s+(DATABASE|TABLE|SCHEMA)|TRUNCATE\s+TABLE|TRUNCATE)\b", |
| 15 | ) |
| 16 | _MONGO_DROP = re.compile( |
| 17 | r"(?i)\b(db\.dropDatabase\s*\(|db\.[\w.]+\.drop\s*\(|dropDatabase\s*\()", |
| 18 | ) |
| 19 | _REDIS_FLUSH = re.compile(r"(?i)\b(FLUSHALL|FLUSHDB)\b") |
| 20 | _PROD_URL = re.compile( |
| 21 | r"(prod|production|live)[.-].*\.(com|net|io|org)|" |
| 22 | r"://[^/\s]*(prod|production)[^/\s]*", |
| 23 | re.IGNORECASE, |
| 24 | ) |
| 25 | |
| 26 | |
| 27 | @register_rule( |
| 28 | RuleDefinition( |
| 29 | id="database.destructive", |
| 30 | category="database_destructive", |
| 31 | risk=RiskLevel.CRITICAL, |
| 32 | default_decision=Decision.BLOCK, |
| 33 | description="Detects DROP/TRUNCATE/FLUSH and MongoDB dropDatabase operations.", |
| 34 | ) |
| 35 | ) |
| 36 | def rule_database_destructive(parsed: ParsedCommand, config: GuardConfig) -> list[Finding]: |
| 37 | text = parsed.raw |
| 38 | findings: list[Finding] = [] |
| 39 | |
| 40 | if _DESTRUCTIVE_SQL.search(text): |
| 41 | m = _DESTRUCTIVE_SQL.search(text) |
| 42 | findings.append( |
| 43 | Finding( |
| 44 | rule_id="database.sql_destructive", |
| 45 | category="database_destructive", |
| 46 | risk=RiskLevel.CRITICAL, |
| 47 | decision=Decision.BLOCK, |
| 48 | reason="Destructive SQL can permanently erase database data.", |
| 49 | target=m.group(0) if m else None, |
| 50 | score_delta=SCORE_DATABASE_DESTRUCTIVE, |
| 51 | suggestions=("Use migrations and backups before destructive schema changes.",), |
| 52 | ) |
| 53 | ) |
| 54 | |
| 55 | if _MONGO_DROP.search(text): |
| 56 | findings.append( |
| 57 | Finding( |
| 58 | rule_id="database.mongo_drop", |
| 59 | category="database_destructive", |
| 60 | risk=RiskLevel.CRITICAL, |
| 61 | decision=Decision.BLOCK, |
| 62 | reason="MongoDB drop operations permanently remove collections or databases.", |
| 63 | score_delta=SCORE_DATABASE_DESTRUCTIVE, |
| 64 | ) |
| 65 | ) |
| 66 | |
| 67 | if _REDIS_FLUSH.search(text): |
| 68 | findings.append( |
| 69 | Finding( |
| 70 | rule_id="database.redis_flush", |
| 71 | category="database_destructive", |
| 72 | risk=RiskLevel.CRITICAL, |
| 73 | decision=Decision.BLOCK, |
| 74 | reason="Redis FLUSHALL/FLUSHDB erase all keys in the target database.", |
| 75 | score_delta=SCORE_DATABASE_DESTRUCTIVE, |
| 76 | ) |
| 77 | ) |
| 78 | |
| 79 | # Production connection strings in the command |
| 80 | if _PROD_URL.search(text) and any( |
| 81 | x in text.lower() |
| 82 | for x in ("postgres", "mysql", "mongodb", "redis", "database_url", "jdbc:") |
| 83 | ): |
| 84 | findings.append( |
| 85 | Finding( |
| 86 | rule_id="database.production_target", |
| 87 | category="database_destructive", |
| 88 | risk=RiskLevel.CRITICAL, |
| 89 | decision=Decision.BLOCK, |
| 90 | reason="Command appears to target a production database endpoint.", |
| 91 | score_delta=SCORE_PRODUCTION_DATABASE, |
| 92 | suggestions=("Use a local or staging connection string instead.",), |
| 93 | ) |
| 94 | ) |
| 95 | |
| 96 | return findings |