Repository navigation
Expand file tree
/
Copy pathgit.ts
More file actions
161 lines (134 loc) · 4.94 KB
/
Copy pathgit.ts
File metadata and controls
161 lines (134 loc) · 4.94 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Git Command Classification
*
* Positive allowlist approach: only explicitly verified read-only git
* subcommands are allowed. Everything else is forbidden.
*
* Allowed readonly: status, log, diff, show, branch (with readonly flags only).
* Any git with -c config override is forbidden (can override hooks, aliases, etc).
*/
const READONLY_SUBCOMMANDS = ['status', 'log', 'diff', 'show', 'branch', 'rev-parse'] as const
/**
* Returns true if the git args represent a read-only invocation.
* Args should NOT include 'git' itself — just everything after it.
*/
export function isGitReadOnly(args: string[]): boolean {
if (hasConfigOverride(args)) return false
const sub = findSubcommand(args)
if (!sub) return false
switch (sub.command) {
case 'status':
case 'log':
case 'diff':
case 'show':
case 'rev-parse':
return subcommandArgsReadOnly(sub.argsAfter)
case 'branch':
return subcommandArgsReadOnly(sub.argsAfter) && branchIsReadOnly(sub.argsAfter)
default:
return false
}
}
// ─── Config override detection ──────────────────────────────
function hasConfigOverride(args: string[]): boolean {
for (const arg of args) {
// git -c key=value — standalone
if (arg === '-c') return true
// git -ckey=value — inline
if (arg.startsWith('-c') && arg.length > 2) return true
// git --config-env=KEY=ENVVAR
if (arg === '--config-env' || arg.startsWith('--config-env=')) return true
}
return false
}
// ─── Subcommand extraction ──────────────────────────────────
interface SubcommandResult {
command: string
argsAfter: string[]
}
/**
* Walk past git global options to find the subcommand.
* Returns null if the first non-option word isn't in the readonly allowlist.
*/
function findSubcommand(args: string[]): SubcommandResult | null {
let skipNext = false
for (let i = 0; i < args.length; i++) {
if (skipNext) {
skipNext = false
continue
}
const arg = args[i]
// Global options with inline values: --git-dir=/foo, -C/tmp
if (isGlobalOptionInline(arg)) continue
// Global options that consume the next arg: -C /tmp, --git-dir /foo
if (isGlobalOptionWithValue(arg)) {
skipNext = true
continue
}
// End-of-options marker or unknown flag — skip
if (arg === '--' || arg.startsWith('-')) continue
// First non-option word: must be an allowed subcommand
if (READONLY_SUBCOMMANDS.includes(arg as typeof READONLY_SUBCOMMANDS[number])) {
return { command: arg, argsAfter: args.slice(i + 1) }
}
return null
}
return null
}
// Git global options that take a separate value argument
const GLOBAL_OPTIONS_WITH_VALUE = new Set([
'-C', '-c', '--config-env', '--exec-path',
'--git-dir', '--namespace', '--super-prefix', '--work-tree',
])
function isGlobalOptionWithValue(arg: string): boolean {
return GLOBAL_OPTIONS_WITH_VALUE.has(arg)
}
// Git global options with value packed inline: --git-dir=/foo, -C/tmp
function isGlobalOptionInline(arg: string): boolean {
if (arg.startsWith('--config-env=')) return true
if (arg.startsWith('--exec-path=')) return true
if (arg.startsWith('--git-dir=')) return true
if (arg.startsWith('--namespace=')) return true
if (arg.startsWith('--super-prefix=')) return true
if (arg.startsWith('--work-tree=')) return true
// -C<path> (inline, not just -C)
if (arg.startsWith('-C') && arg.length > 2) return true
// -c<key=val> (inline) — but this is already caught by hasConfigOverride
if (arg.startsWith('-c') && arg.length > 2) return true
return false
}
// ─── Subcommand arg validation ──────────────────────────────
// Flags on subcommands that can trigger writes or arbitrary execution
const UNSAFE_SUBCOMMAND_FLAGS = new Set([
'--output', '--ext-diff', '--textconv', '--exec', '--paginate',
])
function subcommandArgsReadOnly(args: string[]): boolean {
for (const arg of args) {
if (UNSAFE_SUBCOMMAND_FLAGS.has(arg)) return false
if (arg.startsWith('--output=')) return false
if (arg.startsWith('--exec=')) return false
}
return true
}
// ─── Branch-specific validation ─────────────────────────────
const BRANCH_READONLY_FLAGS = new Set([
'--list', '-l', '--show-current',
'-a', '--all', '-r', '--remotes',
'-v', '-vv', '--verbose',
])
/**
* `git branch` with no args is readonly (lists branches).
* With args, every arg must be a known readonly flag.
*/
function branchIsReadOnly(args: string[]): boolean {
if (args.length === 0) return true
let sawReadOnlyFlag = false
for (const arg of args) {
if (BRANCH_READONLY_FLAGS.has(arg) || arg.startsWith('--format=')) {
sawReadOnlyFlag = true
} else {
return false
}
}
return sawReadOnlyFlag
}