flowtask-permission-gate 0.0.0-stage → 2.0.0
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.
- package/LICENSE +21 -0
- package/README.md +90 -2
- package/dist/authorizer.d.ts +16 -0
- package/dist/authorizer.js +473 -0
- package/dist/context-budget.d.ts +138 -0
- package/dist/context-budget.js +369 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +642 -0
- package/dist/layers.d.ts +104 -0
- package/dist/layers.js +185 -0
- package/dist/registration.d.ts +44 -0
- package/dist/registration.js +135 -0
- package/dist/worktree-manager-location.d.ts +17 -0
- package/dist/worktree-manager-location.js +30 -0
- package/package.json +41 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 FlowTask contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,91 @@
|
|
|
1
|
-
#
|
|
1
|
+
# flowtask-permission-gate
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
OpenCode permission gate for FlowTask. The plugin applies FlowTask policies at
|
|
4
|
+
the tool boundary: the Runner allowlist, the universal review-stamp commit gate,
|
|
5
|
+
the permissions layer check, and the context budget for read tools.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
The plugin is distributed as the npm package `flowtask-permission-gate` and
|
|
10
|
+
registered by FlowTask for the OpenCode user:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
flowtask install
|
|
14
|
+
flowtask update
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Consumers do not copy the package into a project and do not install it
|
|
18
|
+
directly. The OpenCode user configuration registers it once with the compatible
|
|
19
|
+
range `flowtask-permission-gate@^2.0.0` and the `configDir` and `runtimeRoot`
|
|
20
|
+
options. OpenCode resolves the package from the npm registry on the first load
|
|
21
|
+
and after a version change; later starts use the resolved package.
|
|
22
|
+
|
|
23
|
+
An installation that kept a local managed copy is migrated by `flowtask install`
|
|
24
|
+
or `flowtask update`: the absolute entry is replaced by the npm range and the
|
|
25
|
+
copy is removed only when it carries the FlowTask ownership marker. A directory
|
|
26
|
+
without the marker is preserved.
|
|
27
|
+
|
|
28
|
+
## What it enforces
|
|
29
|
+
|
|
30
|
+
- **Runner allowlist.** Authorized inspection and management commands run;
|
|
31
|
+
destructive, writing, installing, and ambiguous commands are denied with the
|
|
32
|
+
delegation message. Only the enumerated subagents, MCP tools, and Graphify
|
|
33
|
+
queries are authorized.
|
|
34
|
+
- **Permissions layer.** The Runner does not execute with expanded limits when
|
|
35
|
+
the autonomy or permissions layer is missing or invalid. The denial names the
|
|
36
|
+
layer, the file, the cause, and the action.
|
|
37
|
+
- **Review stamp.** A commit requires a valid stamp unless it is a trivial
|
|
38
|
+
`--no-verify`/`--no-review` bypass. The stamp is JSON with an ISO-8601 `ts`
|
|
39
|
+
and the current branch; it is valid for the configured TTL and is not
|
|
40
|
+
consumed when validated.
|
|
41
|
+
- **Config resolution.** `review.json` is looked up in this order:
|
|
42
|
+
`.opencode/flowtask/config/review.json`, then
|
|
43
|
+
`.flowtask/config/review.json`, relative to the commit workdir. A relative
|
|
44
|
+
`stampPath` is resolved against that workdir. In a registered project, a
|
|
45
|
+
missing or invalid config blocks the commit; `enabled: false` is the explicit
|
|
46
|
+
opt-out.
|
|
47
|
+
- **Context budget.** Broad `read`, `glob`, and `grep` requests are announced,
|
|
48
|
+
large deliveries are reduced without a preview, and image/PDF attachments are
|
|
49
|
+
delegated. Thresholds are never exposed in messages.
|
|
50
|
+
|
|
51
|
+
## Configuration
|
|
52
|
+
|
|
53
|
+
`review.json`:
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"enabled": true,
|
|
58
|
+
"stampPath": ".opencode/flowtask/config/.review-stamp",
|
|
59
|
+
"stampTtlMinutes": 30
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`context-budget.json` (optional, fail-open to defaults):
|
|
64
|
+
|
|
65
|
+
```json
|
|
66
|
+
{
|
|
67
|
+
"readThreshold": 16000,
|
|
68
|
+
"grepThreshold": 8000,
|
|
69
|
+
"globThreshold": 8000,
|
|
70
|
+
"turnAccumulated": 32000,
|
|
71
|
+
"preventiveReadStatLimit": 51200,
|
|
72
|
+
"preventiveGlobPatterns": ["**/*", "**"],
|
|
73
|
+
"blockedPaths": ["node_modules"]
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Both files are read from `.opencode/flowtask/config/` first and from
|
|
78
|
+
`.flowtask/config/` as the fallback.
|
|
79
|
+
|
|
80
|
+
## Known limits
|
|
81
|
+
|
|
82
|
+
- A valid stamp is not consumed, and the stamp is not bound to a specific diff.
|
|
83
|
+
- Commit detection covers `git commit`, `git -C`, `git -c`, `--git-dir`, and
|
|
84
|
+
`cd ... && git commit`. It does not inspect commits started by the worktree
|
|
85
|
+
manager or through nested shells.
|
|
86
|
+
- The plugin does not restart OpenCode; a new version is loaded on the next
|
|
87
|
+
restart.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const RUNNER_DELEGATION_MESSAGE = "Remember that you must delegate this operation to the corresponding subagent.";
|
|
2
|
+
export declare const WORKTREE_MANAGER_MCP_SERVER = "worktree_manager";
|
|
3
|
+
export declare const WORKTREE_MANAGER_MCP_TOOLS: readonly ["worktree_create", "worktree_complete", "worktree_cleanup", "worktree_recover", "worktree_list", "worktree_prune", "worktree_status"];
|
|
4
|
+
/** The runner may invoke exactly the seven capabilities the manager publishes. */
|
|
5
|
+
export declare function isWorktreeManagerMcpTool(tool: unknown): boolean;
|
|
6
|
+
export declare const FLOWTASK_SUBAGENTS: Set<string>;
|
|
7
|
+
/** V2 `subagent` resources the runner may select; the runner itself is never a child. */
|
|
8
|
+
export declare function isAuthorizedRunnerSubagent(agent: unknown): boolean;
|
|
9
|
+
/** Tokenize the small shell grammar used by FlowTask commands. */
|
|
10
|
+
export declare function tokenizeCommand(command: string): string[] | null;
|
|
11
|
+
/**
|
|
12
|
+
* True only for a single, exact authority command. Any compound, redirect, or
|
|
13
|
+
* unknown token makes the command ineligible for the layer bypass.
|
|
14
|
+
*/
|
|
15
|
+
export declare function isAuthorityCommand(command: string): boolean;
|
|
16
|
+
export declare function isAuthorizedRunnerCommand(command: string): boolean;
|
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
export const RUNNER_DELEGATION_MESSAGE = "Remember that you must delegate this operation to the corresponding subagent.";
|
|
2
|
+
export const WORKTREE_MANAGER_MCP_SERVER = "worktree_manager";
|
|
3
|
+
export const WORKTREE_MANAGER_MCP_TOOLS = Object.freeze([
|
|
4
|
+
"worktree_create", "worktree_complete", "worktree_cleanup",
|
|
5
|
+
"worktree_recover", "worktree_list", "worktree_prune", "worktree_status",
|
|
6
|
+
]);
|
|
7
|
+
/** The runner may invoke exactly the seven capabilities the manager publishes. */
|
|
8
|
+
export function isWorktreeManagerMcpTool(tool) {
|
|
9
|
+
return typeof tool === "string"
|
|
10
|
+
&& WORKTREE_MANAGER_MCP_TOOLS.some((name) => tool === `${WORKTREE_MANAGER_MCP_SERVER}_${name}`);
|
|
11
|
+
}
|
|
12
|
+
export const FLOWTASK_SUBAGENTS = new Set([
|
|
13
|
+
"flowtask-ca-writer", "flowtask-planner", "flowtask-plan-auditor", "flowtask-constructor",
|
|
14
|
+
"flowtask-validator", "flowtask-initializer", "flowtask-logger", "flowtask-tester",
|
|
15
|
+
"flowtask-review-orchestrator", "flowtask-inspector", "flowtask-onboarder",
|
|
16
|
+
"flowtask-graphify-docs-media",
|
|
17
|
+
]);
|
|
18
|
+
/** V2 `subagent` resources the runner may select; the runner itself is never a child. */
|
|
19
|
+
export function isAuthorizedRunnerSubagent(agent) {
|
|
20
|
+
return typeof agent === "string" && FLOWTASK_SUBAGENTS.has(agent);
|
|
21
|
+
}
|
|
22
|
+
/** Tokenize the small shell grammar used by FlowTask commands. */
|
|
23
|
+
export function tokenizeCommand(command) {
|
|
24
|
+
const tokens = [];
|
|
25
|
+
let token = "";
|
|
26
|
+
let quote = "";
|
|
27
|
+
let escaped = false;
|
|
28
|
+
const trimmed = command.trim();
|
|
29
|
+
for (let index = 0; index < trimmed.length; index += 1) {
|
|
30
|
+
const char = trimmed[index];
|
|
31
|
+
if (escaped) {
|
|
32
|
+
token += char;
|
|
33
|
+
escaped = false;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (char === "\\") {
|
|
37
|
+
escaped = true;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (quote) {
|
|
41
|
+
if (char === quote)
|
|
42
|
+
quote = "";
|
|
43
|
+
else
|
|
44
|
+
token += char;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (char === "'" || char === '"') {
|
|
48
|
+
quote = char;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (char === ";" || char === "&" || char === "|" || char === "`" || char === "<" || char === ">" || char === "(" || char === ")")
|
|
52
|
+
return null;
|
|
53
|
+
if (char === "$" && trimmed[index + 1] === "(")
|
|
54
|
+
return null;
|
|
55
|
+
if (/\s/.test(char)) {
|
|
56
|
+
if (token) {
|
|
57
|
+
tokens.push(token);
|
|
58
|
+
token = "";
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
token += char;
|
|
63
|
+
}
|
|
64
|
+
if (escaped || quote)
|
|
65
|
+
return null;
|
|
66
|
+
if (token)
|
|
67
|
+
tokens.push(token);
|
|
68
|
+
return tokens;
|
|
69
|
+
}
|
|
70
|
+
function hasOnlyFlags(tokens) { return tokens.every((token) => token.startsWith("-")); }
|
|
71
|
+
function hasPaths(tokens) { return tokens.length > 0 && tokens.every((token) => !token.startsWith("-")); }
|
|
72
|
+
function hasFlagsAndPaths(tokens) {
|
|
73
|
+
let pathsStarted = false;
|
|
74
|
+
for (const token of tokens) {
|
|
75
|
+
if (token === "--") {
|
|
76
|
+
pathsStarted = true;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (token.startsWith("-") && !pathsStarted)
|
|
80
|
+
continue;
|
|
81
|
+
if (token.startsWith("-") && pathsStarted)
|
|
82
|
+
return false;
|
|
83
|
+
pathsStarted = true;
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
function isRedirectBoundary(char) {
|
|
88
|
+
return char === undefined || /\s/.test(char) || char === "&" || char === "|";
|
|
89
|
+
}
|
|
90
|
+
/** Consume one of the explicitly safe redirects and return the next index. */
|
|
91
|
+
function consumeDevNullRedirect(command, redirectIndex) {
|
|
92
|
+
if (command[redirectIndex + 1] === ">")
|
|
93
|
+
return null;
|
|
94
|
+
let targetIndex = redirectIndex + 1;
|
|
95
|
+
while (/\s/.test(command[targetIndex] ?? ""))
|
|
96
|
+
targetIndex += 1;
|
|
97
|
+
if (!command.startsWith("/dev/null", targetIndex))
|
|
98
|
+
return null;
|
|
99
|
+
const endIndex = targetIndex + "/dev/null".length;
|
|
100
|
+
return isRedirectBoundary(command[endIndex]) ? endIndex : null;
|
|
101
|
+
}
|
|
102
|
+
/** Split only on && and || while rejecting shell syntax outside the allowlist. */
|
|
103
|
+
function splitCommandSegments(command) {
|
|
104
|
+
const segments = [];
|
|
105
|
+
let segment = "";
|
|
106
|
+
let quote = "";
|
|
107
|
+
let escaped = false;
|
|
108
|
+
const pushSegment = () => {
|
|
109
|
+
if (!segment.trim())
|
|
110
|
+
return false;
|
|
111
|
+
segments.push(segment.trim());
|
|
112
|
+
segment = "";
|
|
113
|
+
return true;
|
|
114
|
+
};
|
|
115
|
+
for (let index = 0; index < command.length; index += 1) {
|
|
116
|
+
const char = command[index];
|
|
117
|
+
if (escaped) {
|
|
118
|
+
segment += `\\${char}`;
|
|
119
|
+
escaped = false;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (char === "\\") {
|
|
123
|
+
segment += char;
|
|
124
|
+
escaped = true;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (quote) {
|
|
128
|
+
segment += char;
|
|
129
|
+
if (char === quote)
|
|
130
|
+
quote = "";
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (char === "'" || char === '"') {
|
|
134
|
+
segment += char;
|
|
135
|
+
quote = char;
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (char === "&" && command[index + 1] === "&") {
|
|
139
|
+
if (!pushSegment())
|
|
140
|
+
return null;
|
|
141
|
+
index += 1;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (char === "|" && command[index + 1] === "|") {
|
|
145
|
+
if (!pushSegment())
|
|
146
|
+
return null;
|
|
147
|
+
index += 1;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (char === "&") {
|
|
151
|
+
if (command[index + 1] !== ">")
|
|
152
|
+
return null;
|
|
153
|
+
const nextIndex = consumeDevNullRedirect(command, index + 1);
|
|
154
|
+
if (nextIndex === null)
|
|
155
|
+
return null;
|
|
156
|
+
index = nextIndex - 1;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (char === "2" && command[index + 1] === ">" && (segment.length === 0 || /\s/.test(segment.at(-1) ?? ""))) {
|
|
160
|
+
const nextIndex = consumeDevNullRedirect(command, index + 1);
|
|
161
|
+
if (nextIndex === null)
|
|
162
|
+
return null;
|
|
163
|
+
index = nextIndex - 1;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (char === ">") {
|
|
167
|
+
const nextIndex = consumeDevNullRedirect(command, index);
|
|
168
|
+
if (nextIndex === null)
|
|
169
|
+
return null;
|
|
170
|
+
index = nextIndex - 1;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (char === ";" || char === "|" || char === "<" || char === "`" || char === "(" || char === ")")
|
|
174
|
+
return null;
|
|
175
|
+
if (char === "$" && command[index + 1] === "(")
|
|
176
|
+
return null;
|
|
177
|
+
segment += char;
|
|
178
|
+
}
|
|
179
|
+
if (escaped || quote || !pushSegment())
|
|
180
|
+
return null;
|
|
181
|
+
return segments;
|
|
182
|
+
}
|
|
183
|
+
/** Metacharacters bash expands or interprets alive inside double quotes. */
|
|
184
|
+
const DOUBLE_QUOTE_LIVE_METACHARACTERS = new Set(["$", "`", "|"]);
|
|
185
|
+
/**
|
|
186
|
+
* Reject quoting contexts bash treats differently from the tokenizer:
|
|
187
|
+
* `$()` substitution, backticks and pipes stay alive inside double quotes, and
|
|
188
|
+
* a backslash inside single quotes is literal in bash (so quote tracking could
|
|
189
|
+
* diverge from real shell parsing). Fails closed on any quoting ambiguity.
|
|
190
|
+
*/
|
|
191
|
+
function hasUnsafeQuoteContext(segment) {
|
|
192
|
+
let quote = "";
|
|
193
|
+
let escaped = false;
|
|
194
|
+
for (let index = 0; index < segment.length; index += 1) {
|
|
195
|
+
const char = segment[index];
|
|
196
|
+
if (quote === "'") {
|
|
197
|
+
if (char === "'")
|
|
198
|
+
quote = "";
|
|
199
|
+
else if (char === "\\")
|
|
200
|
+
return true;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (escaped) {
|
|
204
|
+
escaped = false;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (char === "\\") {
|
|
208
|
+
escaped = true;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (!quote) {
|
|
212
|
+
if (char === "'" || char === '"')
|
|
213
|
+
quote = char;
|
|
214
|
+
}
|
|
215
|
+
else if (quote === '"') {
|
|
216
|
+
if (char === '"')
|
|
217
|
+
quote = "";
|
|
218
|
+
else if (DOUBLE_QUOTE_LIVE_METACHARACTERS.has(char))
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return quote !== "";
|
|
223
|
+
}
|
|
224
|
+
/** Flags that give `git diff` write or external-driver side effects. */
|
|
225
|
+
const GIT_DIFF_EFFECT_FLAGS = new Set(["--output", "--ext-diff", "--textconv"]);
|
|
226
|
+
function hasEffectFlags(tokens) {
|
|
227
|
+
return tokens.some((token) => GIT_DIFF_EFFECT_FLAGS.has(token.split("=", 1)[0]));
|
|
228
|
+
}
|
|
229
|
+
const BLOCKED_COMMANDS = new Set([
|
|
230
|
+
"rm", "rmdir", "unlink", "shred", "mv", "cp", "install", "touch", "mkdir", "truncate", "chmod", "chown",
|
|
231
|
+
"tee", "dd", "sed", "perl", "python", "python3", "ruby", "bash", "sh", "zsh", "node",
|
|
232
|
+
"npx", "npm", "pnpm", "yarn", "bun", "deno", "make", "makepkg", "pytest", "jest", "vitest",
|
|
233
|
+
"tsc", "eslint", "biome", "docker", "curl", "wget", "sudo", "eval", "source", "exec", "awk", "gawk",
|
|
234
|
+
"env", "xargs", "pip", "pip3", "corepack", "webpack", "vite", "rollup",
|
|
235
|
+
]);
|
|
236
|
+
const BLOCKED_EFFECT_FLAGS = new Set([
|
|
237
|
+
"--force", "-f", "--delete", "--overwrite", "--output", "--exec", "--execute",
|
|
238
|
+
"--upload-pack", "--receive-pack", "--ext-diff", "--textconv",
|
|
239
|
+
]);
|
|
240
|
+
function hasBlockedEffects(tokens) {
|
|
241
|
+
const command = tokens[0]?.replaceAll("\\", "/").split("/").at(-1) ?? "";
|
|
242
|
+
if (BLOCKED_COMMANDS.has(command) || /^(?:python|python\d+(?:\.\d+)*|node\d*|ruby\d*|perl\d*|bash\d*|sh\d*)$/.test(command))
|
|
243
|
+
return true;
|
|
244
|
+
if (command === "git" && tokens.some((token) => BLOCKED_EFFECT_FLAGS.has(token.split("=", 1)[0])))
|
|
245
|
+
return true;
|
|
246
|
+
if (command === "find" && tokens.some((token) => ["-delete", "-exec", "-execdir", "-ok"].includes(token)))
|
|
247
|
+
return true;
|
|
248
|
+
if (/\.(?:sh|bash|zsh|py|pyc|rb|pl|js|mjs|cjs|ts|tsx|jsx)$/.test(tokens[0] ?? ""))
|
|
249
|
+
return true;
|
|
250
|
+
if (tokens.some((token) => BLOCKED_EFFECT_FLAGS.has(token.split("=", 1)[0])))
|
|
251
|
+
return true;
|
|
252
|
+
if (command === "git") {
|
|
253
|
+
const verb = tokens[1];
|
|
254
|
+
if (["checkout", "reset", "clean", "rebase", "cherry-pick", "apply", "am", "stash", "config", "init", "clone", "fetch", "pull", "push", "merge", "add", "commit", "restore", "switch", "tag", "gc", "prune"].includes(verb ?? ""))
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
/** Additional read-only flags allowed in git log (they do not count as a limit). */
|
|
260
|
+
const GIT_LOG_READONLY_FLAGS = new Set([
|
|
261
|
+
"--oneline", "--format", "--stat", "--name-only", "--no-merges",
|
|
262
|
+
"--first-parent", "--all", "--no-patch", "--decorate", "--graph",
|
|
263
|
+
"--abbrev-commit", "--no-abbrev-commit", "--oneline", "--raw",
|
|
264
|
+
]);
|
|
265
|
+
/**
|
|
266
|
+
* Validates git log arguments: requires a maximum limit of 10 commits.
|
|
267
|
+
* Accepts -n <N>, --max-count=<N>, and the short form -<N> with an integer 1..10.
|
|
268
|
+
* Allows additional read-only flags and paths after --.
|
|
269
|
+
* Rejects git log without a limit, with a limit > 10, or with unrecognized arguments.
|
|
270
|
+
*/
|
|
271
|
+
function isAuthorizedGitLog(args) {
|
|
272
|
+
let hasLimit = false;
|
|
273
|
+
let index = 0;
|
|
274
|
+
while (index < args.length) {
|
|
275
|
+
const token = args[index];
|
|
276
|
+
// Paths after --
|
|
277
|
+
if (token === "--")
|
|
278
|
+
return hasLimit;
|
|
279
|
+
// Short form -<N> (e.g. -5, -10)
|
|
280
|
+
if (/^-[1-9]\d*$/.test(token)) {
|
|
281
|
+
const n = Number(token.slice(1));
|
|
282
|
+
if (n < 1 || n > 10)
|
|
283
|
+
return false;
|
|
284
|
+
hasLimit = true;
|
|
285
|
+
index += 1;
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
// -n <N> or --max-count=<N>
|
|
289
|
+
if (token === "-n" || token === "--max-count") {
|
|
290
|
+
const next = args[index + 1];
|
|
291
|
+
if (!next || !/^\d+$/.test(next))
|
|
292
|
+
return false;
|
|
293
|
+
const n = Number(next);
|
|
294
|
+
if (n < 1 || n > 10)
|
|
295
|
+
return false;
|
|
296
|
+
hasLimit = true;
|
|
297
|
+
index += 2;
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (token.startsWith("--max-count=")) {
|
|
301
|
+
const n = Number(token.slice("--max-count=".length));
|
|
302
|
+
if (!Number.isInteger(n) || n < 1 || n > 10)
|
|
303
|
+
return false;
|
|
304
|
+
hasLimit = true;
|
|
305
|
+
index += 1;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
// --format=<val> or --format <val> (consume the value if present)
|
|
309
|
+
if (token.startsWith("--format=") || token === "--format") {
|
|
310
|
+
if (token === "--format")
|
|
311
|
+
index += 1; // skip the value
|
|
312
|
+
index += 1;
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
// Known read-only flags (no value)
|
|
316
|
+
if (GIT_LOG_READONLY_FLAGS.has(token)) {
|
|
317
|
+
index += 1;
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
// Any other unrecognized token → reject
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
return hasLimit;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Exactly the authority commands that may cross the layer denial:
|
|
327
|
+
* `flowtask authority status [--json]`, `flowtask repair [--json]`, and their
|
|
328
|
+
* `node .flowtask/bin/flowtask.js ...` forms. Nothing is composed or widened.
|
|
329
|
+
*/
|
|
330
|
+
function authorityTokens(tokens) {
|
|
331
|
+
const base = tokens[0] === "node" && tokens[1] === ".flowtask/bin/flowtask.js"
|
|
332
|
+
? tokens.slice(2)
|
|
333
|
+
: tokens[0] === "flowtask"
|
|
334
|
+
? tokens.slice(1)
|
|
335
|
+
: null;
|
|
336
|
+
if (!base)
|
|
337
|
+
return false;
|
|
338
|
+
if (base[0] === "authority") {
|
|
339
|
+
return base.length >= 2
|
|
340
|
+
&& base[1] === "status"
|
|
341
|
+
&& (base.length === 2 || (base.length === 3 && base[2] === "--json"));
|
|
342
|
+
}
|
|
343
|
+
if (base[0] === "repair") {
|
|
344
|
+
return base.length === 1 || (base.length === 2 && base[1] === "--json");
|
|
345
|
+
}
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* True only for a single, exact authority command. Any compound, redirect, or
|
|
350
|
+
* unknown token makes the command ineligible for the layer bypass.
|
|
351
|
+
*/
|
|
352
|
+
export function isAuthorityCommand(command) {
|
|
353
|
+
const segments = splitCommandSegments(command);
|
|
354
|
+
if (!segments || segments.length !== 1)
|
|
355
|
+
return false;
|
|
356
|
+
if (hasUnsafeQuoteContext(segments[0]))
|
|
357
|
+
return false;
|
|
358
|
+
const tokens = tokenizeCommand(segments[0]);
|
|
359
|
+
return Boolean(tokens?.length && authorityTokens(tokens));
|
|
360
|
+
}
|
|
361
|
+
export function isAuthorizedRunnerCommand(command) {
|
|
362
|
+
const segments = splitCommandSegments(command);
|
|
363
|
+
if (!segments)
|
|
364
|
+
return false;
|
|
365
|
+
return segments.every((segment) => {
|
|
366
|
+
if (hasUnsafeQuoteContext(segment))
|
|
367
|
+
return false;
|
|
368
|
+
const tokens = tokenizeCommand(segment);
|
|
369
|
+
if (!tokens?.length)
|
|
370
|
+
return false;
|
|
371
|
+
if (authorityTokens(tokens))
|
|
372
|
+
return true;
|
|
373
|
+
if (tokens[0] === "node" && tokens[1] === ".flowtask/bin/flowtask.js" && tokens[2] === "graphify")
|
|
374
|
+
return tokens.length > 3;
|
|
375
|
+
const worktreeScript = tokens[0].replaceAll("\\", "/");
|
|
376
|
+
if (worktreeScript === "./.flowtask/scripts/worktree.sh" || worktreeScript.endsWith("/flowtask/scripts/worktree.sh")) {
|
|
377
|
+
// The request protocol is reserved for the consumer: the runner never
|
|
378
|
+
// publishes a canonical request through the shell.
|
|
379
|
+
if (tokens.includes("--request-json"))
|
|
380
|
+
return false;
|
|
381
|
+
if (tokens.length === 2 && ["list", "prune"].includes(tokens[1]))
|
|
382
|
+
return true;
|
|
383
|
+
if (tokens[1] === "recover" && tokens.length <= 3)
|
|
384
|
+
return true; // recover | recover <tx-id>
|
|
385
|
+
if (tokens[1] === "create") {
|
|
386
|
+
return tokens.length === 5 && Boolean(tokens[2]) && tokens[3] === "--base" && Boolean(tokens[4]);
|
|
387
|
+
}
|
|
388
|
+
if (tokens[1] === "complete") {
|
|
389
|
+
if (!Boolean(tokens[2]) || tokens[2] === "--base" || tokens[2].startsWith("--"))
|
|
390
|
+
return false;
|
|
391
|
+
const args = tokens.slice(3);
|
|
392
|
+
const seen = new Set();
|
|
393
|
+
let i = 0;
|
|
394
|
+
while (i < args.length) {
|
|
395
|
+
const flag = args[i];
|
|
396
|
+
if (flag === "--preserve-dirty") {
|
|
397
|
+
if (seen.has(flag))
|
|
398
|
+
return false;
|
|
399
|
+
seen.add(flag);
|
|
400
|
+
i += 1;
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
if (flag === "--base" || flag === "--title" || flag === "--body") {
|
|
404
|
+
if (seen.has(flag))
|
|
405
|
+
return false;
|
|
406
|
+
if (i + 1 >= args.length)
|
|
407
|
+
return false;
|
|
408
|
+
const value = args[i + 1];
|
|
409
|
+
if (!value || value.startsWith("--"))
|
|
410
|
+
return false;
|
|
411
|
+
seen.add(flag);
|
|
412
|
+
i += 2;
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
return false;
|
|
416
|
+
}
|
|
417
|
+
return true;
|
|
418
|
+
}
|
|
419
|
+
if (tokens[1] === "cleanup") {
|
|
420
|
+
if (!Boolean(tokens[2]) || tokens[2] === "--base" || tokens[2].startsWith("--"))
|
|
421
|
+
return false;
|
|
422
|
+
const args = tokens.slice(3);
|
|
423
|
+
const ownFlag = "--discard";
|
|
424
|
+
if (args.length === 0)
|
|
425
|
+
return true;
|
|
426
|
+
if (args.length === 1 && args[0] === ownFlag)
|
|
427
|
+
return true;
|
|
428
|
+
if (args.length === 2 && args[0] === "--base" && Boolean(args[1]))
|
|
429
|
+
return true;
|
|
430
|
+
if (args.length === 3 && args[0] === "--base" && Boolean(args[1]) && args[2] === ownFlag)
|
|
431
|
+
return true;
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
if (tokens[0] === "ls")
|
|
437
|
+
return hasFlagsAndPaths(tokens.slice(1));
|
|
438
|
+
if (tokens[0] === "cat")
|
|
439
|
+
return tokens.length >= 2; // read-only file access for runner diagnostics
|
|
440
|
+
if (tokens[0] === "echo")
|
|
441
|
+
return true;
|
|
442
|
+
if (tokens[0] !== "git")
|
|
443
|
+
return !hasBlockedEffects(tokens);
|
|
444
|
+
if (tokens[1] === "worktree" && tokens[2] === "list")
|
|
445
|
+
return hasOnlyFlags(tokens.slice(3));
|
|
446
|
+
if (tokens[1] === "diff") {
|
|
447
|
+
const rest = tokens.slice(2);
|
|
448
|
+
return !hasEffectFlags(rest) && hasFlagsAndPaths(rest);
|
|
449
|
+
}
|
|
450
|
+
if (tokens[1] === "status")
|
|
451
|
+
return hasOnlyFlags(tokens.slice(2));
|
|
452
|
+
if (tokens[1] === "add")
|
|
453
|
+
return hasPaths(tokens.slice(2));
|
|
454
|
+
if (tokens[1] === "restore" && tokens[2] === "--staged")
|
|
455
|
+
return hasPaths(tokens.slice(3));
|
|
456
|
+
if (tokens[1] === "commit")
|
|
457
|
+
return tokens.length === 4 && tokens[2] === "-m" && Boolean(tokens[3]);
|
|
458
|
+
if (tokens[1] === "push" || tokens[1] === "merge")
|
|
459
|
+
return tokens.length >= 2;
|
|
460
|
+
// Git read-only commands for runner coordination and diagnostics
|
|
461
|
+
if (tokens[1] === "branch")
|
|
462
|
+
return tokens.length >= 2 && hasOnlyFlags(tokens.slice(2));
|
|
463
|
+
if (tokens[1] === "log")
|
|
464
|
+
return isAuthorizedGitLog(tokens.slice(2));
|
|
465
|
+
if (tokens[1] === "show")
|
|
466
|
+
return tokens.length >= 2;
|
|
467
|
+
if (tokens[1] === "rev-parse")
|
|
468
|
+
return hasFlagsAndPaths(tokens.slice(2));
|
|
469
|
+
// Git is default-deny: only explicitly reviewed read-only/admin commands
|
|
470
|
+
// and the existing narrowly-shaped coordination commands are authorized.
|
|
471
|
+
return false;
|
|
472
|
+
});
|
|
473
|
+
}
|