@comment-labs/opencode-hidden-guard 0.1.0 → 0.1.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.
- package/dist/index.d.mts +1 -14
- package/dist/index.mjs +153 -135
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
//#region src/core.d.ts
|
|
2
|
-
type HiddenGuardOptions = {
|
|
3
|
-
/** dotfile basenames that are allowed to be read (eg. [".bashrc"]) */
|
|
4
|
-
allow?: string[];
|
|
5
|
-
disabled?: boolean;
|
|
6
|
-
};
|
|
7
|
-
//#endregion
|
|
8
|
-
//#region src/v1.d.ts
|
|
9
|
-
type V1Hooks = Record<string, unknown>;
|
|
10
|
-
declare const v1: (_input: unknown, options?: HiddenGuardOptions) => Promise<V1Hooks>;
|
|
11
|
-
//#endregion
|
|
12
|
-
//#region src/index.d.ts
|
|
13
1
|
declare const _default: import("@opencode/plugin/promise/plugin").Plugin;
|
|
14
|
-
|
|
15
|
-
export { _default as default, v1 };
|
|
2
|
+
export { _default as default };
|
|
16
3
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
import { Plugin } from "@opencode/plugin";
|
|
2
2
|
import { parse } from "unbash";
|
|
3
|
-
|
|
3
|
+
// src/core.ts
|
|
4
4
|
/**
|
|
5
5
|
* hidden-guard core: dotfile-read detection for tool args and bash
|
|
6
|
-
* commands. Framework-agnostic —
|
|
6
|
+
* commands. Framework-agnostic — the V2 adapter runs this core.
|
|
7
7
|
*/
|
|
8
|
+
const PATH_SEGMENT_SPLIT_REGEX = /[\\/]/u;
|
|
9
|
+
const PATH_ISH_REGEX = /^[.~/]/u;
|
|
10
|
+
const URL_REGEX = /:\/\//u;
|
|
8
11
|
function isWord(n) {
|
|
9
|
-
return typeof n === "object" && n !== null && typeof n.text === "string" && !n.type;
|
|
12
|
+
return typeof n === "object" && n !== null && "text" in n && typeof n.text === "string" && !("type" in n && n.type !== void 0 && n.type !== "");
|
|
10
13
|
}
|
|
11
|
-
/**
|
|
12
|
-
*
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Dotfile DIRECTORIES that are infrastructure, not secrets — allowed as
|
|
16
|
+
* any path segment (.config, .git, build caches …).
|
|
17
|
+
*/
|
|
15
18
|
const INFRA_SEGMENTS = /* @__PURE__ */ new Set([
|
|
16
19
|
".config",
|
|
17
20
|
".local",
|
|
@@ -49,7 +52,9 @@ const INFRA_SEGMENTS = /* @__PURE__ */ new Set([
|
|
|
49
52
|
".expo",
|
|
50
53
|
".docusaurus"
|
|
51
54
|
]);
|
|
52
|
-
/**
|
|
55
|
+
/**
|
|
56
|
+
* Dotfile NAMES that are code/config, not secrets — allowed as leaves.
|
|
57
|
+
*/
|
|
53
58
|
const INFRA_FILES = /* @__PURE__ */ new Set([
|
|
54
59
|
".gitignore",
|
|
55
60
|
".gitattributes",
|
|
@@ -82,9 +87,11 @@ const INFRA_FILES = /* @__PURE__ */ new Set([
|
|
|
82
87
|
".taplo.toml",
|
|
83
88
|
".latexmkrc"
|
|
84
89
|
]);
|
|
85
|
-
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
90
|
+
/**
|
|
91
|
+
* Command flags whose VALUE is an expression — a jq filter, Go template,
|
|
92
|
+
* or JMESPath — never a file path. The word after one of these is not a
|
|
93
|
+
* file argument (gh --jq .headSha, aws --query '.Tags[0].Value' …).
|
|
94
|
+
*/
|
|
88
95
|
const EXPR_VALUE_FLAGS = {
|
|
89
96
|
gh: /* @__PURE__ */ new Set([
|
|
90
97
|
"--jq",
|
|
@@ -94,16 +101,20 @@ const EXPR_VALUE_FLAGS = {
|
|
|
94
101
|
aws: /* @__PURE__ */ new Set(["--query"]),
|
|
95
102
|
az: /* @__PURE__ */ new Set(["--query"])
|
|
96
103
|
};
|
|
97
|
-
/**
|
|
104
|
+
/**
|
|
105
|
+
* jq/yq subcommand aliases that are not the filter itself (yq eval …).
|
|
106
|
+
*/
|
|
98
107
|
const FILTER_EVAL_WORDS = /* @__PURE__ */ new Set([
|
|
99
108
|
"e",
|
|
100
109
|
"eval",
|
|
101
110
|
"eval-all",
|
|
102
111
|
"ea"
|
|
103
112
|
]);
|
|
104
|
-
/**
|
|
105
|
-
*
|
|
106
|
-
*
|
|
113
|
+
/**
|
|
114
|
+
* jq flags that consume words from the stream; those words are file (or
|
|
115
|
+
* variable) arguments — they are checked as files and never mistaken for
|
|
116
|
+
* the filter expression.
|
|
117
|
+
*/
|
|
107
118
|
const BASH_VALUE_FLAGS = {
|
|
108
119
|
jq: {
|
|
109
120
|
"-f": 1,
|
|
@@ -113,54 +124,65 @@ const BASH_VALUE_FLAGS = {
|
|
|
113
124
|
},
|
|
114
125
|
yq: {}
|
|
115
126
|
};
|
|
116
|
-
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
127
|
+
/**
|
|
128
|
+
* Any path segment that is a hidden file/dir (`.secrets/tokens.json` is
|
|
129
|
+
* hidden because of `.secrets`, not its basename). Returns the first
|
|
130
|
+
* hidden segment, or undefined.
|
|
131
|
+
*/
|
|
119
132
|
function pathHasHidden(s, opts = {}) {
|
|
120
|
-
if (
|
|
121
|
-
for (const seg of s.split(
|
|
122
|
-
if (
|
|
133
|
+
if (s === "" || s.length > 4096) return;
|
|
134
|
+
for (const seg of s.split(PATH_SEGMENT_SPLIT_REGEX)) {
|
|
135
|
+
if (seg === "" || [
|
|
136
|
+
".",
|
|
137
|
+
"..",
|
|
138
|
+
"~"
|
|
139
|
+
].includes(seg)) continue;
|
|
123
140
|
if (!seg.startsWith(".")) continue;
|
|
124
141
|
if (INFRA_SEGMENTS.has(seg)) continue;
|
|
125
142
|
if (INFRA_FILES.has(seg)) continue;
|
|
126
143
|
if (!(opts.allow ?? []).includes(seg)) return seg;
|
|
127
144
|
}
|
|
128
145
|
}
|
|
129
|
-
|
|
130
|
-
|
|
146
|
+
function isPlainObject(v) {
|
|
147
|
+
return typeof v === "object" && v !== null;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* True when a tool argument object references a hidden file in any
|
|
151
|
+
* path-ish string value (filePath, path, file, or nested arrays of them).
|
|
152
|
+
*/
|
|
131
153
|
function argsTouchHidden(args, opts = {}) {
|
|
132
154
|
const seen = [];
|
|
133
155
|
const visit = (v, depth) => {
|
|
134
|
-
if (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const s = v;
|
|
138
|
-
if (s.length > 0 && s.length < 4096 && /^[.~/]/.test(s)) {
|
|
139
|
-
const base = pathHasHidden(s, opts);
|
|
140
|
-
if (base) return base;
|
|
141
|
-
}
|
|
156
|
+
if (v === null || v === void 0 || depth > 6) return;
|
|
157
|
+
if (typeof v === "string") {
|
|
158
|
+
if (v.length > 0 && v.length < 4096 && PATH_ISH_REGEX.test(v)) return pathHasHidden(v, opts);
|
|
142
159
|
return;
|
|
143
160
|
}
|
|
144
|
-
if (
|
|
145
|
-
if (seen.includes(v)) return
|
|
161
|
+
if (!isPlainObject(v)) return;
|
|
162
|
+
if (seen.includes(v)) return;
|
|
146
163
|
seen.push(v);
|
|
147
164
|
for (const [k, val] of Object.entries(v)) {
|
|
148
165
|
if (k === "text" || k === "description") continue;
|
|
149
166
|
const hit = visit(val, depth + 1);
|
|
150
|
-
if (hit) return hit;
|
|
167
|
+
if (hit !== void 0) return hit;
|
|
151
168
|
}
|
|
152
169
|
};
|
|
153
170
|
return visit(args, 0);
|
|
154
171
|
}
|
|
155
172
|
const MSG = (name, what) => `hidden-guard BLOCKED: \`${name}\` is a hidden (dot) file. ${what} Reading hidden files is not allowed (they hold credentials and local state). If you truly need it, ask the user to paste the relevant part.`;
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
*
|
|
161
|
-
*
|
|
173
|
+
function blockMessage(name) {
|
|
174
|
+
return MSG(name, "Requested via a tool.");
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Bash layer: any positional file argument whose basename is a hidden file
|
|
178
|
+
* (any command, any reader — cat .env, wc -l .foo, strings .secret …).
|
|
179
|
+
* NOT file args: flags; the value word of expression flags (--jq, --query,
|
|
180
|
+
* --template — jq/JMESPath/Go-template expressions, never paths); value
|
|
181
|
+
* words of known value-flags (checked as files); and the first positional
|
|
182
|
+
* of jq/yq (the filter expression — files come after it).
|
|
183
|
+
*/
|
|
162
184
|
function bashTouchesHidden(command, opts = {}) {
|
|
163
|
-
if (opts.disabled) return
|
|
185
|
+
if (opts.disabled === true) return;
|
|
164
186
|
let ast;
|
|
165
187
|
try {
|
|
166
188
|
ast = parse(command);
|
|
@@ -168,122 +190,118 @@ function bashTouchesHidden(command, opts = {}) {
|
|
|
168
190
|
return;
|
|
169
191
|
}
|
|
170
192
|
const found = [];
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
193
|
+
walkBashAst(ast, found, opts);
|
|
194
|
+
if (found.length === 0) return;
|
|
195
|
+
return MSG(found.join(", "), "Found it in a bash command.");
|
|
196
|
+
}
|
|
197
|
+
function walkBashAst(node, found, opts, depth = 0) {
|
|
198
|
+
if (node === null || node === void 0 || typeof node !== "object" || depth > 32) return;
|
|
199
|
+
if (Array.isArray(node)) {
|
|
200
|
+
for (const child of node) walkBashAst(child, found, opts, depth + 1);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const n = node;
|
|
204
|
+
if (n.type === "Command") scanCommandWords(n, found, opts);
|
|
205
|
+
for (const k of [
|
|
206
|
+
"command",
|
|
207
|
+
"body",
|
|
208
|
+
"clause",
|
|
209
|
+
"then",
|
|
210
|
+
"else",
|
|
211
|
+
"wordlist",
|
|
212
|
+
"commands"
|
|
213
|
+
]) {
|
|
214
|
+
const v = n[k];
|
|
215
|
+
if (v !== null && v !== void 0) walkBashAst(v, found, opts, depth + 1);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
function scanCommandWords(node, found, opts) {
|
|
219
|
+
const words = Array.isArray(node.suffix) ? node.suffix.filter((w) => isWord(w)) : [];
|
|
220
|
+
const base = (node.name?.text ?? "").split("/").pop() ?? "";
|
|
221
|
+
const exprFlags = EXPR_VALUE_FLAGS[base];
|
|
222
|
+
const valueFlags = BASH_VALUE_FLAGS[base];
|
|
223
|
+
const filterCmd = base === "jq" || base === "yq";
|
|
224
|
+
let skipExpr = false;
|
|
225
|
+
let consumeValues = 0;
|
|
226
|
+
let sawFilter = false;
|
|
227
|
+
let skipTgrepPattern = false;
|
|
228
|
+
for (const w of words) {
|
|
229
|
+
if (base === "tgrep" && skipTgrepPattern) {
|
|
230
|
+
skipTgrepPattern = false;
|
|
231
|
+
continue;
|
|
176
232
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
continue;
|
|
196
|
-
}
|
|
197
|
-
if (w.text.startsWith("-") && w.text !== "-") {
|
|
198
|
-
if (exprFlags?.has(w.text)) skipExpr = true;
|
|
199
|
-
else if (valueFlags?.[w.text]) consumeValues = valueFlags[w.text];
|
|
200
|
-
continue;
|
|
201
|
-
}
|
|
202
|
-
if (/:\/\//.test(w.text)) continue;
|
|
203
|
-
if (filterCmd) {
|
|
204
|
-
if (!sawFilter && FILTER_EVAL_WORDS.has(w.text)) continue;
|
|
205
|
-
if (!sawFilter) {
|
|
206
|
-
sawFilter = true;
|
|
207
|
-
continue;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
if (pathHasHidden(w.text, opts) && !found.includes(w.text)) found.push(w.text);
|
|
233
|
+
if (base === "tgrep" && w.text === "--") {
|
|
234
|
+
skipTgrepPattern = true;
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
if (skipExpr) {
|
|
238
|
+
skipExpr = false;
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (consumeValues > 0) {
|
|
242
|
+
consumeValues--;
|
|
243
|
+
if (pathHasHidden(w.text, opts) !== void 0 && !found.includes(w.text)) found.push(w.text);
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (w.text.startsWith("-") && w.text !== "-") {
|
|
247
|
+
if (exprFlags?.has(w.text) === true) skipExpr = true;
|
|
248
|
+
else {
|
|
249
|
+
const count = valueFlags?.[w.text];
|
|
250
|
+
if (count !== void 0) consumeValues = count;
|
|
211
251
|
}
|
|
252
|
+
continue;
|
|
212
253
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
"commands"
|
|
221
|
-
]) {
|
|
222
|
-
const v = node[k];
|
|
223
|
-
if (v) walk(v, depth + 1);
|
|
254
|
+
if (URL_REGEX.test(w.text)) continue;
|
|
255
|
+
if (filterCmd) {
|
|
256
|
+
if (!sawFilter && FILTER_EVAL_WORDS.has(w.text)) continue;
|
|
257
|
+
if (!sawFilter) {
|
|
258
|
+
sawFilter = true;
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
224
261
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
if (found.length === 0) return void 0;
|
|
228
|
-
return MSG(found.join(", "), "Found it in a bash command.");
|
|
229
|
-
}
|
|
230
|
-
function blockMessage(name) {
|
|
231
|
-
return MSG(name, "Requested via a tool.");
|
|
262
|
+
if (pathHasHidden(w.text, opts) !== void 0 && !found.includes(w.text)) found.push(w.text);
|
|
263
|
+
}
|
|
232
264
|
}
|
|
233
|
-
|
|
234
|
-
//#region src/v2.ts
|
|
265
|
+
// src/v2.ts
|
|
235
266
|
/**
|
|
236
267
|
* V2 adapter (OpenCode 2) for hidden-guard.
|
|
237
268
|
*/
|
|
269
|
+
function readGuardOptions(value) {
|
|
270
|
+
if (typeof value !== "object" || value === null) return {};
|
|
271
|
+
const options = {};
|
|
272
|
+
if ("disabled" in value && typeof value.disabled === "boolean") options.disabled = value.disabled;
|
|
273
|
+
if ("allow" in value && Array.isArray(value.allow)) options.allow = value.allow.filter((item) => typeof item === "string");
|
|
274
|
+
return options;
|
|
275
|
+
}
|
|
276
|
+
function hasCommand(input) {
|
|
277
|
+
return typeof input === "object" && input !== null && "command" in input;
|
|
278
|
+
}
|
|
238
279
|
const v2def = () => Plugin.define({
|
|
239
280
|
id: "hidden-guard",
|
|
240
281
|
async setup(ctx) {
|
|
241
|
-
const options = ctx.options
|
|
242
|
-
if (options.disabled) return;
|
|
282
|
+
const options = readGuardOptions(ctx.options);
|
|
283
|
+
if (options.disabled === true) return;
|
|
243
284
|
await ctx.tool.hook("execute.before", (event) => {
|
|
244
285
|
if (event.tool === "bash" || event.tool === "shell") {
|
|
245
|
-
const cmd = event.input
|
|
246
|
-
if (typeof cmd
|
|
247
|
-
|
|
248
|
-
|
|
286
|
+
const cmd = hasCommand(event.input) ? event.input.command : void 0;
|
|
287
|
+
if (typeof cmd === "string") {
|
|
288
|
+
const hit = bashTouchesHidden(cmd, options);
|
|
289
|
+
if (hit !== void 0) throw new Error(hit);
|
|
290
|
+
}
|
|
249
291
|
return;
|
|
250
292
|
}
|
|
251
293
|
const hit = argsTouchHidden(event.input, options);
|
|
252
|
-
if (hit) throw new Error(blockMessage(hit));
|
|
294
|
+
if (hit !== void 0) throw new Error(blockMessage(hit));
|
|
253
295
|
});
|
|
254
296
|
}
|
|
255
297
|
});
|
|
256
|
-
|
|
257
|
-
//#region src/v1.ts
|
|
258
|
-
/**
|
|
259
|
-
* V1 adapter (OpenCode 1.x) for hidden-guard.
|
|
260
|
-
*/
|
|
261
|
-
const v1 = async (_input, options = {}) => {
|
|
262
|
-
if (options.disabled) return {};
|
|
263
|
-
return { "tool.execute.before": async (input, output) => {
|
|
264
|
-
if (input.tool === "bash" || input.tool === "shell") {
|
|
265
|
-
const cmd = output.args?.command;
|
|
266
|
-
if (typeof cmd !== "string") return;
|
|
267
|
-
const hit = bashTouchesHidden(cmd, options);
|
|
268
|
-
if (hit) throw new Error(hit);
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
const hit = argsTouchHidden(output.args, options);
|
|
272
|
-
if (hit) throw new Error(blockMessage(hit));
|
|
273
|
-
} };
|
|
274
|
-
};
|
|
275
|
-
//#endregion
|
|
276
|
-
//#region src/index.ts
|
|
298
|
+
// src/index.ts
|
|
277
299
|
/**
|
|
278
|
-
*
|
|
300
|
+
* opencode-hidden-guard — OpenCode 2 plugin.
|
|
279
301
|
*
|
|
280
|
-
* Default export: pure Plugin.define (v2 calls setup()).
|
|
281
|
-
* `server()` key — on v2.0.18 a dual-shape default export loads the V1
|
|
282
|
-
* entry and the hook silently no-ops. The V1 adapter stays importable as a
|
|
283
|
-
* named export for v1 shims (./v1).
|
|
302
|
+
* Default export: pure Plugin.define (v2 calls setup()).
|
|
284
303
|
*/
|
|
285
304
|
var src_default = v2def();
|
|
286
|
-
|
|
287
|
-
export { src_default as default, v1 };
|
|
305
|
+
export { src_default as default };
|
|
288
306
|
|
|
289
307
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/core.ts","../src/v2.ts","../src/v1.ts","../src/index.ts"],"sourcesContent":["/**\n * hidden-guard core: dotfile-read detection for tool args and bash\n * commands. Framework-agnostic — V1 and V2 adapters share this.\n */\nimport { parse } from \"unbash\"\n\nexport type HiddenGuardOptions = {\n\t/** dotfile basenames that are allowed to be read (eg. [\".bashrc\"]) */\n\tallow?: string[]\n\tdisabled?: boolean\n}\n\ntype Node = {\n\ttype?: string\n\ttext?: string\n\tname?: { text?: string }\n\tsuffix?: unknown[]\n\tcommands?: Node[]\n\tcommand?: Node\n\tbody?: Node\n\tclause?: Node\n\tthen?: Node\n\telse?: Node\n\twordlist?: Node[]\n\t[k: string]: unknown\n}\n\ntype Word = { text: string; pos?: number; end?: number; value?: string }\n\nfunction isWord(n: unknown): n is Word {\n\treturn typeof n === \"object\" && n !== null && typeof (n as Word).text === \"string\" && !(n as Node).type\n}\n\n/** The last path segment of a string that looks like a path (or the whole\n * string when it has no separator). Returns undefined for non-paths. */\n/** Dotfile DIRECTORIES that are infrastructure, not secrets — allowed as\n * any path segment (.config, .git, build caches …). */\nconst INFRA_SEGMENTS = new Set([\n\t\".config\", \".local\", \".cache\", \".git\", \".github\", \".gitlab\", \".husky\",\n\t\".opencode\", \".codemode\", \".tgrep\", \".turbo\", \".next\", \".nuxt\",\n\t\".svelte-kit\", \".astro\", \".output\", \".pnpm-store\", \".venv\", \".venvs\",\n\t\".tox\", \".mypy_cache\", \".pytest_cache\", \".ruff_cache\", \".idea\", \".vscode\",\n\t\".devcontainer\", \".storybook\", \".vercel\", \".wrangler\", \".claude\",\n\t\".codesandbox\", \".parcel-cache\", \".yarn\", \".expo\", \".docusaurus\",\n])\n\n/** Dotfile NAMES that are code/config, not secrets — allowed as leaves. */\nconst INFRA_FILES = new Set([\n\t\".gitignore\", \".gitattributes\", \".gitmodules\", \".gitkeep\", \".editorconfig\",\n\t\".prettierrc\", \".eslintrc\", \".oxlintrc.json\", \".dockerignore\", \".npmignore\",\n\t\".nvmrc\", \".node-version\", \".tool-versions\", \".python-version\", \".DS_Store\",\n\t\".babelrc\", \".lintstagedrc\", \".czrc\", \".watchmanconfig\", \".hgignore\",\n\t\".flake8\", \".pylintrc\", \".prettierignore\", \".eslintignore\", \".stylelintrc\",\n\t\".commitlintrc\", \".releaserc\", \".versionrc\", \".taplo.toml\", \".latexmkrc\",\n])\n\n/** Command flags whose VALUE is an expression — a jq filter, Go template,\n * or JMESPath — never a file path. The word after one of these is not a\n * file argument (gh --jq .headSha, aws --query '.Tags[0].Value' …). */\nconst EXPR_VALUE_FLAGS: Record<string, Set<string>> = {\n\tgh: new Set([\"--jq\", \"--template\", \"-t\"]),\n\taws: new Set([\"--query\"]),\n\taz: new Set([\"--query\"]),\n}\n\n/** jq/yq subcommand aliases that are not the filter itself (yq eval …). */\nconst FILTER_EVAL_WORDS = new Set([\"e\", \"eval\", \"eval-all\", \"ea\"])\n\n/** jq flags that consume words from the stream; those words are file (or\n * variable) arguments — they are checked as files and never mistaken for\n * the filter expression. */\nconst BASH_VALUE_FLAGS: Record<string, Record<string, number>> = {\n\tjq: { \"-f\": 1, \"--from-file\": 1, \"--slurpfile\": 2, \"--rawfile\": 2 },\n\tyq: {},\n}\n\nexport function isHiddenBase(name: string, opts: HiddenGuardOptions = {}): boolean {\n\tif (!name.startsWith(\".\") || name === \".\" || name === \"..\") return false\n\tif (INFRA_SEGMENTS.has(name) || INFRA_FILES.has(name)) return false\n\treturn !(opts.allow ?? []).includes(name)\n}\n\n/** Any path segment that is a hidden file/dir (`.secrets/tokens.json` is\n * hidden because of `.secrets`, not its basename). Returns the first\n * hidden segment, or undefined. */\nexport function pathHasHidden(s: string, opts: HiddenGuardOptions = {}): string | undefined {\n\tif (!s || s.length > 4096) return undefined\n\tfor (const seg of s.split(/[\\\\/]/)) {\n\t\tif (!seg || seg === \".\" || seg === \"..\" || seg === \"~\") continue\n\t\tif (!seg.startsWith(\".\")) continue\n\t\t// infra dot-dirs (.config, .git, …) are walkable; sensitive hidden\n\t\t// dirs (.ssh, .aws) and hidden FILES anywhere in the path are not\n\t\tif (INFRA_SEGMENTS.has(seg)) continue\n\t\tif (INFRA_FILES.has(seg)) continue\n\t\tif (!(opts.allow ?? []).includes(seg)) return seg\n\t}\n\treturn undefined\n}\n\n/** True when a tool argument object references a hidden file in any\n * path-ish string value (filePath, path, file, or nested arrays of them). */\nexport function argsTouchHidden(args: unknown, opts: HiddenGuardOptions = {}): string | undefined {\n\tconst seen: unknown[] = []\n\tconst visit = (v: unknown, depth: number): string | undefined => {\n\t\tif (depth > 6 || v === null || v === undefined) return undefined\n\t\tconst t = typeof v\n\t\tif (t === \"string\") {\n\t\t\tconst s = v as string\n\t\t\t// path-ish only: starts with ., / or ~, or a plain dotfile name\n\t\t\tif (s.length > 0 && s.length < 4096 && /^[.~/]/.test(s)) {\n\t\t\t\tconst base = pathHasHidden(s, opts)\n\t\t\t\tif (base) return base\n\t\t\t}\n\t\t\treturn undefined\n\t\t}\n\t\tif (t !== \"object\") return undefined\n\t\tif (seen.includes(v)) return undefined\n\t\tseen.push(v)\n\t\tfor (const [k, val] of Object.entries(v as Record<string, unknown>)) {\n\t\t\tif (k === \"text\" || k === \"description\") continue // prose fields may legitimately mention dotfiles\n\t\t\tconst hit = visit(val, depth + 1)\n\t\t\tif (hit) return hit\n\t\t}\n\t\treturn undefined\n\t}\n\treturn visit(args, 0)\n}\n\nconst MSG = (name: string, what: string) =>\n\t`hidden-guard BLOCKED: \\`${name}\\` is a hidden (dot) file. ${what} Reading hidden files is not allowed (they hold credentials and local state). If you truly need it, ask the user to paste the relevant part.`\n\n/** Bash layer: any positional file argument whose basename is a hidden file\n * (any command, any reader — cat .env, wc -l .foo, strings .secret …).\n * NOT file args: flags; the value word of expression flags (--jq, --query,\n * --template — jq/JMESPath/Go-template expressions, never paths); value\n * words of known value-flags (checked as files); and the first positional\n * of jq/yq (the filter expression — files come after it). */\nexport function bashTouchesHidden(command: string, opts: HiddenGuardOptions = {}): string | undefined {\n\tif (opts.disabled) return undefined\n\tlet ast: Node\n\ttry {\n\t\tast = parse(command) as unknown as Node\n\t} catch {\n\t\treturn undefined // fail open\n\t}\n\tconst found: string[] = []\n\tconst walk = (n: unknown, depth: number): void => {\n\t\tif (!n || typeof n !== \"object\" || depth > 32) return\n\t\tif (Array.isArray(n)) {\n\t\t\tfor (const c of n) walk(c, depth + 1)\n\t\t\treturn\n\t\t}\n\t\tconst node = n as Node\n\t\tif (node.type === \"Command\") {\n\t\t\tconst words: Word[] = Array.isArray(node.suffix) ? node.suffix.filter(isWord) : []\n\t\t\tconst base = String(node.name?.text ?? \"\").split(\"/\").pop() ?? \"\"\n\t\t\tconst exprFlags = EXPR_VALUE_FLAGS[base]\n\t\t\tconst valueFlags = BASH_VALUE_FLAGS[base]\n\t\t\tconst filterCmd = base === \"jq\" || base === \"yq\"\n\t\t\tlet skipExpr = false\n\t\t\tlet consumeValues = 0\n\t\t\tlet sawFilter = false\n\t\t\tfor (const w of words) {\n\t\t\t\tif (skipExpr) {\n\t\t\t\t\t// expression value (--jq .headSha) — not a file argument\n\t\t\t\t\tskipExpr = false\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tif (consumeValues > 0) {\n\t\t\t\t\t// flag value word (-f FILE, --slurpfile VAR FILE) — checked as\n\t\t\t\t\t// a file, never treated as the filter\n\t\t\t\t\tconsumeValues--\n\t\t\t\t\tconst hidden = pathHasHidden(w.text, opts)\n\t\t\t\t\tif (hidden && !found.includes(w.text)) found.push(w.text)\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tif (w.text.startsWith(\"-\") && w.text !== \"-\") {\n\t\t\t\t\tif (exprFlags?.has(w.text)) skipExpr = true\n\t\t\t\t\telse if (valueFlags?.[w.text]) consumeValues = valueFlags[w.text]\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\t// URLs are not filesystem paths — dot-segments inside them\n\t\t\t\t// (https://host/.well-known/…) are never hidden-file reads\n\t\t\t\tif (/:\\/\\//.test(w.text)) continue\n\t\t\t\tif (filterCmd) {\n\t\t\t\t\tif (!sawFilter && FILTER_EVAL_WORDS.has(w.text)) continue\n\t\t\t\t\tif (!sawFilter) {\n\t\t\t\t\t\tsawFilter = true // first positional is the filter expression\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tconst hidden = pathHasHidden(w.text, opts)\n\t\t\t\tif (hidden && !found.includes(w.text)) found.push(w.text)\n\t\t\t}\n\t\t}\n\t\tfor (const k of [\"command\", \"body\", \"clause\", \"then\", \"else\", \"wordlist\", \"commands\"]) {\n\t\t\tconst v = (node as Record<string, unknown>)[k]\n\t\t\tif (v) walk(v, depth + 1)\n\t\t}\n\t}\n\twalk(ast, 0)\n\tif (found.length === 0) return undefined\n\treturn MSG(found.join(\", \"), \"Found it in a bash command.\")\n}\n\nexport function blockMessage(name: string): string {\n\treturn MSG(name, \"Requested via a tool.\")\n}\n","/**\n * V2 adapter (OpenCode 2) for hidden-guard.\n */\nimport { Plugin } from \"@opencode/plugin\"\nimport { argsTouchHidden, bashTouchesHidden, blockMessage, type HiddenGuardOptions } from \"./core\"\n\nexport const v2def = () =>\n\tPlugin.define({\n\t\tid: \"hidden-guard\",\n\t\tasync setup(ctx) {\n\t\t\tconst options = (ctx.options ?? {}) as HiddenGuardOptions\n\t\t\tif (options.disabled) return\n\t\t\tawait ctx.tool.hook(\"execute.before\", (event) => {\n\t\t\t\tif (event.tool === \"bash\" || event.tool === \"shell\") {\n\t\t\t\t\tconst cmd = (event.input as { command?: unknown } | undefined)?.command\n\t\t\t\t\tif (typeof cmd !== \"string\") return\n\t\t\t\t\tconst hit = bashTouchesHidden(cmd, options)\n\t\t\t\t\tif (hit) throw new Error(hit)\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tconst hit = argsTouchHidden(event.input, options)\n\t\t\t\tif (hit) throw new Error(blockMessage(hit))\n\t\t\t})\n\t\t},\n\t})\n","/**\n * V1 adapter (OpenCode 1.x) for hidden-guard.\n */\nimport { argsTouchHidden, bashTouchesHidden, blockMessage, type HiddenGuardOptions } from \"./core\"\n\ntype V1Hooks = Record<string, unknown>\n\nexport const v1 = async (_input: unknown, options: HiddenGuardOptions = {}): Promise<V1Hooks> => {\n\tif (options.disabled) return {}\n\treturn {\n\t\t\"tool.execute.before\": async (input: { tool: string }, output: { args: Record<string, unknown> }) => {\n\t\t\tif (input.tool === \"bash\" || input.tool === \"shell\") {\n\t\t\t\tconst cmd = output.args?.command\n\t\t\t\tif (typeof cmd !== \"string\") return\n\t\t\t\tconst hit = bashTouchesHidden(cmd, options)\n\t\t\t\tif (hit) throw new Error(hit)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconst hit = argsTouchHidden(output.args, options)\n\t\t\tif (hit) throw new Error(blockMessage(hit))\n\t\t},\n\t}\n}\n","/**\n * @comment-labs/opencode-hidden-guard — OpenCode 2 plugin.\n *\n * Default export: pure Plugin.define (v2 calls setup()). Do NOT add a\n * `server()` key — on v2.0.18 a dual-shape default export loads the V1\n * entry and the hook silently no-ops. The V1 adapter stays importable as a\n * named export for v1 shims (./v1).\n */\nimport { v2def } from \"./v2\"\n\nexport default v2def()\nexport { v1 } from \"./v1\"\n"],"mappings":";;;;;;;AA6BA,SAAS,OAAO,GAAuB;CACtC,OAAO,OAAO,MAAM,YAAY,MAAM,QAAQ,OAAQ,EAAW,SAAS,YAAY,CAAE,EAAW;AACpG;;;;;AAMA,MAAM,iCAAiB,IAAI,IAAI;CAC9B;CAAW;CAAU;CAAU;CAAQ;CAAW;CAAW;CAC7D;CAAa;CAAa;CAAU;CAAU;CAAS;CACvD;CAAe;CAAU;CAAW;CAAe;CAAS;CAC5D;CAAQ;CAAe;CAAiB;CAAe;CAAS;CAChE;CAAiB;CAAc;CAAW;CAAa;CACvD;CAAgB;CAAiB;CAAS;CAAS;AACpD,CAAC;;AAGD,MAAM,8BAAc,IAAI,IAAI;CAC3B;CAAc;CAAkB;CAAe;CAAY;CAC3D;CAAe;CAAa;CAAkB;CAAiB;CAC/D;CAAU;CAAiB;CAAkB;CAAmB;CAChE;CAAY;CAAiB;CAAS;CAAmB;CACzD;CAAW;CAAa;CAAmB;CAAiB;CAC5D;CAAiB;CAAc;CAAc;CAAe;AAC7D,CAAC;;;;AAKD,MAAM,mBAAgD;CACrD,oBAAI,IAAI,IAAI;EAAC;EAAQ;EAAc;CAAI,CAAC;CACxC,qBAAK,IAAI,IAAI,CAAC,SAAS,CAAC;CACxB,oBAAI,IAAI,IAAI,CAAC,SAAS,CAAC;AACxB;;AAGA,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UAA8B,CAAC,MAAwB;CAChG,IAAI,QAAQ,UAAU,OAAO,CAAC;CAC9B,OAAO,EACN,uBAAuB,OAAO,OAAyB,WAA8C;EACpG,IAAI,MAAM,SAAS,UAAU,MAAM,SAAS,SAAS;GACpD,MAAM,MAAM,OAAO,MAAM;GACzB,IAAI,OAAO,QAAQ,UAAU;GAC7B,MAAM,MAAM,kBAAkB,KAAK,OAAO;GAC1C,IAAI,KAAK,MAAM,IAAI,MAAM,GAAG;GAC5B;EACD;EACA,MAAM,MAAM,gBAAgB,OAAO,MAAM,OAAO;EAChD,IAAI,KAAK,MAAM,IAAI,MAAM,aAAa,GAAG,CAAC;CAC3C,EACD;AACD;;;;;;;;;;;ACZA,IAAA,cAAe,MAAM"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/core.ts","../src/v2.ts","../src/index.ts"],"sourcesContent":["/**\n * hidden-guard core: dotfile-read detection for tool args and bash\n * commands. Framework-agnostic — the V2 adapter runs this core.\n */\nimport { parse } from 'unbash'\n\nexport type HiddenGuardOptions = {\n /** dotfile basenames that are allowed to be read (eg. ['.bashrc']) */\n allow?: string[]\n disabled?: boolean\n}\n\ntype Node = {\n type?: string\n text?: string\n name?: { text?: string }\n suffix?: unknown[]\n commands?: Node[]\n command?: Node\n body?: Node\n clause?: Node\n then?: Node\n else?: Node\n wordlist?: Node[]\n [k: string]: unknown\n}\n\ntype Word = { text: string; pos?: number; end?: number; value?: string }\n\nconst PATH_SEGMENT_SPLIT_REGEX = /[\\\\/]/u\nconst PATH_ISH_REGEX = /^[.~/]/u\nconst URL_REGEX = /:\\/\\//u\n\nfunction isWord(n: unknown): n is Word {\n return typeof n === 'object'\n && n !== null\n && 'text' in n\n && typeof n.text === 'string'\n && !('type' in n && n.type !== undefined && n.type !== '')\n}\n\n/**\n * Dotfile DIRECTORIES that are infrastructure, not secrets — allowed as\n * any path segment (.config, .git, build caches …).\n */\nconst INFRA_SEGMENTS = new Set([\n '.config', '.local', '.cache', '.git', '.github', '.gitlab', '.husky',\n '.opencode', '.codemode', '.tgrep', '.turbo', '.next', '.nuxt',\n '.svelte-kit', '.astro', '.output', '.pnpm-store', '.venv', '.venvs',\n '.tox', '.mypy_cache', '.pytest_cache', '.ruff_cache', '.idea', '.vscode',\n '.devcontainer', '.storybook', '.vercel', '.wrangler', '.claude',\n '.codesandbox', '.parcel-cache', '.yarn', '.expo', '.docusaurus'\n])\n\n/**\n * Dotfile NAMES that are code/config, not secrets — allowed as leaves.\n */\nconst INFRA_FILES = new Set([\n '.gitignore', '.gitattributes', '.gitmodules', '.gitkeep', '.editorconfig',\n '.prettierrc', '.eslintrc', '.oxlintrc.json', '.dockerignore', '.npmignore',\n '.nvmrc', '.node-version', '.tool-versions', '.python-version', '.DS_Store',\n '.babelrc', '.lintstagedrc', '.czrc', '.watchmanconfig', '.hgignore',\n '.flake8', '.pylintrc', '.prettierignore', '.eslintignore', '.stylelintrc',\n '.commitlintrc', '.releaserc', '.versionrc', '.taplo.toml', '.latexmkrc'\n])\n\n/**\n * Command flags whose VALUE is an expression — a jq filter, Go template,\n * or JMESPath — never a file path. The word after one of these is not a\n * file argument (gh --jq .headSha, aws --query '.Tags[0].Value' …).\n */\nconst EXPR_VALUE_FLAGS: Record<string, Set<string>> = {\n gh: new Set([ '--jq', '--template', '-t' ]),\n aws: new Set([ '--query' ]),\n az: new Set([ '--query' ])\n}\n\n/**\n * jq/yq subcommand aliases that are not the filter itself (yq eval …).\n */\nconst FILTER_EVAL_WORDS = new Set([ 'e', 'eval', 'eval-all', 'ea' ])\n\n/**\n * jq flags that consume words from the stream; those words are file (or\n * variable) arguments — they are checked as files and never mistaken for\n * the filter expression.\n */\nconst BASH_VALUE_FLAGS: Record<string, Record<string, number>> = {\n jq: { '-f': 1, '--from-file': 1, '--slurpfile': 2, '--rawfile': 2 },\n yq: {}\n}\n\nexport function isHiddenBase(name: string, opts: HiddenGuardOptions = {}): boolean {\n if (name === '.' || name === '..' || !name.startsWith('.')) {\n return false\n }\n\n if (INFRA_SEGMENTS.has(name) || INFRA_FILES.has(name)) {\n return false\n }\n\n return !(opts.allow ?? []).includes(name)\n}\n\n/**\n * Any path segment that is a hidden file/dir (`.secrets/tokens.json` is\n * hidden because of `.secrets`, not its basename). Returns the first\n * hidden segment, or undefined.\n */\nexport function pathHasHidden(s: string, opts: HiddenGuardOptions = {}): string | undefined {\n if (s === '' || s.length > 4096) {\n return undefined\n }\n\n for (const seg of s.split(PATH_SEGMENT_SPLIT_REGEX)) {\n if (seg === '' || [ '.', '..', '~' ].includes(seg)) {\n continue\n }\n\n if (!seg.startsWith('.')) {\n continue\n }\n\n // infra dot-dirs (.config, .git, …) are walkable; sensitive hidden\n // dirs (.ssh, .aws) and hidden FILES anywhere in the path are not\n if (INFRA_SEGMENTS.has(seg)) {\n continue\n }\n\n if (INFRA_FILES.has(seg)) {\n continue\n }\n\n if (!(opts.allow ?? []).includes(seg)) {\n return seg\n }\n }\n\n return undefined\n}\n\nfunction isPlainObject(v: unknown): v is Record<string, unknown> {\n return typeof v === 'object' && v !== null\n}\n\n/**\n * True when a tool argument object references a hidden file in any\n * path-ish string value (filePath, path, file, or nested arrays of them).\n */\nexport function argsTouchHidden(args: unknown, opts: HiddenGuardOptions = {}): string | undefined {\n const seen: unknown[] = []\n\n const visit = (v: unknown, depth: number): string | undefined => {\n if (v === null || v === undefined || depth > 6) {\n return undefined\n }\n\n if (typeof v === 'string') {\n // path-ish only: starts with ., / or ~, or a plain dotfile name\n if (v.length > 0 && v.length < 4096 && PATH_ISH_REGEX.test(v)) {\n return pathHasHidden(v, opts)\n }\n\n return undefined\n }\n\n if (!isPlainObject(v)) {\n return undefined\n }\n\n if (seen.includes(v)) {\n return undefined\n }\n\n seen.push(v)\n\n for (const [ k, val ] of Object.entries(v)) {\n // prose fields may legitimately mention dotfiles\n if (k === 'text' || k === 'description') {\n continue\n }\n\n const hit = visit(val, depth + 1)\n if (hit !== undefined) {\n return hit\n }\n }\n\n return undefined\n }\n\n return visit(args, 0)\n}\n\nconst MSG = (name: string, what: string) =>\n `hidden-guard BLOCKED: \\`${name}\\` is a hidden (dot) file. ${what} Reading hidden files is not allowed (they hold credentials and local state). If you truly need it, ask the user to paste the relevant part.`\n\nexport function blockMessage(name: string): string {\n return MSG(name, 'Requested via a tool.')\n}\n\n/**\n * Bash layer: any positional file argument whose basename is a hidden file\n * (any command, any reader — cat .env, wc -l .foo, strings .secret …).\n * NOT file args: flags; the value word of expression flags (--jq, --query,\n * --template — jq/JMESPath/Go-template expressions, never paths); value\n * words of known value-flags (checked as files); and the first positional\n * of jq/yq (the filter expression — files come after it).\n */\nexport function bashTouchesHidden(command: string, opts: HiddenGuardOptions = {}): string | undefined {\n if (opts.disabled === true) {\n return undefined\n }\n\n let ast: Node\n\n try {\n // unbash's own AST types are stricter unions; this loose Node is the\n // analyzer's working shape\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion\n ast = parse(command) as unknown as Node\n } catch {\n // fail open\n return undefined\n }\n\n const found: string[] = []\n walkBashAst(ast, found, opts)\n\n if (found.length === 0) {\n return undefined\n }\n\n return MSG(found.join(', '), 'Found it in a bash command.')\n}\n\nfunction walkBashAst(node: unknown, found: string[], opts: HiddenGuardOptions, depth = 0): void {\n if (node === null || node === undefined || typeof node !== 'object' || depth > 32) {\n return\n }\n\n if (Array.isArray(node)) {\n for (const child of node) {\n walkBashAst(child, found, opts, depth + 1)\n }\n\n return\n }\n\n // parser AST boundary: every value reaching here comes from unbash's parse()\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion\n const n = node as Node\n if (n.type === 'Command') {\n scanCommandWords(n, found, opts)\n }\n\n for (const k of [ 'command', 'body', 'clause', 'then', 'else', 'wordlist', 'commands' ]) {\n const v = (n as Record<string, unknown>)[k]\n if (v !== null && v !== undefined) {\n walkBashAst(v, found, opts, depth + 1)\n }\n }\n}\n\nfunction scanCommandWords(node: Node, found: string[], opts: HiddenGuardOptions): void {\n const words: Word[] = Array.isArray(node.suffix) ? node.suffix.filter(w => isWord(w)) : []\n const base = (node.name?.text ?? '').split('/').pop() ?? ''\n const exprFlags = EXPR_VALUE_FLAGS[base]\n const valueFlags = BASH_VALUE_FLAGS[base]\n const filterCmd = base === 'jq' || base === 'yq'\n let skipExpr = false\n let consumeValues = 0\n let sawFilter = false\n let skipTgrepPattern = false\n\n for (const w of words) {\n if (base === 'tgrep' && skipTgrepPattern) {\n // tgrep's search expression after `--` is not a file argument\n skipTgrepPattern = false\n continue\n }\n\n if (base === 'tgrep' && w.text === '--') {\n skipTgrepPattern = true\n continue\n }\n\n if (skipExpr) {\n // expression value (--jq .headSha) — not a file argument\n skipExpr = false\n continue\n }\n\n if (consumeValues > 0) {\n // flag value word (-f FILE, --slurpfile VAR FILE) — checked as a\n // file, never treated as the filter\n consumeValues--\n\n const hidden = pathHasHidden(w.text, opts)\n if (hidden !== undefined && !found.includes(w.text)) {\n found.push(w.text)\n }\n\n continue\n }\n\n if (w.text.startsWith('-') && w.text !== '-') {\n if (exprFlags?.has(w.text) === true) {\n skipExpr = true\n } else {\n const count = valueFlags?.[w.text]\n if (count !== undefined) {\n consumeValues = count\n }\n }\n\n continue\n }\n\n // URLs are not filesystem paths — dot-segments inside them\n // (https://host/.well-known/…) are never hidden-file reads\n if (URL_REGEX.test(w.text)) {\n continue\n }\n\n if (filterCmd) {\n if (!sawFilter && FILTER_EVAL_WORDS.has(w.text)) {\n continue\n }\n\n if (!sawFilter) {\n // first positional is the filter expression\n sawFilter = true\n continue\n }\n }\n\n const hidden = pathHasHidden(w.text, opts)\n if (hidden !== undefined && !found.includes(w.text)) {\n found.push(w.text)\n }\n }\n}\n","/**\n * V2 adapter (OpenCode 2) for hidden-guard.\n */\nimport { Plugin } from '@opencode/plugin'\nimport { argsTouchHidden, bashTouchesHidden, blockMessage, type HiddenGuardOptions } from './core'\n\n\nfunction readGuardOptions(value: unknown): HiddenGuardOptions {\n if (typeof value !== 'object' || value === null) {\n return {}\n }\n\n const options: HiddenGuardOptions = {}\n if ('disabled' in value && typeof value.disabled === 'boolean') {\n options.disabled = value.disabled\n }\n\n if ('allow' in value && Array.isArray(value.allow)) {\n options.allow = value.allow.filter((item): item is string => typeof item === 'string')\n }\n\n return options\n}\n\nfunction hasCommand(input: unknown): input is { command: unknown } {\n return typeof input === 'object' && input !== null && 'command' in input\n}\n\nexport const v2def = () =>\n Plugin.define({\n id: 'hidden-guard',\n async setup(ctx) {\n const options = readGuardOptions(ctx.options)\n if (options.disabled === true) {\n return\n }\n\n await ctx.tool.hook('execute.before', event => {\n if (event.tool === 'bash' || event.tool === 'shell') {\n const cmd = hasCommand(event.input) ? event.input.command : undefined\n if (typeof cmd === 'string') {\n const hit = bashTouchesHidden(cmd, options)\n if (hit !== undefined) {\n throw new Error(hit)\n }\n }\n\n return\n }\n\n const hit = argsTouchHidden(event.input, options)\n if (hit !== undefined) {\n throw new Error(blockMessage(hit))\n }\n })\n }\n })\n","/**\n * opencode-hidden-guard — OpenCode 2 plugin.\n *\n * Default export: pure Plugin.define (v2 calls setup()).\n */\nimport { v2def } from './v2'\n\nexport default v2def()\n"],"mappings":";;;;;;;AA6BA,MAAM,2BAA2B;AACjC,MAAM,iBAAiB;AACvB,MAAM,YAAY;AAElB,SAAS,OAAO,GAAuB;CACrC,OAAO,OAAO,MAAM,YACf,MAAM,QACN,UAAU,KACV,OAAO,EAAE,SAAS,YAClB,EAAE,UAAU,KAAK,EAAE,SAAS,KAAA,KAAa,EAAE,SAAS;AAC3D;;;;;AAMA,MAAM,iCAAiB,IAAI,IAAI;CAC7B;CAAW;CAAU;CAAU;CAAQ;CAAW;CAAW;CAC7D;CAAa;CAAa;CAAU;CAAU;CAAS;CACvD;CAAe;CAAU;CAAW;CAAe;CAAS;CAC5D;CAAQ;CAAe;CAAiB;CAAe;CAAS;CAChE;CAAiB;CAAc;CAAW;CAAa;CACvD;CAAgB;CAAiB;CAAS;CAAS;AACrD,CAAC;;;;AAKD,MAAM,8BAAc,IAAI,IAAI;CAC1B;CAAc;CAAkB;CA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|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@comment-labs/opencode-hidden-guard",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.2",
|
|
5
5
|
"description": "opencode plugin: blocks agent reads of hidden (dot) files (.env, .ssh, tokens) at the tool and bash layers.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"author": "CommEnt Team",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"unbash": "4.0.11"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@comment-labs/oxlint-config": "1.0.
|
|
24
|
+
"@comment-labs/oxlint-config": "1.0.10",
|
|
25
25
|
"@comment-labs/tsconfig": "1.0.0-beta.8",
|
|
26
26
|
"@types/node": "25.9.5",
|
|
27
|
-
"oxlint": "1.
|
|
28
|
-
"oxlint-tsgolint": "7.0.
|
|
27
|
+
"oxlint": "1.95.0",
|
|
28
|
+
"oxlint-tsgolint": "7.0.2008",
|
|
29
29
|
"tsdown": "0.23.1",
|
|
30
30
|
"typescript": "npm:typescript-native-bridge@6.0.3-bridge.12.tsgo.7.0.2",
|
|
31
31
|
"vitest": "4.1.11"
|