flowtask-permission-gate 2.0.0 → 2.0.1

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/README.md CHANGED
@@ -27,10 +27,18 @@ without the marker is preserved.
27
27
 
28
28
  ## What it enforces
29
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.
30
+ - **Runner allowlist.** The runner's shell tool is `shell`, and `bash` receives
31
+ exactly the same command analysis with the same result for the same command.
32
+ Authorized inspection and management commands run; destructive, writing,
33
+ installing, and ambiguous commands are denied with the delegation message.
34
+ Only the enumerated subagents, MCP tools, and Graphify queries are authorized.
35
+ - **Code Mode transport.** An `execute` script is authorized only when it
36
+ reaches the MCP surface through literal calls — `tools.engram.<tool>`,
37
+ `tools.graphify.query_graph`, and `tools.worktree_manager.<capability>` — with
38
+ at least one invocation. `fetch`, `eval`, `Function`, `require`, `process`,
39
+ any other namespace, a tool outside the surface, a non-literal reference, or a
40
+ mixed script rejects the whole script with the delegation message and no
41
+ effect.
34
42
  - **Permissions layer.** The Runner does not execute with expanded limits when
35
43
  the autonomy or permissions layer is missing or invalid. The denial names the
36
44
  layer, the file, the cause, and the action.
@@ -83,6 +91,8 @@ Both files are read from `.opencode/flowtask/config/` first and from
83
91
  - Commit detection covers `git commit`, `git -C`, `git -c`, `--git-dir`, and
84
92
  `cd ... && git commit`. It does not inspect commits started by the worktree
85
93
  manager or through nested shells.
94
+ - The `execute` script analysis is static: a reference that is not literal (a
95
+ computed name, an intermediate alias, or a third segment) is rejected.
86
96
  - The plugin does not restart OpenCode; a new version is loaded on the next
87
97
  restart.
88
98
 
@@ -1,8 +1,18 @@
1
1
  export declare const RUNNER_DELEGATION_MESSAGE = "Remember that you must delegate this operation to the corresponding subagent.";
2
+ /** Host shell names that share one command analysis and one result per command. */
3
+ export declare const RUNNER_SHELL_TOOLS: readonly ["bash", "shell"];
4
+ /** Both host shell names receive exactly the same command analysis. */
5
+ export declare function isRunnerShellTool(tool: unknown): boolean;
2
6
  export declare const WORKTREE_MANAGER_MCP_SERVER = "worktree_manager";
3
7
  export declare const WORKTREE_MANAGER_MCP_TOOLS: readonly ["worktree_create", "worktree_complete", "worktree_cleanup", "worktree_recover", "worktree_list", "worktree_prune", "worktree_status"];
4
8
  /** The runner may invoke exactly the seven capabilities the manager publishes. */
5
9
  export declare function isWorktreeManagerMcpTool(tool: unknown): boolean;
10
+ /**
11
+ * The MCP surface the runner may invoke: the seven worktree-manager
12
+ * capabilities, Engram tools with the `engram_` prefix, and the single Graphify
13
+ * query. Any other MCP server or tool stays outside the surface.
14
+ */
15
+ export declare function isAuthorizedRunnerMcpTool(tool: unknown): boolean;
6
16
  export declare const FLOWTASK_SUBAGENTS: Set<string>;
7
17
  /** V2 `subagent` resources the runner may select; the runner itself is never a child. */
8
18
  export declare function isAuthorizedRunnerSubagent(agent: unknown): boolean;
@@ -1,4 +1,10 @@
1
1
  export const RUNNER_DELEGATION_MESSAGE = "Remember that you must delegate this operation to the corresponding subagent.";
2
+ /** Host shell names that share one command analysis and one result per command. */
3
+ export const RUNNER_SHELL_TOOLS = Object.freeze(["bash", "shell"]);
4
+ /** Both host shell names receive exactly the same command analysis. */
5
+ export function isRunnerShellTool(tool) {
6
+ return typeof tool === "string" && RUNNER_SHELL_TOOLS.some((name) => name === tool);
7
+ }
2
8
  export const WORKTREE_MANAGER_MCP_SERVER = "worktree_manager";
3
9
  export const WORKTREE_MANAGER_MCP_TOOLS = Object.freeze([
4
10
  "worktree_create", "worktree_complete", "worktree_cleanup",
@@ -9,6 +15,16 @@ export function isWorktreeManagerMcpTool(tool) {
9
15
  return typeof tool === "string"
10
16
  && WORKTREE_MANAGER_MCP_TOOLS.some((name) => tool === `${WORKTREE_MANAGER_MCP_SERVER}_${name}`);
11
17
  }
18
+ /**
19
+ * The MCP surface the runner may invoke: the seven worktree-manager
20
+ * capabilities, Engram tools with the `engram_` prefix, and the single Graphify
21
+ * query. Any other MCP server or tool stays outside the surface.
22
+ */
23
+ export function isAuthorizedRunnerMcpTool(tool) {
24
+ return isWorktreeManagerMcpTool(tool)
25
+ || (typeof tool === "string" && tool.startsWith("engram_"))
26
+ || tool === "graphify_query_graph";
27
+ }
12
28
  export const FLOWTASK_SUBAGENTS = new Set([
13
29
  "flowtask-ca-writer", "flowtask-planner", "flowtask-plan-auditor", "flowtask-constructor",
14
30
  "flowtask-validator", "flowtask-initializer", "flowtask-logger", "flowtask-tester",
@@ -0,0 +1,9 @@
1
+ export declare const RUNNER_EXECUTE_TOOL = "execute";
2
+ /**
3
+ * Authorizes an `execute` script only when every `tools` access is one of the
4
+ * two contiguous literal segments followed immediately by `(`, and the resolved
5
+ * MCP name belongs to the runner surface. Any forbidden identifier, unresolved
6
+ * reference, or missing invocation rejects the whole script: nothing is
7
+ * rewritten and no part of it runs.
8
+ */
9
+ export declare function isAuthorizedRunnerScript(script: unknown): boolean;
@@ -0,0 +1,163 @@
1
+ import { isAuthorizedRunnerMcpTool } from "./authorizer.js";
2
+ export const RUNNER_EXECUTE_TOOL = "execute";
3
+ /** Identifiers that would add execution primitives or reach outside the MCP surface. */
4
+ const FORBIDDEN_IDENTIFIERS = new Set(["fetch", "eval", "Function", "require", "process"]);
5
+ const WORD_CHARACTER = /[A-Za-z0-9_$]/;
6
+ /**
7
+ * Positions that carry code: comments and single- or double-quoted literals are
8
+ * data. Template literals stay code because their interpolations execute.
9
+ * Returns null when a literal or block comment is unterminated so the caller
10
+ * rejects the script.
11
+ */
12
+ function buildCodeMask(script) {
13
+ const mask = new Array(script.length).fill(true);
14
+ let index = 0;
15
+ while (index < script.length) {
16
+ const char = script[index];
17
+ if (char === "/" && script[index + 1] === "/") {
18
+ mask[index] = false;
19
+ mask[index + 1] = false;
20
+ index += 2;
21
+ while (index < script.length && script[index] !== "\n") {
22
+ mask[index] = false;
23
+ index += 1;
24
+ }
25
+ continue;
26
+ }
27
+ if (char === "/" && script[index + 1] === "*") {
28
+ mask[index] = false;
29
+ mask[index + 1] = false;
30
+ index += 2;
31
+ let closed = false;
32
+ while (index < script.length) {
33
+ if (script[index] === "*" && script[index + 1] === "/") {
34
+ mask[index] = false;
35
+ mask[index + 1] = false;
36
+ index += 2;
37
+ closed = true;
38
+ break;
39
+ }
40
+ mask[index] = false;
41
+ index += 1;
42
+ }
43
+ if (!closed)
44
+ return null;
45
+ continue;
46
+ }
47
+ if (char === "'" || char === '"') {
48
+ const quote = char;
49
+ mask[index] = false;
50
+ index += 1;
51
+ let closed = false;
52
+ while (index < script.length) {
53
+ const current = script[index];
54
+ mask[index] = false;
55
+ if (current === "\\") {
56
+ if (index + 1 < script.length)
57
+ mask[index + 1] = false;
58
+ index += 2;
59
+ continue;
60
+ }
61
+ index += 1;
62
+ if (current === quote) {
63
+ closed = true;
64
+ break;
65
+ }
66
+ }
67
+ if (!closed)
68
+ return null;
69
+ continue;
70
+ }
71
+ index += 1;
72
+ }
73
+ return mask;
74
+ }
75
+ /**
76
+ * Reads one contiguous `.name` or `["name"]` segment starting at `index`.
77
+ * Returns null unless the segment is in code position, has a non-empty name,
78
+ * and ends exactly where the next segment may start.
79
+ */
80
+ function readToolSegment(script, mask, index) {
81
+ if (index >= script.length || !mask[index])
82
+ return null;
83
+ if (script[index] === ".") {
84
+ const start = index + 1;
85
+ if (start >= script.length || !mask[start] || !WORD_CHARACTER.test(script[start]))
86
+ return null;
87
+ let end = start;
88
+ while (end < script.length && mask[end] && WORD_CHARACTER.test(script[end]))
89
+ end += 1;
90
+ return { name: script.slice(start, end), end };
91
+ }
92
+ if (script[index] === "[") {
93
+ const quote = script[index + 1];
94
+ if (quote !== "'" && quote !== '"')
95
+ return null;
96
+ let end = index + 2;
97
+ let name = "";
98
+ while (end < script.length) {
99
+ const current = script[end];
100
+ if (current === "\\") {
101
+ if (end + 1 >= script.length)
102
+ return null;
103
+ name += script[end + 1];
104
+ end += 2;
105
+ continue;
106
+ }
107
+ if (current === quote) {
108
+ if (!name)
109
+ return null;
110
+ if (script[end + 1] !== "]")
111
+ return null;
112
+ return { name, end: end + 2 };
113
+ }
114
+ name += current;
115
+ end += 1;
116
+ }
117
+ }
118
+ return null;
119
+ }
120
+ /**
121
+ * Authorizes an `execute` script only when every `tools` access is one of the
122
+ * two contiguous literal segments followed immediately by `(`, and the resolved
123
+ * MCP name belongs to the runner surface. Any forbidden identifier, unresolved
124
+ * reference, or missing invocation rejects the whole script: nothing is
125
+ * rewritten and no part of it runs.
126
+ */
127
+ export function isAuthorizedRunnerScript(script) {
128
+ if (typeof script !== "string" || !script)
129
+ return false;
130
+ const mask = buildCodeMask(script);
131
+ if (!mask)
132
+ return false;
133
+ let authorizedReferences = 0;
134
+ for (let index = 0; index < script.length; index += 1) {
135
+ if (!mask[index] || !WORD_CHARACTER.test(script[index]))
136
+ continue;
137
+ if (index > 0 && mask[index - 1] && WORD_CHARACTER.test(script[index - 1]))
138
+ continue;
139
+ let end = index;
140
+ while (end < script.length && mask[end] && WORD_CHARACTER.test(script[end]))
141
+ end += 1;
142
+ const word = script.slice(index, end);
143
+ if (FORBIDDEN_IDENTIFIERS.has(word))
144
+ return false;
145
+ if (word !== "tools")
146
+ continue;
147
+ const server = readToolSegment(script, mask, end);
148
+ if (!server)
149
+ return false;
150
+ const capability = readToolSegment(script, mask, server.end);
151
+ if (!capability)
152
+ return false;
153
+ const next = script[capability.end];
154
+ if (next === "." || next === "[")
155
+ return false;
156
+ if (next !== "(")
157
+ return false;
158
+ if (!isAuthorizedRunnerMcpTool(`${server.name}_${capability.name}`))
159
+ return false;
160
+ authorizedReferences += 1;
161
+ }
162
+ return authorizedReferences > 0;
163
+ }
package/dist/index.js CHANGED
@@ -2,7 +2,8 @@ import { Plugin } from "@opencode/plugin";
2
2
  import { execFileSync } from "node:child_process";
3
3
  import { readFileSync, statSync } from "node:fs";
4
4
  import { isAbsolute, resolve } from "node:path";
5
- import { RUNNER_DELEGATION_MESSAGE, isAuthorizedRunnerCommand, isAuthorizedRunnerSubagent, isAuthorityCommand, isWorktreeManagerMcpTool, } from "./authorizer.js";
5
+ import { RUNNER_DELEGATION_MESSAGE, isAuthorizedRunnerCommand, isAuthorizedRunnerMcpTool, isAuthorizedRunnerSubagent, isAuthorityCommand, isRunnerShellTool, } from "./authorizer.js";
6
+ import { RUNNER_EXECUTE_TOOL, isAuthorizedRunnerScript } from "./code-mode.js";
6
7
  import { buildLayerBlockMessage, readOperationalLayers, readOperationalLayersAt } from "./layers.js";
7
8
  import { evaluateOpencodeActivation, findOpencodeRegistration, } from "./registration.js";
8
9
  import { applyReducedDelivery, buildAttachmentDelivery, buildDegradationMetadata, buildNarrowingNotice, buildReducedDelivery, isImageOrPdfOutput, loadContextBudgetConfig, readDeliveredText, readRequestedPath, registerNarrowingNotice, resetNarrowingNotices, shouldPreventiveBlockGlob, shouldPreventiveBlockGrep, shouldPreventiveBlockRead, } from "./context-budget.js";
@@ -167,15 +168,13 @@ function isGitCommitCommand(command) {
167
168
  return false;
168
169
  }
169
170
  function runnerToolAllowed(tool, args) {
170
- if (isWorktreeManagerMcpTool(tool))
171
+ if (isAuthorizedRunnerMcpTool(tool))
171
172
  return true;
172
- if (tool === "bash")
173
+ if (isRunnerShellTool(tool))
173
174
  return isAuthorizedRunnerCommand(String(args.command ?? ""));
174
- if (tool === "read" || tool === "glob" || tool === "grep" || tool === "skill" || tool.startsWith("engram_"))
175
- return true;
176
- // MCP Graphify: the only MCP tool authorized for the runner (path 1 of the research chain).
177
- // Do not expand to other graphify tools — only the exact enumerated tool.
178
- if (tool === "graphify_query_graph")
175
+ if (tool === RUNNER_EXECUTE_TOOL)
176
+ return isAuthorizedRunnerScript(args.code);
177
+ if (tool === "read" || tool === "glob" || tool === "grep" || tool === "skill")
179
178
  return true;
180
179
  if (tool === "subagent")
181
180
  return isAuthorizedRunnerSubagent(args.agent);
@@ -478,7 +477,7 @@ export default Plugin.define({
478
477
  ? readOperationalLayersAt(ctx.options.configDir)
479
478
  : readOperationalLayers([workdir, sessionDir]);
480
479
  if (!operationalLayers.ok) {
481
- const authorityBypass = event.tool === "bash" && isAuthorityCommand(command);
480
+ const authorityBypass = isRunnerShellTool(event.tool) && isAuthorityCommand(command);
482
481
  if (!authorityBypass)
483
482
  throw new Error(buildLayerBlockMessage(operationalLayers));
484
483
  }
@@ -501,7 +500,7 @@ export default Plugin.define({
501
500
  throw new Error(buildNarrowingNotice(budgetTool, input));
502
501
  }
503
502
  }
504
- if (event.tool !== "bash" || !isGitCommitCommand(command))
503
+ if (!isRunnerShellTool(event.tool) || !isGitCommitCommand(command))
505
504
  return;
506
505
  if (command.includes("--no-verify") || command.includes("--no-review")) {
507
506
  // The bypass is only legitimate for trivial changes (≤ TRIVIAL_CHANGE_LINES).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flowtask-permission-gate",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "OpenCode permission gate for FlowTask: runner allowlist, review stamps, and context budget.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",