leakshield 0.3.1 → 0.5.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.
Files changed (112) hide show
  1. package/README.md +144 -546
  2. package/README.zh-TW.md +143 -520
  3. package/dist/adapters/claude-code/capabilities.d.ts +14 -0
  4. package/dist/adapters/claude-code/capabilities.js +47 -0
  5. package/dist/adapters/claude-code/cli.d.ts +10 -0
  6. package/dist/adapters/claude-code/cli.js +51 -0
  7. package/dist/adapters/claude-code/doctor.d.ts +19 -0
  8. package/dist/adapters/claude-code/doctor.js +177 -0
  9. package/dist/adapters/claude-code/proxy.d.ts +47 -0
  10. package/dist/adapters/claude-code/proxy.js +348 -0
  11. package/dist/adapters/opencode/capabilities.d.ts +10 -0
  12. package/dist/adapters/opencode/capabilities.js +48 -0
  13. package/dist/adapters/opencode/doctor.d.ts +67 -0
  14. package/dist/{doctor.js → adapters/opencode/doctor.js} +26 -44
  15. package/dist/adapters/opencode/env-queries.d.ts +133 -0
  16. package/dist/adapters/opencode/env-tools.d.ts +20 -0
  17. package/dist/adapters/opencode/hooks.d.ts +4 -0
  18. package/dist/adapters/opencode/plugin.d.ts +12 -0
  19. package/dist/{opencode.js → adapters/opencode/plugin.js} +43 -14
  20. package/dist/adapters/opencode/tool-policy.d.ts +2 -0
  21. package/dist/adapters/opencode/tool-policy.js +23 -0
  22. package/dist/adapters/opencode/transparent-aisdk.d.ts +6 -0
  23. package/dist/{opencode-transparent-aisdk.js → adapters/opencode/transparent-aisdk.js} +1 -1
  24. package/dist/adapters/opencode/transparent.d.ts +70 -0
  25. package/dist/adapters/opencode/transparent.js +242 -0
  26. package/dist/adapters/opencode/v2.d.ts +23 -0
  27. package/dist/{opencode-v2.js → adapters/opencode/v2.js} +13 -8
  28. package/dist/adapters/pi/capabilities.d.ts +10 -0
  29. package/dist/adapters/pi/capabilities.js +50 -0
  30. package/dist/adapters/pi/doctor.d.ts +26 -0
  31. package/dist/adapters/pi/doctor.js +276 -0
  32. package/dist/adapters/pi/extension.d.ts +28 -0
  33. package/dist/adapters/pi/extension.js +109 -0
  34. package/dist/adapters/pi/guard.d.ts +33 -0
  35. package/dist/adapters/pi/guard.js +154 -0
  36. package/dist/adapters/pi/persistence.d.ts +9 -0
  37. package/dist/adapters/pi/persistence.js +69 -0
  38. package/dist/adapters/pi/settings.d.ts +51 -0
  39. package/dist/adapters/pi/settings.js +136 -0
  40. package/dist/adapters/pi/tool-policy.d.ts +11 -0
  41. package/dist/adapters/pi/tool-policy.js +114 -0
  42. package/dist/cli.d.ts +18 -0
  43. package/dist/cli.js +17 -6
  44. package/dist/config/loader.d.ts +70 -0
  45. package/dist/config/loader.js +8 -0
  46. package/dist/config/plain-object.d.ts +2 -0
  47. package/dist/core/index.d.ts +9 -0
  48. package/dist/core/index.js +9 -0
  49. package/dist/core/session-guard.d.ts +62 -0
  50. package/dist/{opencode-transparent.js → core/session-guard.js} +56 -713
  51. package/dist/core/sse-recovery.d.ts +6 -0
  52. package/dist/core/tool-restore-policy.d.ts +153 -0
  53. package/dist/core/tool-restore-policy.js +453 -0
  54. package/dist/core/wire.d.ts +213 -0
  55. package/dist/{opencode-transparent-http.js → core/wire.js} +125 -42
  56. package/dist/core/ws.d.ts +26 -0
  57. package/dist/{opencode-transparent-ws.js → core/ws.js} +1 -1
  58. package/dist/decision/deidentify.d.ts +63 -0
  59. package/dist/decision/ingress.d.ts +40 -0
  60. package/dist/decision/mapping-key.d.ts +91 -0
  61. package/dist/decision/mapping-store.d.ts +166 -0
  62. package/dist/decision/model.d.ts +41 -0
  63. package/dist/decision/pseudonymization.d.ts +176 -0
  64. package/dist/decision/redaction.d.ts +56 -0
  65. package/dist/decision/redaction.js +3 -0
  66. package/dist/decision/schema-strings.d.ts +23 -0
  67. package/dist/decision/schema-strings.js +2 -2
  68. package/dist/decision/value-search.d.ts +39 -0
  69. package/dist/doctor-capabilities.d.ts +21 -0
  70. package/dist/doctor-capabilities.js +58 -0
  71. package/dist/doctor-hosts.d.ts +18 -0
  72. package/dist/doctor-hosts.js +144 -0
  73. package/dist/doctor-report.d.ts +45 -0
  74. package/dist/doctor-report.js +111 -0
  75. package/dist/forget.d.ts +18 -0
  76. package/dist/guard-options.d.ts +45 -0
  77. package/dist/{plugin-options.js → guard-options.js} +8 -6
  78. package/dist/help.d.ts +2 -0
  79. package/dist/help.js +15 -1
  80. package/dist/i18n/en.d.ts +65 -0
  81. package/dist/i18n/index.d.ts +15 -0
  82. package/dist/i18n/zh-TW.d.ts +65 -0
  83. package/dist/index.d.ts +13 -0
  84. package/dist/index.js +1 -1
  85. package/dist/rules/builtin.d.ts +4 -0
  86. package/dist/rules/builtin.js +75 -2
  87. package/dist/rules/dynamic.d.ts +33 -0
  88. package/dist/scanner/allowlist.d.ts +22 -0
  89. package/dist/scanner/cidr.d.ts +39 -0
  90. package/dist/scanner/encoding.d.ts +87 -0
  91. package/dist/scanner/engine.d.ts +43 -0
  92. package/dist/scanner/exact-values.d.ts +11 -0
  93. package/dist/scanner/redaction-verifier.d.ts +24 -0
  94. package/dist/scanner/remotes.d.ts +29 -0
  95. package/dist/scanner/unicode.d.ts +57 -0
  96. package/dist/scanner/utils.d.ts +22 -0
  97. package/dist/transports/cli.d.ts +27 -0
  98. package/dist/transports/web-ui.d.ts +1 -0
  99. package/dist/transports/web-ui.js +1 -1
  100. package/dist/transports/web.d.ts +4 -0
  101. package/dist/types/capabilities.d.ts +67 -0
  102. package/dist/types/capabilities.js +133 -0
  103. package/dist/types.d.ts +149 -0
  104. package/dist/types.js +3 -0
  105. package/dist/utils/files.d.ts +3 -0
  106. package/dist/utils/path-security.d.ts +18 -0
  107. package/dist/utils/win-encoding.d.ts +16 -0
  108. package/package.json +15 -3
  109. /package/dist/{env-queries.js → adapters/opencode/env-queries.js} +0 -0
  110. /package/dist/{opencode-env-tools.js → adapters/opencode/env-tools.js} +0 -0
  111. /package/dist/{opencode-hooks.js → adapters/opencode/hooks.js} +0 -0
  112. /package/dist/{opencode-transparent-stream.js → core/sse-recovery.js} +0 -0
@@ -0,0 +1,70 @@
1
+ import { z } from "zod";
2
+ import type { GuardConfig } from "../types.js";
3
+ export declare const GuardConfigSchema: z.ZodPipe<z.ZodCustom<unknown, unknown>, z.ZodObject<{
4
+ nda_keywords: z.ZodDefault<z.ZodArray<z.ZodString>>;
5
+ private_domains: z.ZodDefault<z.ZodArray<z.ZodString>>;
6
+ custom_rules: z.ZodPipe<z.ZodDefault<z.ZodNullable<z.ZodArray<z.ZodObject<{
7
+ description: z.ZodString;
8
+ pattern: z.ZodString;
9
+ severity: z.ZodDefault<z.ZodEnum<{
10
+ HIGH: "HIGH";
11
+ MEDIUM: "MEDIUM";
12
+ LOW: "LOW";
13
+ }>>;
14
+ flags: z.ZodOptional<z.ZodString>;
15
+ word_boundary: z.ZodOptional<z.ZodBoolean>;
16
+ allow: z.ZodOptional<z.ZodArray<z.ZodString>>;
17
+ path: z.ZodOptional<z.ZodArray<z.ZodString>>;
18
+ }, z.core.$strip>>>>, z.ZodTransform<{
19
+ description: string;
20
+ pattern: string;
21
+ severity: "HIGH" | "MEDIUM" | "LOW";
22
+ flags?: string | undefined;
23
+ word_boundary?: boolean | undefined;
24
+ allow?: string[] | undefined;
25
+ path?: string[] | undefined;
26
+ }[], {
27
+ description: string;
28
+ pattern: string;
29
+ severity: "HIGH" | "MEDIUM" | "LOW";
30
+ flags?: string | undefined;
31
+ word_boundary?: boolean | undefined;
32
+ allow?: string[] | undefined;
33
+ path?: string[] | undefined;
34
+ }[] | null>>;
35
+ settings: z.ZodPrefault<z.ZodPipe<z.ZodCustom<unknown, unknown>, z.ZodObject<{
36
+ max_chars: z.ZodDefault<z.ZodNumber>;
37
+ show_line_numbers: z.ZodDefault<z.ZodBoolean>;
38
+ preview_length: z.ZodDefault<z.ZodNumber>;
39
+ encoding_detection: z.ZodDefault<z.ZodBoolean>;
40
+ env_files: z.ZodDefault<z.ZodArray<z.ZodString>>;
41
+ rule_overrides: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodBoolean>>;
42
+ private_cidrs: z.ZodDefault<z.ZodArray<z.ZodString>>;
43
+ allowlist: z.ZodDefault<z.ZodArray<z.ZodString>>;
44
+ }, z.core.$strip>>>;
45
+ }, z.core.$strip>>;
46
+ /**
47
+ * Resolves the guard config path exactly like `loadConfig` does: the override
48
+ * when it exists, otherwise the first default candidate that exists. Returns
49
+ * undefined when no config file is present (defaults apply).
50
+ */
51
+ export declare function findConfigPath(overridePath?: string): string | undefined;
52
+ export interface LoadedGuardConfig {
53
+ config: GuardConfig;
54
+ /**
55
+ * sha256 of the exact bytes `config` was parsed from, or "absent" when no
56
+ * configuration file exists (defaults apply). Consumers that authorize from
57
+ * this config can compare the fingerprint on later reads.
58
+ */
59
+ fingerprint: string;
60
+ configPath: string | undefined;
61
+ }
62
+ /**
63
+ * Loads the guard config and, from the very same read, the fingerprint of the
64
+ * bytes it was parsed from. The allowlist inside `config` and `fingerprint`
65
+ * are therefore guaranteed to describe one configuration state — an edit
66
+ * between a config load and a separate fingerprint read could otherwise
67
+ * authorize against content that never existed at a single point in time.
68
+ */
69
+ export declare function loadConfigWithFingerprint(overridePath?: string, workspaceRoot?: string): LoadedGuardConfig;
70
+ export declare function loadConfig(overridePath?: string, workspaceRoot?: string): GuardConfig;
@@ -129,6 +129,14 @@ export function findConfigPath(overridePath) {
129
129
  * authorize against content that never existed at a single point in time.
130
130
  */
131
131
  export function loadConfigWithFingerprint(overridePath, workspaceRoot = process.cwd()) {
132
+ // An explicit path (OpenCode configPath, pi LEAKSHIELD_CONFIG, proxy --config)
133
+ // that does not exist fails the load. Falling back to the built-in defaults
134
+ // would silently drop the user's keywords, domains, rules and env_files.
135
+ // Hosts fail closed on this error; only the doctor and terminal output show
136
+ // the path.
137
+ if (overridePath && !fs.existsSync(overridePath)) {
138
+ throw new Error(`guard config file not found: ${overridePath}`);
139
+ }
132
140
  const configPath = findConfigPath(overridePath);
133
141
  if (!configPath) {
134
142
  return {
@@ -0,0 +1,2 @@
1
+ import { z } from "zod";
2
+ export declare function plainObject<T extends z.ZodType>(schema: T): z.ZodPipe<z.ZodCustom<unknown, unknown>, T>;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Experimental host-neutral LeakShield core entrypoint.
3
+ *
4
+ * This subpath is an incremental packaging boundary, not a separate npm
5
+ * package. Tool restoration requires an explicit HostToolPolicy; without one, token-bearing tool calls are refused.
6
+ *
7
+ * Do not import OpenCode or Claude Code adapters from this module.
8
+ */
9
+ export { createGuardCore, requireSessionID, INITIALIZATION_ERROR, NO_REASON_HINT, PROTECT_FAILED_ERROR, ATTRIBUTION_ERROR, UNSUPPORTED_ROUTE_ERROR, type GuardCore, type GuardPersistenceOptions, type HostToolPolicy, type RecordLike, } from "./session-guard.js";
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Experimental host-neutral LeakShield core entrypoint.
3
+ *
4
+ * This subpath is an incremental packaging boundary, not a separate npm
5
+ * package. Tool restoration requires an explicit HostToolPolicy; without one, token-bearing tool calls are refused.
6
+ *
7
+ * Do not import OpenCode or Claude Code adapters from this module.
8
+ */
9
+ export { createGuardCore, requireSessionID, INITIALIZATION_ERROR, NO_REASON_HINT, PROTECT_FAILED_ERROR, ATTRIBUTION_ERROR, UNSUPPORTED_ROUTE_ERROR, } from "./session-guard.js";
@@ -0,0 +1,62 @@
1
+ import { type MappingKeyProvider } from "../decision/mapping-store.js";
2
+ import { createResponsesWebSocketAdapter } from "./ws.js";
3
+ import { ToolLeafPolicy } from "./tool-restore-policy.js";
4
+ import type { GuardConfig } from "../types.js";
5
+ export type RecordLike = {
6
+ [key: string]: unknown;
7
+ };
8
+ export declare const INITIALIZATION_ERROR = "LeakShield initialization failed, so the request was blocked before anything was sent.";
9
+ export declare const NO_REASON_HINT = "Run `leakshield --doctor` to see why, fix it and restart OpenCode.";
10
+ export declare const PROTECT_FAILED_ERROR = "LeakShield blocked a model request because it could not be masked safely.";
11
+ export declare const ATTRIBUTION_ERROR = "LeakShield blocked a response because it could not be matched to a request.";
12
+ export declare const UNSUPPORTED_ROUTE_ERROR = "LeakShield blocked a request on an unsupported route.";
13
+ export declare function requireSessionID(event: RecordLike): string;
14
+ /** Persistence wiring options (W2/B4 + R-round), mapped from
15
+ * PluginOptions.persistence. `keyProvider` is a test/CI injection seam —
16
+ * production callers leave it unset and the OS keychain ladder resolves;
17
+ * an explicit null forces the ladder-bottom memory-only path deterministically. */
18
+ export interface GuardPersistenceOptions {
19
+ ttlDays: number;
20
+ path: string;
21
+ keyFile: boolean;
22
+ keyfilePath?: string;
23
+ keyProvider?: MappingKeyProvider | null;
24
+ }
25
+ /** Host-defined classification. Without this capability, token-bearing tool calls refuse. */
26
+ export interface HostToolPolicy {
27
+ isCodeModeCall(event: RecordLike): boolean;
28
+ isShellTool(rawTool: string): boolean;
29
+ classifyField(key: string, inherited: ToolLeafPolicy): ToolLeafPolicy;
30
+ /** `rawTool` is the tool name as the host sent it, not lowercased. */
31
+ allowsDegradedRestore(rawTool: string, input: unknown): boolean;
32
+ }
33
+ export interface GuardCore {
34
+ readonly hasSession: (sessionID: string) => boolean;
35
+ readonly assertTrackable: (sessionID: string) => void;
36
+ readonly ensureSeeded: (sessionID: string) => void;
37
+ readonly track: (sessionID: string) => void;
38
+ readonly persistIfChanged: (sessionID: string) => void;
39
+ readonly protectRequestKind: (event: RecordLike, kind: "primary" | "title" | "compaction" | "generate") => void;
40
+ readonly onHttpRequest: (event: RecordLike) => Promise<void>;
41
+ readonly onHttpResponse: (event: RecordLike) => Promise<void>;
42
+ readonly onToolBefore: (event: RecordLike) => void;
43
+ readonly releaseSession: (sessionID: string) => void;
44
+ readonly noticeOnce: (message: string) => void;
45
+ readonly webSocket: ReturnType<typeof createResponsesWebSocketAdapter>;
46
+ readonly assertWritableDataProperty: (record: object, key: string | number) => void;
47
+ /** Releases handles without clearing sessions: used when registration fails before any hook ran. */
48
+ readonly closeResources: () => void;
49
+ readonly dispose: () => void;
50
+ }
51
+ /**
52
+ * Creates the host-independent guard core for one plugin setup (= one OpenCode
53
+ * process): a single bounded pseudonymizer, exact-session mapping and private
54
+ * request attribution. The host layer registers hooks that call its handlers.
55
+ * With persistence options the core additionally hydrates sessions from the
56
+ * encrypted store on first touch and writes committed mappings back (decision
57
+ * §4: dispose unloads hooks but never deletes persisted mappings — forget is
58
+ * the only erasure path). Returns null when the pseudonymizer cannot be built.
59
+ */
60
+ export declare function createGuardCore(config: GuardConfig, attachmentsPolicy?: "block" | "allow", wireProtection?: boolean, persistence?: GuardPersistenceOptions, dialect?: {
61
+ anthropicThinking?: "refuse" | "mask";
62
+ }, hostToolPolicy?: HostToolPolicy): GuardCore | null;