From 322e347a86f206dff8063bfc330889087ed74c40 Mon Sep 17 00:00:00 2001 From: yuhoyuho Date: Thu, 12 Nov 2026 01:08:39 +0900 Subject: [PATCH] fix(gateguard): gate DROP DATABASE, DROP SCHEMA and ALTER TABLE ... DROP COLUMN DESTRUCTIVE_SQL matched only DROP TABLE, DELETE FROM and TRUNCATE. `psql -c "DROP DATABASE prod"`, `DROP SCHEMA public CASCADE` and `ALTER TABLE users DROP COLUMN email` went to the routine gate instead of the destructive gate, even though each loses at least as much data as DROP TABLE. Add DROP DATABASE, DROP SCHEMA and an ALTER TABLE ... DROP COLUMN arm to the phrase check. The DROP COLUMN arm stops at `;` so it never matches across two statements. It requires the COLUMN keyword so ALTER COLUMN ... DROP DEFAULT / DROP NOT NULL stay passive. Existing literal and quote stripping keeps SQL strings, commit messages and echo mentions passive. Update the gateguard trigger list (en, ja-JP, zh-CN) and rebind the skill-triggers registry digest for the SKILL.md body edit. Skill descriptions and triggers are unchanged. --- docs/ja-JP/skills/gateguard/SKILL.md | 2 +- docs/zh-CN/skills/gateguard/SKILL.md | 2 +- manifests/context-packs/skill-triggers@1.json | 2 +- scripts/hooks/gateguard-fact-force.js | 8 ++++- skills/gateguard/SKILL.md | 2 +- tests/hooks/gateguard-fact-force.test.js | 35 +++++++++++++++++++ 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/docs/ja-JP/skills/gateguard/SKILL.md b/docs/ja-JP/skills/gateguard/SKILL.md index 0a7fde34b..9a6612e4e 100644 --- a/docs/ja-JP/skills/gateguard/SKILL.md +++ b/docs/ja-JP/skills/gateguard/SKILL.md @@ -103,7 +103,7 @@ code クラスでは変更内容も読み、公開宣言に触れない Edit に ### Destructive Bash Gate (every destructive command) -Triggers on: `rm -rf`, `git reset --hard`, `git push --force`, `drop table`, etc. +Triggers on: `rm -rf`, `git reset --hard`, `git push --force`, `drop table`, `drop database`, `drop schema`, `alter table ... drop column`, etc. ``` 1. List all files/data this command will modify or delete diff --git a/docs/zh-CN/skills/gateguard/SKILL.md b/docs/zh-CN/skills/gateguard/SKILL.md index f6669fa4f..8f36dce36 100644 --- a/docs/zh-CN/skills/gateguard/SKILL.md +++ b/docs/zh-CN/skills/gateguard/SKILL.md @@ -71,7 +71,7 @@ LLM 的自我评估不起作用。问"你是否违反了任何策略?"答案 ### 破坏性 Bash 门控(每个破坏性命令) -触发条件:`rm -rf`、`git reset --hard`、`git push --force`、`drop table` 等。 +触发条件:`rm -rf`、`git reset --hard`、`git push --force`、`drop table`、`drop database`、`drop schema`、`alter table ... drop column` 等。 ``` 1. 列出此命令将修改或删除的所有文件/数据 diff --git a/manifests/context-packs/skill-triggers@1.json b/manifests/context-packs/skill-triggers@1.json index 48081c3fb..8e7858ce6 100644 --- a/manifests/context-packs/skill-triggers@1.json +++ b/manifests/context-packs/skill-triggers@1.json @@ -10,7 +10,7 @@ "id": "hand-seeded", "source": "manual-curation-pending-regeneration" }, - "registryDigest": "6f213dc845937b4093bc018d041b25b538d88d78451b9d799ad614989b360ee4", + "registryDigest": "bb9891dd197ceda96e6b4da38279e654a8e70e80f2c2c49891800f8ac2e2d451", "schemaVersion": 1, "triggers": { "skill:agent-runtime-gateway-smoke-test": [ diff --git a/scripts/hooks/gateguard-fact-force.js b/scripts/hooks/gateguard-fact-force.js index 583f36052..dd5cb2221 100644 --- a/scripts/hooks/gateguard-fact-force.js +++ b/scripts/hooks/gateguard-fact-force.js @@ -123,7 +123,13 @@ const ECC_ENABLE_VALUES = new Set(['1', 'true', 'on', 'enabled', 'enable', 'yes' // `echo dd if=x` — which runs no dd at all — was gated. The boundary decided // the verdict instead of the command position, so dd moved to isDestructiveDd() // alongside the other token-based detectors (#2642). -const DESTRUCTIVE_SQL = /\b(drop\s+table|delete\s+from|truncate)\b/i; +// DROP DATABASE / DROP SCHEMA remove at least as much data as DROP TABLE, and +// ALTER TABLE ... DROP COLUMN discards a column's data across every row. The +// DROP COLUMN arm is bounded by `;` so it cannot join two separate statements, +// and it requires the explicit COLUMN keyword so `ALTER COLUMN ... DROP +// DEFAULT` / `DROP NOT NULL` (metadata-only changes) stay passive. +const DESTRUCTIVE_SQL = + /\b(drop\s+(?:table|database|schema)|delete\s+from|truncate|alter\s+table\b[^;]*?\bdrop\s+column)\b/i; // Operator-supplied additional destructive patterns. Lazily compiled from // `GATEGUARD_BASH_EXTRA_DESTRUCTIVE` (regex source) on first use, then diff --git a/skills/gateguard/SKILL.md b/skills/gateguard/SKILL.md index 208174601..59c18b228 100644 --- a/skills/gateguard/SKILL.md +++ b/skills/gateguard/SKILL.md @@ -190,7 +190,7 @@ may collapse, and that a sibling must share the first file's class. ### Destructive Bash Gate (every destructive command) -Triggers on: `rm -rf`, `git reset --hard`, `git push --force`, `drop table`, etc. +Triggers on: `rm -rf`, `git reset --hard`, `git push --force`, `drop table`, `drop database`, `drop schema`, `alter table ... drop column`, etc. ``` 1. List all files/data this command will modify or delete diff --git a/tests/hooks/gateguard-fact-force.test.js b/tests/hooks/gateguard-fact-force.test.js index a0d16f056..94eb58e79 100644 --- a/tests/hooks/gateguard-fact-force.test.js +++ b/tests/hooks/gateguard-fact-force.test.js @@ -2248,6 +2248,41 @@ function runTests() { passed++; else failed++; + if ( + test('denies DROP DATABASE, DROP SCHEMA and ALTER TABLE ... DROP COLUMN passed to SQL clients', () => { + // Each of these destroys at least as much data as DROP TABLE. + expectDestructiveDeny('psql -c "DROP DATABASE prod"', 'psql drop database'); + expectDestructiveDeny('psql -c "drop database if exists app_dev"', 'psql drop database if exists'); + expectDestructiveDeny('mysql -e "DROP DATABASE app"', 'mysql drop database'); + expectDestructiveDeny('psql -c "DROP SCHEMA public CASCADE"', 'psql drop schema cascade'); + expectDestructiveDeny('mysql -e "DROP SCHEMA app"', 'mysql drop schema'); + expectDestructiveDeny('psql -c "ALTER TABLE users DROP COLUMN email"', 'psql alter table drop column'); + expectDestructiveDeny( + 'psql -c "ALTER TABLE IF EXISTS ONLY public.users DROP COLUMN IF EXISTS email CASCADE"', + 'psql alter table if exists only drop column' + ); + expectDestructiveDeny('sqlite3 app.db "ALTER TABLE users DROP COLUMN email"', 'sqlite3 drop column'); + expectDestructiveDeny('sudo -u postgres psql -c "drop database prod"', 'sudo psql drop database'); + expectDestructiveDeny('psql -c "drop database prod"; echo done', 'drop database before a separator'); + }) + ) + passed++; + else failed++; + + if ( + test('allows non-destructive ALTER TABLE forms and quoted DROP DATABASE mentions', () => { + expectAllow('psql -c "ALTER TABLE users ALTER COLUMN email DROP DEFAULT"', 'alter column drop default'); + expectAllow('psql -c "ALTER TABLE users ALTER COLUMN email DROP NOT NULL"', 'alter column drop not null'); + expectAllow('psql -c "ALTER TABLE users ADD COLUMN note text"', 'alter table add column'); + expectAllow('psql -c "ALTER TABLE users ADD COLUMN note text; SELECT \'drop column\'"', 'drop column in a literal'); + expectAllow('psql -c "SELECT \'drop database\' AS label"', 'drop database in a literal'); + expectAllow('git commit -m "docs: explain drop database recovery"', 'drop database in -m'); + expectAllow('echo "drop schema public cascade"', 'echo drop schema mention'); + }) + ) + passed++; + else failed++; + if ( test('denies quoted destructive SQL through sudo/env wrappers', () => { expectDestructiveDeny('sudo -u postgres psql -c "drop table users"', 'sudo -u psql');