@cloop/runner 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,301 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/drivers/read-permissions.ts
4
+ import { resolve } from "path";
5
+ var READ_PREFIXES = [
6
+ "git log",
7
+ "git show",
8
+ "git diff",
9
+ "git status",
10
+ "git fetch",
11
+ "git branch",
12
+ "git rev-parse",
13
+ "gh pr view",
14
+ "gh pr list",
15
+ "gh issue view",
16
+ "gh issue list",
17
+ "ls",
18
+ "cat",
19
+ "head",
20
+ "tail",
21
+ "wc"
22
+ ];
23
+ var READ_TOOLS = [
24
+ "Read",
25
+ "Glob",
26
+ "Grep",
27
+ "LS",
28
+ "mcp__cloop__get_task",
29
+ "mcp__cloop__get_task_context",
30
+ "mcp__cloop__list_tasks",
31
+ "mcp__cloop__search",
32
+ ...READ_PREFIXES.map((prefix) => `Bash(${prefix} *)`)
33
+ ];
34
+ var FLAGS = {
35
+ "git log": [
36
+ "--oneline",
37
+ "--all",
38
+ "--graph",
39
+ "--decorate",
40
+ "--no-decorate",
41
+ "--stat",
42
+ "--shortstat",
43
+ "--name-only",
44
+ "--name-status",
45
+ "--no-merges",
46
+ "--merges",
47
+ "--reverse",
48
+ "--first-parent",
49
+ "--no-pager"
50
+ ],
51
+ "git show": [
52
+ "--stat",
53
+ "--shortstat",
54
+ "--name-only",
55
+ "--name-status",
56
+ "--oneline",
57
+ "--no-patch",
58
+ "-s",
59
+ "-p"
60
+ ],
61
+ "git diff": [
62
+ "--stat",
63
+ "--shortstat",
64
+ "--name-only",
65
+ "--name-status",
66
+ "--cached",
67
+ "--staged",
68
+ "--check",
69
+ "--no-ext-diff",
70
+ "--no-textconv",
71
+ "--numstat",
72
+ "--summary",
73
+ "--no-index"
74
+ ],
75
+ "git status": [
76
+ "--short",
77
+ "-s",
78
+ "--branch",
79
+ "-b",
80
+ "--porcelain",
81
+ "--porcelain=v1",
82
+ "--porcelain=v2",
83
+ "--untracked-files",
84
+ "--untracked-files=all",
85
+ "--untracked-files=no"
86
+ ],
87
+ "git fetch": ["-q", "--quiet", "--all", "--tags", "--no-tags", "--dry-run"],
88
+ "git branch": [
89
+ "--list",
90
+ "--all",
91
+ "-a",
92
+ "--remotes",
93
+ "-r",
94
+ "--show-current",
95
+ "-v",
96
+ "-vv",
97
+ "--verbose",
98
+ "--no-color"
99
+ ],
100
+ "git rev-parse": [
101
+ "--show-toplevel",
102
+ "--show-prefix",
103
+ "--show-cdup",
104
+ "--git-dir",
105
+ "--is-inside-work-tree",
106
+ "--abbrev-ref",
107
+ "--verify",
108
+ "--short",
109
+ "--symbolic-full-name"
110
+ ],
111
+ "gh pr view": ["--comments"],
112
+ "gh pr list": [],
113
+ "gh issue view": ["--comments"],
114
+ "gh issue list": []
115
+ };
116
+ var VALUES = {
117
+ "git log": [
118
+ "--format",
119
+ "--pretty",
120
+ "--date",
121
+ "--max-count",
122
+ "-n",
123
+ "--since",
124
+ "--until",
125
+ "--author",
126
+ "--grep"
127
+ ],
128
+ "git show": ["--format", "--pretty", "--date"],
129
+ "git diff": ["--unified", "-U"],
130
+ "git branch": ["--sort", "--format", "--contains", "--no-contains", "--merged", "--no-merged"],
131
+ "gh pr view": ["--json", "--jq", "-q", "--template", "-t", "--repo", "-R"],
132
+ "gh issue view": ["--json", "--jq", "-q", "--template", "-t", "--repo", "-R"],
133
+ "gh pr list": [
134
+ "--json",
135
+ "--jq",
136
+ "-q",
137
+ "--template",
138
+ "-t",
139
+ "--repo",
140
+ "-R",
141
+ "--author",
142
+ "--assignee",
143
+ "--base",
144
+ "--head",
145
+ "--label",
146
+ "--limit",
147
+ "-L",
148
+ "--search",
149
+ "--state"
150
+ ],
151
+ "gh issue list": [
152
+ "--json",
153
+ "--jq",
154
+ "-q",
155
+ "--template",
156
+ "-t",
157
+ "--repo",
158
+ "-R",
159
+ "--author",
160
+ "--assignee",
161
+ "--label",
162
+ "--limit",
163
+ "-L",
164
+ "--search",
165
+ "--state",
166
+ "--milestone"
167
+ ]
168
+ };
169
+ function commands(command) {
170
+ const result = [];
171
+ let args = [];
172
+ let word = "";
173
+ let quote = "";
174
+ const wordEnd = () => {
175
+ if (word) args.push(word);
176
+ word = "";
177
+ };
178
+ for (let i = 0; i < command.length; i++) {
179
+ const c = command.charAt(i);
180
+ if (c === "$" || c === "`" || c === "\\") return null;
181
+ if (quote) {
182
+ if (c === quote) quote = "";
183
+ else word += c;
184
+ } else if (c === '"' || c === "'") quote = c;
185
+ else if ("><(){}*?[]#!".includes(c)) return null;
186
+ else if (";&|\n".includes(c)) {
187
+ wordEnd();
188
+ if (!args.length) return null;
189
+ result.push(args);
190
+ args = [];
191
+ if ((c === "&" || c === "|") && command[i + 1] === c) i++;
192
+ else if (c === "&") return null;
193
+ } else if (/\s/.test(c)) wordEnd();
194
+ else word += c;
195
+ }
196
+ if (quote) return null;
197
+ wordEnd();
198
+ if (!args.length) return null;
199
+ result.push(args);
200
+ return result;
201
+ }
202
+ function readCommand(words) {
203
+ const prefix = READ_PREFIXES.find((p) => p.split(" ").every((w, i) => words[i] === w));
204
+ if (!prefix) return false;
205
+ const args = words.slice(prefix.split(" ").length);
206
+ if (prefix === "git branch" && args.some((a) => !a.startsWith("-")) && !args.includes("--list"))
207
+ return false;
208
+ let paths = false;
209
+ for (let i = 0; i < args.length; i++) {
210
+ const arg = args[i];
211
+ if (arg === void 0) return false;
212
+ if (paths || !arg.startsWith("-")) {
213
+ if (prefix === "git fetch" && !/^[\w./~-]+$/.test(arg)) return false;
214
+ continue;
215
+ }
216
+ if (arg === "--") {
217
+ paths = true;
218
+ continue;
219
+ }
220
+ if (FLAGS[prefix]?.includes(arg)) continue;
221
+ const flag = arg.split("=")[0] ?? "";
222
+ if (VALUES[prefix]?.includes(flag)) {
223
+ if (!arg.includes("=")) {
224
+ const value = args[++i];
225
+ if (!value || value.startsWith("-")) return false;
226
+ }
227
+ continue;
228
+ }
229
+ if (["git log", "git show"].includes(prefix) && /^-(?:n)?\d+$/.test(arg)) continue;
230
+ if (prefix === "ls" && /^-[alhtrSd1AF]+$/.test(arg)) continue;
231
+ if (prefix === "cat" && /^-[benstuvAET]+$/.test(arg)) continue;
232
+ if (prefix === "wc" && /^-[clmwL]+$/.test(arg)) continue;
233
+ if (["head", "tail"].includes(prefix) && /^-\d+$/.test(arg)) continue;
234
+ if (["head", "tail"].includes(prefix) && ["-n", "-c", "--lines", "--bytes"].includes(flag)) {
235
+ if (!/^[+-]?\d+$/.test(arg.includes("=") ? arg.slice(arg.indexOf("=") + 1) : args[++i] ?? ""))
236
+ return false;
237
+ continue;
238
+ }
239
+ return false;
240
+ }
241
+ return true;
242
+ }
243
+ function isReadOnlyCommand(command) {
244
+ const parsed = commands(command);
245
+ return parsed !== null && parsed.length > 0 && parsed.every(readCommand);
246
+ }
247
+ function readOnlySettings(askEverything = false) {
248
+ const quote = (s) => `'${s.replace(/'/g, "'\\''")}'`;
249
+ return {
250
+ permissions: { ask: ["Bash(git push *)"] },
251
+ hooks: {
252
+ PreToolUse: [
253
+ {
254
+ matcher: "Bash",
255
+ hooks: [
256
+ {
257
+ type: "command",
258
+ command: `${quote(process.execPath)} ${quote(resolve(process.argv[1] ?? ""))} hook runner-permission${askEverything ? " --ask-everything" : ""}`
259
+ }
260
+ ]
261
+ }
262
+ ]
263
+ }
264
+ };
265
+ }
266
+ function needsReadGuard(command, askEverything = false) {
267
+ if (isReadOnlyCommand(command)) return false;
268
+ const parsed = commands(command);
269
+ return askEverything || parsed === null || parsed.some(
270
+ (words) => READ_PREFIXES.some((prefix) => prefix.split(" ").every((word, i) => words[i] === word))
271
+ );
272
+ }
273
+ async function runnerPermissionHook() {
274
+ let input = "";
275
+ for await (const chunk of process.stdin) input += chunk;
276
+ let command;
277
+ try {
278
+ command = JSON.parse(input)?.tool_input?.command;
279
+ } catch {
280
+ }
281
+ if (typeof command === "string" && !needsReadGuard(command, process.argv.includes("--ask-everything")))
282
+ return;
283
+ process.stdout.write(
284
+ JSON.stringify({
285
+ hookSpecificOutput: {
286
+ hookEventName: "PreToolUse",
287
+ permissionDecision: "ask",
288
+ permissionDecisionReason: "This command needs your approval."
289
+ }
290
+ })
291
+ );
292
+ }
293
+
294
+ export {
295
+ READ_PREFIXES,
296
+ READ_TOOLS,
297
+ isReadOnlyCommand,
298
+ readOnlySettings,
299
+ needsReadGuard,
300
+ runnerPermissionHook
301
+ };