dcg

MySQL/MariaDB

database.mysql

Protects against destructive MySQL/MariaDB operations like DROP DATABASE, TRUNCATE, and mysqladmin drop

  • 13blocking rules
  • 5safe patterns
  • 3Critical
  • 8High
  • 2Medium
~/.config/dcg/config.toml
[packs]
enabled = ["database.mysql"]

Evaluated when a command contains mysql, mysqladmin, mysqldump, mariadb, DROP, TRUNCATE, DELETE, delete, drop, truncate.

Blocking rules

Each rule ID is database.mysql:<name>. Use it in allowlists and [policy.rules].

  1. drop-database

    Critical

    DROP DATABASE permanently deletes the entire database. Verify and back up first.

    Explanation
    DROP DATABASE completely removes a database and ALL its contents: - All tables, views, and indexes - All stored procedures and functions - All triggers and events - All data - gone permanently - User privileges on the database remain but are orphaned IF EXISTS only prevents errors if the database doesn't exist - it still deletes! Before dropping: mysqldump -h host -u user -p dbname > backup.sql Verify database name: SHOW DATABASES LIKE 'dbname';
  2. mysqladmin-drop

    Critical

    mysqladmin drop permanently deletes the database. Verify carefully.

    Explanation
    mysqladmin drop is the CLI equivalent of DROP DATABASE: - Completely removes the database - All data is lost permanently - Has an 'are you sure?' prompt by default - Adding --force skips confirmation! Triple-check the database name. Common mistake: mysqladmin drop myapp_production # Oops, meant myapp_staging Backup first: mysqldump -h host -u user -p dbname > backup.sql List databases to verify: mysql -e 'SHOW DATABASES;'
  3. reset-master

    Critical

    RESET MASTER deletes all binary logs and resets the binlog position.

    Explanation
    RESET MASTER is destructive to replication and point-in-time recovery: - Deletes ALL binary log files - Resets binary log index - Clears GTID executed set - Breaks replication to all replicas - Loses point-in-time recovery capability Replicas must be reconfigured after RESET MASTER on the source. Before running: - Ensure no replicas are replicating - Backup binary logs if needed for recovery - Consider PURGE BINARY LOGS for selective cleanup Safer alternative: PURGE BINARY LOGS BEFORE 'date'; -- Keeps recent logs
  4. delete-without-where

    High

    DELETE without WHERE clause deletes ALL rows. Add a WHERE clause.

    Explanation
    DELETE without WHERE removes ALL rows from the table: - Each row deletion is logged in binary log - CAN be rolled back if within a transaction - Fires DELETE triggers for each row - Does NOT reset AUTO_INCREMENT counter - Much slower than TRUNCATE for large tables If you meant to delete all rows: - TRUNCATE is faster (but non-transactional, and dcg gates it too) - DELETE in transaction allows rollback Otherwise, add a WHERE clause: DELETE FROM tablename WHERE condition; Preview what would be deleted: SELECT COUNT(*) FROM tablename; -- all rows! SELECT * FROM tablename LIMIT 10;
  5. drop-column

    High

    ALTER TABLE ... DROP COLUMN permanently deletes that column's data in every row.

    Explanation
    Dropping a column (or a partition) removes its data from every row it covers; ALTER TABLE is not transactional in MySQL, so there is no rollback. Back up the column first: CREATE TABLE tablename_col_backup AS SELECT id, col FROM tablename;
  6. drop-table

    High

    DROP TABLE permanently deletes the table. Verify and back up first.

    Explanation
    DROP TABLE removes the table structure and ALL data: - All rows are permanently deleted - Indexes and constraints are removed - Foreign key relationships may break - Triggers on the table are dropped - AUTO_INCREMENT values are lost IF EXISTS only prevents errors - it still drops the table! Backup table first: mysqldump -h host -u user -p dbname tablename > table_backup.sql Preview table contents: SELECT COUNT(*) FROM tablename; SELECT * FROM tablename LIMIT 10;
  7. grant-all

    High

    GRANT ALL ON *.* gives unrestricted access to all databases.

    Explanation
    GRANT ALL PRIVILEGES ON *.* gives the user complete control: - Can create, modify, and drop ANY database - Can create, modify, and drop ANY user - Can grant privileges to others - Essentially creates a superuser This is rarely necessary and violates principle of least privilege. Better alternatives: - GRANT ALL ON specific_db.* - Limit to one database - GRANT SELECT, INSERT, UPDATE ON db.* - Limit operations - GRANT ... ON db.table - Limit to specific tables Review current grants: SHOW GRANTS FOR 'user'@'host';
  8. mysqldump-add-drop-database

    High

    mysqldump --add-drop-database drops the database before restore.

    Explanation
    mysqldump --add-drop-database adds DROP DATABASE to the backup file. On restore, this will: - DROP the database before CREATE DATABASE - Destroy ALL existing data in that database - If restore fails partway, data may be lost This is dangerous when restoring to a database with data you want to keep. Safer approach: - Restore to a new/different database first - Verify the restore completed successfully - Then rename or swap databases Without --add-drop-database: mysqldump dbname > backup.sql # Creates only, no drops
  9. stdin-unverified

    High

    mysql/mariadb receives indirect input that dcg cannot statically verify.

    Explanation
    Materialize and review the exact SQL before piping or redirecting it into the client.
  10. truncate-table

    High

    TRUNCATE permanently deletes all rows. Cannot be rolled back in MySQL.

    Explanation
    TRUNCATE is faster than DELETE but more dangerous in MySQL: - Removes ALL rows instantly - CANNOT be rolled back (implicit commit) - Does not fire DELETE triggers - Resets AUTO_INCREMENT counter to 1 - Requires DROP privilege - Acquires table lock Unlike PostgreSQL, MySQL's TRUNCATE is NOT transactional! Check row count first: SELECT COUNT(*) FROM tablename; If you need rollback capability, use: DELETE FROM tablename; -- Slower but transactional (dcg gates an unfiltered DELETE too, so this form also needs approval)
  11. update-without-where

    High

    UPDATE without WHERE clause overwrites the column in ALL rows. Add a WHERE clause.

    Explanation
    UPDATE without WHERE changes every row in the table. With autocommit on (the MySQL default) the previous values are gone the moment it runs. Scope it: UPDATE tablename SET col = value WHERE condition; Preview what would change: SELECT COUNT(*) FROM tablename WHERE condition;
  12. drop-user

    Medium

    DROP USER permanently removes the user account and all their privileges.

    Explanation
    DROP USER removes the MySQL user account: - All privileges granted to the user are revoked - User can no longer authenticate - Applications using this user will fail - Cannot be undone (user must be recreated) IF EXISTS only prevents errors if user doesn't exist. Before dropping: SHOW GRANTS FOR 'user'@'host'; -- Document privileges SELECT * FROM mysql.user WHERE user='username'; -- Verify user Consider disabling instead: ALTER USER 'user'@'host' ACCOUNT LOCK;
  13. mysqldump-add-drop-table

    Medium

    mysqldump --add-drop-table drops tables before creating them on restore.

    Explanation
    mysqldump --add-drop-table adds DROP TABLE before each CREATE TABLE. This is actually the DEFAULT behavior of mysqldump! On restore, each table is dropped before being recreated. Risks: - If restore fails partway, some tables may be dropped without recreation - Existing data in those tables is permanently lost Safer alternatives: - Use --skip-add-drop-table to disable drops - Restore to a new database first, then verify - Keep the original database until restore is confirmed To preserve existing data: mysqldump --skip-add-drop-table dbname > backup.sql

Safe patterns

Checked first. A command that matches one is never blocked by this pack.

  • select-query
  • show-command
  • describe-query
  • mysqldump-no-drop
  • mysql-select