Repository navigation
Expand file tree
/
Copy pathdatabase.ts
More file actions
62 lines (52 loc) · 2.25 KB
/
Copy pathdatabase.ts
File metadata and controls
62 lines (52 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { hasFlag } from '../util'
const DB_SHELLS_FORBIDDEN = new Set([
'psql',
'mysql',
'mariadb',
'mongosh',
'mongo',
'redis-cli',
'sqlcmd',
])
const FORCE_FLAGS = ['--force', '-f']
const DESTRUCTIVE_VERB_TOKENS = new Set([
'drop',
'delete',
'destroy',
'purge',
'truncate',
'remove',
'rm',
])
const REASON_DB_INTERACTIVE = 'Interactive DB shells allow arbitrary destructive queries the classifier cannot safely evaluate. Use read-only app-level diagnostics or exported artifacts instead.'
const REASON_DROPDB = 'Dropping databases is irreversible without verified backups. Use non-destructive metadata/listing commands instead.'
const REASON_DROPUSER = 'Dropping DB users can break authentication for running systems. Use user listing/inspection commands first.'
const REASON_CREATEDB = 'Creating databases mutates remote server state. Use read-only inspection commands instead.'
const REASON_CREATEUSER = 'Creating database users mutates remote server state. Use read-only inspection commands instead.'
const REASON_PG_RESTORE = 'Restoring data mutates remote database state. Use `pg_dump` for read-only exports instead.'
const REASON_FORCE_DESTRUCTIVE = 'Forced destructive DB utility operations bypass safeguards and confirmations. Use dry-run/list/inspect commands before changes.'
export function isDatabaseForbidden(base: string, _args: readonly string[]): string | null {
if (DB_SHELLS_FORBIDDEN.has(base)) return REASON_DB_INTERACTIVE
return null
}
export function isDatabaseUtilityForbidden(base: string, args: readonly string[]): string | null {
if (base === 'dropdb') return REASON_DROPDB
if (base === 'dropuser') return REASON_DROPUSER
if (base === 'createdb') return REASON_CREATEDB
if (base === 'createuser') return REASON_CREATEUSER
if (base === 'pg_restore') return REASON_PG_RESTORE
if (hasFlag(args, ...FORCE_FLAGS) && hasDestructiveToken(args)) {
return REASON_FORCE_DESTRUCTIVE
}
return null
}
function hasDestructiveToken(args: readonly string[]): boolean {
for (const arg of args) {
const normalized = arg.toLowerCase()
const fragments = normalized.split(/[=:/_-]+/).filter(Boolean)
for (const fragment of fragments) {
if (DESTRUCTIVE_VERB_TOKENS.has(fragment)) return true
}
}
return false
}