leakshield 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +546 -2
- package/README.zh-TW.md +565 -0
- package/dist/cli.js +61 -0
- package/dist/config/loader.js +94 -0
- package/dist/decision/deidentify.js +195 -0
- package/dist/decision/ingress.js +136 -0
- package/dist/decision/mapping-key.js +318 -0
- package/dist/decision/mapping-store.js +555 -0
- package/dist/decision/model.js +76 -0
- package/dist/decision/pseudonymization.js +1230 -0
- package/dist/decision/redaction.js +185 -0
- package/dist/decision/schema-strings.js +42 -0
- package/dist/doctor.js +438 -0
- package/dist/env-queries.js +584 -0
- package/dist/forget.js +56 -0
- package/dist/help.js +37 -0
- package/dist/i18n/en.js +71 -0
- package/dist/i18n/index.js +28 -0
- package/dist/i18n/zh-TW.js +71 -0
- package/dist/index.js +12 -0
- package/dist/opencode-env-tools.js +243 -0
- package/dist/opencode-hooks.js +84 -0
- package/dist/opencode-transparent-aisdk.js +100 -0
- package/dist/opencode-transparent-http.js +2315 -0
- package/dist/opencode-transparent-stream.js +192 -0
- package/dist/opencode-transparent-ws.js +298 -0
- package/dist/opencode-transparent.js +1492 -0
- package/dist/opencode-v2.js +216 -0
- package/dist/opencode.js +175 -0
- package/dist/plugin-options.js +100 -0
- package/dist/rules/builtin.js +283 -0
- package/dist/rules/dynamic.js +68 -0
- package/dist/scanner/encoding.js +274 -0
- package/dist/scanner/engine.js +321 -0
- package/dist/scanner/exact-values.js +99 -0
- package/dist/scanner/redaction-verifier.js +82 -0
- package/dist/scanner/unicode.js +157 -0
- package/dist/scanner/utils.js +60 -0
- package/dist/transports/cli.js +447 -0
- package/dist/transports/web-ui.js +540 -0
- package/dist/transports/web.js +200 -0
- package/dist/types.js +23 -0
- package/dist/utils/files.js +36 -0
- package/dist/utils/path-security.js +30 -0
- package/dist/utils/win-encoding.js +45 -0
- package/package.json +71 -4
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { createHmac } from "node:crypto";
|
|
2
|
+
import { ContentHashCache, protectUnknownStrings, } from "./decision/ingress.js";
|
|
3
|
+
import { deidentifyText } from "./decision/deidentify.js";
|
|
4
|
+
import { registerV2EnvTools } from "./opencode-env-tools.js";
|
|
5
|
+
const INITIALIZATION_ERROR = "LeakShield initialization failed, so the request was blocked before anything was sent.";
|
|
6
|
+
const BLOCKED_ATTACHMENT_NOTICE = "[LeakShield blocked an attachment: its content cannot be scanned, so it was not sent.]";
|
|
7
|
+
function isRecordLike(value) {
|
|
8
|
+
return typeof value === "object" && value !== null;
|
|
9
|
+
}
|
|
10
|
+
function protectString(text, config, mode, context, protector) {
|
|
11
|
+
if (typeof text !== "string")
|
|
12
|
+
return text;
|
|
13
|
+
return protector(text, config, { mode, context }).text;
|
|
14
|
+
}
|
|
15
|
+
function protectToolResultValue(value, config, mode, protector) {
|
|
16
|
+
if (!isRecordLike(value))
|
|
17
|
+
return value;
|
|
18
|
+
if (value.type === "content" && Array.isArray(value.value)) {
|
|
19
|
+
return {
|
|
20
|
+
...value,
|
|
21
|
+
value: value.value.map((item) => isRecordLike(item) && item.type === "text"
|
|
22
|
+
? { ...item, text: protectString(item.text, config, mode, "tool output", protector) }
|
|
23
|
+
: item),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
...value,
|
|
28
|
+
value: protectUnknownStrings(value.value, config, { mode, context: "tool result" }, protector),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function protectContentPart(part, config, mode, protector) {
|
|
32
|
+
if (!isRecordLike(part))
|
|
33
|
+
return part;
|
|
34
|
+
switch (part.type) {
|
|
35
|
+
case "text":
|
|
36
|
+
return { ...part, text: protectString(part.text, config, mode, "message text", protector) };
|
|
37
|
+
case "reasoning":
|
|
38
|
+
// Encrypted-only reasoning is opaque provider state and is left intact.
|
|
39
|
+
return { ...part, text: protectString(part.text, config, mode, "reasoning text", protector) };
|
|
40
|
+
case "media":
|
|
41
|
+
return { type: "text", text: BLOCKED_ATTACHMENT_NOTICE };
|
|
42
|
+
case "tool-call":
|
|
43
|
+
return {
|
|
44
|
+
...part,
|
|
45
|
+
input: protectUnknownStrings(part.input, config, { mode, context: "tool call input" }, protector),
|
|
46
|
+
};
|
|
47
|
+
case "tool-result":
|
|
48
|
+
return {
|
|
49
|
+
...part,
|
|
50
|
+
result: protectToolResultValue(part.result, config, mode, protector),
|
|
51
|
+
};
|
|
52
|
+
default:
|
|
53
|
+
return part;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function protectContentParts(parts, config, mode, protector) {
|
|
57
|
+
return parts.map((part) => protectContentPart(part, config, mode, protector));
|
|
58
|
+
}
|
|
59
|
+
function protectRequestEvent(event, config, mode, protector) {
|
|
60
|
+
if (Array.isArray(event.system)) {
|
|
61
|
+
for (const part of event.system) {
|
|
62
|
+
if (isRecordLike(part) && part.type === "text") {
|
|
63
|
+
part.text = protectString(part.text, config, mode, "system prompt", protector);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (Array.isArray(event.messages)) {
|
|
68
|
+
for (const message of event.messages) {
|
|
69
|
+
if (!isRecordLike(message))
|
|
70
|
+
continue;
|
|
71
|
+
const parts = message.content;
|
|
72
|
+
if (Array.isArray(parts)) {
|
|
73
|
+
message.content = protectContentParts(parts, config, mode, protector);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (isRecordLike(event.tools)) {
|
|
78
|
+
for (const tool of Object.values(event.tools)) {
|
|
79
|
+
if (!isRecordLike(tool))
|
|
80
|
+
continue;
|
|
81
|
+
tool.description = protectString(tool.description, config, mode, "tool description", protector);
|
|
82
|
+
tool.input = protectUnknownStrings(tool.input, config, { mode, context: "tool parameters" }, protector);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function protectPromptEvent(event, config, mode, protector) {
|
|
87
|
+
const prompt = event.prompt;
|
|
88
|
+
if (!isRecordLike(prompt))
|
|
89
|
+
return;
|
|
90
|
+
prompt.text = protectString(prompt.text, config, mode, "message text", protector);
|
|
91
|
+
if (Array.isArray(prompt.files) && prompt.files.length > 0) {
|
|
92
|
+
prompt.files = [];
|
|
93
|
+
const text = typeof prompt.text === "string" ? prompt.text : "";
|
|
94
|
+
prompt.text = text ? `${text}\n\n${BLOCKED_ATTACHMENT_NOTICE}` : BLOCKED_ATTACHMENT_NOTICE;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function protectToolAfterEvent(event, config, mode, protector) {
|
|
98
|
+
if (event.status !== "completed")
|
|
99
|
+
return;
|
|
100
|
+
const result = event.result;
|
|
101
|
+
if (!isRecordLike(result))
|
|
102
|
+
return;
|
|
103
|
+
const next = { ...result };
|
|
104
|
+
if (typeof result.content === "string") {
|
|
105
|
+
next.content = protector(result.content, config, { mode, context: "tool output" }).text;
|
|
106
|
+
}
|
|
107
|
+
else if (Array.isArray(result.content)) {
|
|
108
|
+
next.content = result.content.map((item) => isRecordLike(item) && item.type === "text"
|
|
109
|
+
? { ...item, text: protectString(item.text, config, mode, "tool output", protector) }
|
|
110
|
+
: item);
|
|
111
|
+
}
|
|
112
|
+
next.output = protectUnknownStrings(result.output, config, { mode, context: "tool output" }, protector);
|
|
113
|
+
next.metadata = protectUnknownStrings(result.metadata, config, { mode, context: "tool metadata" }, protector);
|
|
114
|
+
event.result = next;
|
|
115
|
+
}
|
|
116
|
+
async function registerProtectionHooks(ctx, config, mode, protectorForEvent) {
|
|
117
|
+
await ctx.session.hook("prompt", (event) => {
|
|
118
|
+
protectPromptEvent(event, config, mode, protectorForEvent(event));
|
|
119
|
+
});
|
|
120
|
+
const onRequest = (event) => {
|
|
121
|
+
protectRequestEvent(event, config, mode, protectorForEvent(event));
|
|
122
|
+
};
|
|
123
|
+
await ctx.session.hook("context", onRequest);
|
|
124
|
+
await ctx.session.hook("compaction", onRequest);
|
|
125
|
+
await ctx.session.hook("generate", onRequest);
|
|
126
|
+
await ctx.session.hook("title", onRequest);
|
|
127
|
+
await ctx.tool.hook("execute.after", (event) => {
|
|
128
|
+
protectToolAfterEvent(event, config, mode, protectorForEvent(event));
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
async function registerFailClosedHooks(ctx) {
|
|
132
|
+
const blockRequest = async () => {
|
|
133
|
+
throw new Error(INITIALIZATION_ERROR);
|
|
134
|
+
};
|
|
135
|
+
await ctx.session.hook("prompt", blockRequest);
|
|
136
|
+
await ctx.session.hook("context", blockRequest);
|
|
137
|
+
await ctx.session.hook("compaction", blockRequest);
|
|
138
|
+
await ctx.session.hook("generate", blockRequest);
|
|
139
|
+
await ctx.session.hook("title", blockRequest);
|
|
140
|
+
await ctx.tool.hook("execute.after", blockRequest);
|
|
141
|
+
}
|
|
142
|
+
const PLACEHOLDER_SESSION_LIMIT = 256;
|
|
143
|
+
const PLACEHOLDER_CAPACITY_ERROR = "LeakShield de-identification session capacity reached; existing placeholders remain valid. Continue in an active session.";
|
|
144
|
+
/** Creates the OpenCode v2 `setup(ctx)` implementation for the given configuration. */
|
|
145
|
+
export function createV2Setup(config, mode, deidentify,
|
|
146
|
+
/** When set, the readonly env query tools are registered (opt-in `envQueries`). */
|
|
147
|
+
envQueries) {
|
|
148
|
+
const cache = new ContentHashCache();
|
|
149
|
+
const masterKey = deidentify?.sessionKey;
|
|
150
|
+
const placeholderSessions = new Map();
|
|
151
|
+
const placeholderStateFor = (event) => {
|
|
152
|
+
if (!masterKey)
|
|
153
|
+
return undefined;
|
|
154
|
+
const sessionID = isRecordLike(event) && typeof event.sessionID === "string" ? event.sessionID : "";
|
|
155
|
+
const retained = placeholderSessions.get(sessionID);
|
|
156
|
+
return {
|
|
157
|
+
sessionID,
|
|
158
|
+
state: retained ?? {
|
|
159
|
+
key: sessionID
|
|
160
|
+
? createHmac("sha256", masterKey).update(`session:${sessionID}`).digest()
|
|
161
|
+
: masterKey,
|
|
162
|
+
placeholders: new Set(),
|
|
163
|
+
},
|
|
164
|
+
retained: retained !== undefined,
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
const carriesOwnedPlaceholder = (text, placeholders) => {
|
|
168
|
+
for (const placeholder of placeholders) {
|
|
169
|
+
if (text.includes(placeholder))
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
return false;
|
|
173
|
+
};
|
|
174
|
+
// The plain protector: history cache composition — redact results stay
|
|
175
|
+
// uncached and block decisions are final (fail-closed); only allow decisions
|
|
176
|
+
// reuse the cache. The cache is shared across sessions, which is safe: it
|
|
177
|
+
// stores only allow/block decisions, never text or placeholders.
|
|
178
|
+
const baseProtector = (text, currentConfig, options) => cache.protect(text, currentConfig, options);
|
|
179
|
+
// Per-event protector: when de-identify is enabled, bind the session key
|
|
180
|
+
// carried by this event into the protector before walking it. deidentifyText
|
|
181
|
+
// keeps the same cache composition (allow → cache, block/redact → final and
|
|
182
|
+
// uncached), so the cache's decision always agrees with the direct
|
|
183
|
+
// computation.
|
|
184
|
+
const protectorForEvent = (event) => {
|
|
185
|
+
if (!deidentify)
|
|
186
|
+
return baseProtector;
|
|
187
|
+
const session = placeholderStateFor(event);
|
|
188
|
+
if (!session)
|
|
189
|
+
return baseProtector;
|
|
190
|
+
return (text, currentConfig, options) => {
|
|
191
|
+
const result = deidentifyText(text, currentConfig, session.state.key, options, session.state.placeholders);
|
|
192
|
+
if (!session.retained && session.state.placeholders.size > 0) {
|
|
193
|
+
if (placeholderSessions.size >= PLACEHOLDER_SESSION_LIMIT) {
|
|
194
|
+
throw new Error(PLACEHOLDER_CAPACITY_ERROR);
|
|
195
|
+
}
|
|
196
|
+
placeholderSessions.set(session.sessionID, session.state);
|
|
197
|
+
session.retained = true;
|
|
198
|
+
}
|
|
199
|
+
if (result.action !== "allow")
|
|
200
|
+
return result;
|
|
201
|
+
return carriesOwnedPlaceholder(text, session.state.placeholders)
|
|
202
|
+
? result
|
|
203
|
+
: cache.protect(text, currentConfig, options);
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
return async (ctx) => {
|
|
207
|
+
await registerProtectionHooks(ctx, config, mode, protectorForEvent);
|
|
208
|
+
if (envQueries) {
|
|
209
|
+
await registerV2EnvTools(ctx, envQueries);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/** Creates a fail-closed OpenCode v2 `setup(ctx)` that blocks every protected operation. */
|
|
214
|
+
export function createFailClosedV2Setup() {
|
|
215
|
+
return registerFailClosedHooks;
|
|
216
|
+
}
|
package/dist/opencode.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { findConfigPath, loadConfigWithFingerprint } from "./config/loader.js";
|
|
2
|
+
import { createSessionKey } from "./decision/deidentify.js";
|
|
3
|
+
import { defaultStorePath } from "./decision/mapping-key.js";
|
|
4
|
+
import { EnvQueryService } from "./env-queries.js";
|
|
5
|
+
import { createFailClosedHooks, createHooks } from "./opencode-hooks.js";
|
|
6
|
+
import { createV1EnvTools } from "./opencode-env-tools.js";
|
|
7
|
+
import { createFailClosedV2Setup, createV2Setup } from "./opencode-v2.js";
|
|
8
|
+
import { createFailClosedTransparentGuard, createTransparentSessionGuard } from "./opencode-transparent.js";
|
|
9
|
+
import { PluginOptionsSchema, withDefaultMode } from "./plugin-options.js";
|
|
10
|
+
function withEncodingDetection(config) {
|
|
11
|
+
return {
|
|
12
|
+
...config,
|
|
13
|
+
settings: {
|
|
14
|
+
...config.settings,
|
|
15
|
+
encoding_detection: true,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Builds the readonly env query service when the `envQueries` option is on.
|
|
21
|
+
* The env_files allowlist, workspace root, and guard configuration are frozen
|
|
22
|
+
* at setup from one configuration read (`loadConfigWithFingerprint`), so the
|
|
23
|
+
* authorization can never be a mix of two different config states. Per-call
|
|
24
|
+
* config drift disables queries until restart (handled inside the service),
|
|
25
|
+
* never a silent fallback.
|
|
26
|
+
*/
|
|
27
|
+
function createEnvQueryService(options, workspaceRoot, loaded) {
|
|
28
|
+
if (!options.envQueries)
|
|
29
|
+
return undefined;
|
|
30
|
+
return new EnvQueryService({
|
|
31
|
+
envFiles: loaded.config.settings.env_files ?? [],
|
|
32
|
+
workspaceRoot,
|
|
33
|
+
resolveConfigPath: () => findConfigPath(options.configPath),
|
|
34
|
+
configFingerprint: loaded.fingerprint,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function getWorkspaceRoot(input) {
|
|
38
|
+
if (typeof input !== "object" || input === null)
|
|
39
|
+
return process.cwd();
|
|
40
|
+
const context = input;
|
|
41
|
+
if (typeof context.worktree === "string")
|
|
42
|
+
return context.worktree;
|
|
43
|
+
if (typeof context.directory === "string")
|
|
44
|
+
return context.directory;
|
|
45
|
+
return process.cwd();
|
|
46
|
+
}
|
|
47
|
+
/** Whether the raw v2 plugin options select transparent mode, explicitly or
|
|
48
|
+
* by omitting `mode` (the v2 default), even if the options are invalid. */
|
|
49
|
+
function rawModeIsTransparent(options) {
|
|
50
|
+
const resolved = withDefaultMode(options, "v2");
|
|
51
|
+
return (typeof resolved === "object" &&
|
|
52
|
+
resolved !== null &&
|
|
53
|
+
resolved.mode === "transparent");
|
|
54
|
+
}
|
|
55
|
+
const v2Setup = async (ctx) => {
|
|
56
|
+
try {
|
|
57
|
+
const options = PluginOptionsSchema.parse(withDefaultMode(ctx.options, "v2"));
|
|
58
|
+
const loaded = loadConfigWithFingerprint(options.configPath, ctx.location.directory);
|
|
59
|
+
const config = withEncodingDetection(loaded.config);
|
|
60
|
+
if (options.mode === "transparent") {
|
|
61
|
+
// Transparent pseudonymization arms its own bounded session service,
|
|
62
|
+
// transport hooks, and disposal; deidentify/envQueries combos were
|
|
63
|
+
// already rejected at the option schema. W2: persistence options flow
|
|
64
|
+
// into the guard only when explicitly enabled (default memory-only,
|
|
65
|
+
// zero behavior change).
|
|
66
|
+
const persistence = options.persistence.enabled
|
|
67
|
+
? {
|
|
68
|
+
ttlDays: options.persistence.ttlDays,
|
|
69
|
+
path: options.persistence.path ?? defaultStorePath(),
|
|
70
|
+
keyFile: options.persistence.keyFile,
|
|
71
|
+
keyfilePath: options.persistence.keyfilePath,
|
|
72
|
+
}
|
|
73
|
+
: undefined;
|
|
74
|
+
const guard = createTransparentSessionGuard(config, options.attachments, options.wireProtection ?? true, persistence);
|
|
75
|
+
await guard.setup(ctx);
|
|
76
|
+
// Hook-only must leave provider routing/transport preferences untouched.
|
|
77
|
+
if (options.wireProtection === false)
|
|
78
|
+
return () => guard.dispose();
|
|
79
|
+
try {
|
|
80
|
+
// Native OpenAI defaults to WebSocket in OpenCode v2. HTTP uses the
|
|
81
|
+
// same Responses protocol, but passes through our final wire hooks.
|
|
82
|
+
// This registration is replayed on provider reload and removed on
|
|
83
|
+
// unload; no persisted config, endpoint or credential is changed.
|
|
84
|
+
// Explicit provider config is applied later by the host and can
|
|
85
|
+
// override this compatibility preference; supported Responses-family
|
|
86
|
+
// WS routes now have independent frame masking/restoration hooks.
|
|
87
|
+
const registration = await ctx.provider.transform((editor) => {
|
|
88
|
+
for (const { provider } of editor.list()) {
|
|
89
|
+
if (provider.package !== "@opencode/ai/providers/openai")
|
|
90
|
+
continue;
|
|
91
|
+
editor.update(provider.id, (draft) => {
|
|
92
|
+
draft.settings = { ...draft.settings, transport: "http" };
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
return async () => {
|
|
97
|
+
try {
|
|
98
|
+
await registration.dispose();
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
await guard.dispose();
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
await guard.dispose();
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const deidentify = options.deidentify ? { sessionKey: createSessionKey() } : undefined;
|
|
111
|
+
const envQueries = createEnvQueryService(options, ctx.location.directory, loaded);
|
|
112
|
+
await createV2Setup(config, options.mode, deidentify, envQueries)(ctx);
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
// Fail closed at the selected layer. Wire mode covers transport routes
|
|
116
|
+
// too; explicit hook-only refuses content/tool dispatch without adding
|
|
117
|
+
// transport gates or requiring the SDK API. The raw
|
|
118
|
+
// option is inspected because a schema/refine failure leaves `options`
|
|
119
|
+
// unset. The fallback's dispose is returned for host-triggered cleanup.
|
|
120
|
+
if (rawModeIsTransparent(ctx.options)) {
|
|
121
|
+
const guard = createFailClosedTransparentGuard(ctx.options?.wireProtection !== false);
|
|
122
|
+
await guard.setup(ctx);
|
|
123
|
+
return guard.dispose;
|
|
124
|
+
}
|
|
125
|
+
await createFailClosedV2Setup()(ctx);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
const v1Server = async (input, rawOptions) => {
|
|
129
|
+
try {
|
|
130
|
+
const options = PluginOptionsSchema.parse(withDefaultMode(rawOptions, "v1"));
|
|
131
|
+
if (options.deidentify) {
|
|
132
|
+
// v1 has no de-identification pipeline; the shared options schema now
|
|
133
|
+
// includes the flag, so v1 must explicitly reject it. The throw lands in
|
|
134
|
+
// the catch below → fail-closed hooks.
|
|
135
|
+
throw new Error("deidentify is only supported on OpenCode v2");
|
|
136
|
+
}
|
|
137
|
+
if (options.mode === "transparent") {
|
|
138
|
+
// v1 has no native transport hooks to remask the final wire or restore
|
|
139
|
+
// responses, so transparent mode cannot honor its contract there. The
|
|
140
|
+
// throw lands in the catch below → fail-closed hooks.
|
|
141
|
+
throw new Error("transparent mode is only supported on OpenCode v2");
|
|
142
|
+
}
|
|
143
|
+
const workspaceRoot = getWorkspaceRoot(input);
|
|
144
|
+
const loaded = loadConfigWithFingerprint(options.configPath, workspaceRoot);
|
|
145
|
+
const config = withEncodingDetection(loaded.config);
|
|
146
|
+
const hooks = createHooks(config, options.mode);
|
|
147
|
+
const envQueries = createEnvQueryService(options, workspaceRoot, loaded);
|
|
148
|
+
if (!envQueries)
|
|
149
|
+
return hooks;
|
|
150
|
+
try {
|
|
151
|
+
return { ...hooks, tool: await createV1EnvTools(envQueries) };
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
// The opt-in env query tools failed to load on their own (e.g. a
|
|
155
|
+
// missing v1 SDK module on an incomplete install). Only the feature
|
|
156
|
+
// fails: the full protection hooks above stay exactly as armed as
|
|
157
|
+
// they would be with envQueries off — no fail-closed downgrade, no
|
|
158
|
+
// protection gap, no env tools.
|
|
159
|
+
return hooks;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
return createFailClosedHooks();
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* Dual-version OpenCode plugin entry. OpenCode v2 reads `id` and `setup(ctx)`;
|
|
168
|
+
* OpenCode v1 (1.18.29+) calls `server(input, options)` and uses the returned hooks.
|
|
169
|
+
*/
|
|
170
|
+
const leakshieldPlugin = {
|
|
171
|
+
id: "leakshield",
|
|
172
|
+
setup: v2Setup,
|
|
173
|
+
server: v1Server,
|
|
174
|
+
};
|
|
175
|
+
export default leakshieldPlugin;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin options schema — the single source of truth shared by the plugin
|
|
3
|
+
* entry (src/opencode.ts, which keeps a default-only export surface for the
|
|
4
|
+
* host loaders) and the install doctor (src/doctor.ts).
|
|
5
|
+
*/
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
// deidentify is v2-only and valid only in redact mode; an invalid combination
|
|
8
|
+
// makes the parse throw, and every entry path already fails closed on that.
|
|
9
|
+
// transparent is the v2-only reversible pseudonymization mode (issue #22):
|
|
10
|
+
// it owns its own bounded session service and transport hooks, so combining
|
|
11
|
+
// it with the second (deidentify) protection pipeline or the v1/v2 env query
|
|
12
|
+
// tools is rejected here — every entry path fails closed on the parse error.
|
|
13
|
+
export const PluginOptionsSchema = z
|
|
14
|
+
.object({
|
|
15
|
+
// No schema default: the default depends on the host generation, so
|
|
16
|
+
// callers fill it in with withDefaultMode() before parsing. A caller
|
|
17
|
+
// that forgets fails the parse, and every entry path fails closed.
|
|
18
|
+
mode: z.enum(["block", "redact", "transparent"]),
|
|
19
|
+
configPath: z.string().min(1).optional(),
|
|
20
|
+
deidentify: z.boolean().default(false),
|
|
21
|
+
/** Opt-in readonly env query tools over `settings.env_files`. Default off. */
|
|
22
|
+
envQueries: z.boolean().default(false),
|
|
23
|
+
/**
|
|
24
|
+
* Attachment policy for transparent mode. "block" (default) omits
|
|
25
|
+
* media/file parts — a notice replaces them, binary never crosses the
|
|
26
|
+
* boundary, and the conversation survives. "allow" sends attachments
|
|
27
|
+
* through unscanned with a visible notice part: an explicit,
|
|
28
|
+
* configuration-level decision by the user to vouch for their content.
|
|
29
|
+
* (DLP-standard action names: block / allow.)
|
|
30
|
+
*/
|
|
31
|
+
attachments: z.enum(["block", "allow"]).default("block"),
|
|
32
|
+
/** Transparent mode only. False is hook-only: no outbound HTTP/WS/SDK
|
|
33
|
+
* admission, metadata inspection, final body scan, or provider transform.
|
|
34
|
+
* Content/tool hooks remain, with optional best-effort ingress restoration
|
|
35
|
+
* for successfully prepared session/kind/model scopes. Unknown replies
|
|
36
|
+
* stay literal. Omission preserves the full wire-protection layer. */
|
|
37
|
+
wireProtection: z.boolean().optional(),
|
|
38
|
+
/**
|
|
39
|
+
* W2 (2026-10-07): opt-in encrypted mapping persistence for transparent
|
|
40
|
+
* mode. Default off — with persistence disabled every existing behavior
|
|
41
|
+
* and guarantee is unchanged (memory-only, process-lifetime mappings).
|
|
42
|
+
* Enabled: mappings persist encrypted (per-entry AES-256-GCM) in a
|
|
43
|
+
* SQLite store and rehydrate after restarts. Key custody resolves down
|
|
44
|
+
* the ladder OS keychain → explicit keyfile → memory-only degrade;
|
|
45
|
+
* conversations are never blocked by a missing key.
|
|
46
|
+
*/
|
|
47
|
+
persistence: z
|
|
48
|
+
.object({
|
|
49
|
+
enabled: z.boolean().default(false),
|
|
50
|
+
/** Session TTL in days; 0 = never expire (manual forget only). */
|
|
51
|
+
ttlDays: z.number().int().min(0).max(3650).default(7),
|
|
52
|
+
/** Store path. Default: ~/.local/share/leakshield/SGP.db */
|
|
53
|
+
path: z.string().min(1).optional(),
|
|
54
|
+
/** Opt-in weaker fallback when no OS keychain is available. */
|
|
55
|
+
keyFile: z.boolean().default(false),
|
|
56
|
+
/** Overrides the default keyfile location. */
|
|
57
|
+
keyfilePath: z.string().min(1).optional(),
|
|
58
|
+
})
|
|
59
|
+
.default({}),
|
|
60
|
+
})
|
|
61
|
+
.refine((options) => !options.deidentify || options.mode === "redact", {
|
|
62
|
+
message: "deidentify requires mode 'redact'",
|
|
63
|
+
})
|
|
64
|
+
.refine((options) => options.mode !== "transparent" || !options.envQueries, {
|
|
65
|
+
message: "envQueries is not supported in mode 'transparent'",
|
|
66
|
+
})
|
|
67
|
+
.refine((options) => options.attachments === "block" || options.mode === "transparent", {
|
|
68
|
+
message: 'attachments "allow" requires mode "transparent"',
|
|
69
|
+
})
|
|
70
|
+
.refine((options) => options.wireProtection === undefined || options.mode === "transparent", {
|
|
71
|
+
message: 'wireProtection requires mode "transparent"',
|
|
72
|
+
})
|
|
73
|
+
.refine((options) => !options.persistence.enabled || options.mode === "transparent", {
|
|
74
|
+
message: 'persistence requires mode "transparent"',
|
|
75
|
+
});
|
|
76
|
+
/** Mode used when the options omit `mode`: transparent on v2, block on v1
|
|
77
|
+
* (transparent is v2-only). */
|
|
78
|
+
export function defaultMode(generation) {
|
|
79
|
+
return generation === "v2" ? "transparent" : "block";
|
|
80
|
+
}
|
|
81
|
+
/** Fills in the generation's default mode when the options omit it. Any
|
|
82
|
+
* other non-object input is returned unchanged so the schema rejects it. */
|
|
83
|
+
export function withDefaultMode(input, generation) {
|
|
84
|
+
if (input === undefined || input === null)
|
|
85
|
+
return { mode: defaultMode(generation) };
|
|
86
|
+
if (typeof input !== "object" || Array.isArray(input))
|
|
87
|
+
return input;
|
|
88
|
+
if (input.mode !== undefined)
|
|
89
|
+
return input;
|
|
90
|
+
return { ...input, mode: defaultMode(generation) };
|
|
91
|
+
}
|
|
92
|
+
export function validatePluginOptions(input, generation) {
|
|
93
|
+
const parsed = PluginOptionsSchema.safeParse(withDefaultMode(input, generation));
|
|
94
|
+
if (parsed.success)
|
|
95
|
+
return { ok: true, options: parsed.data };
|
|
96
|
+
return {
|
|
97
|
+
ok: false,
|
|
98
|
+
error: parsed.error.issues.map((issue) => `${issue.path.join(".") || "(root)"}: ${issue.message}`).join("; "),
|
|
99
|
+
};
|
|
100
|
+
}
|