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.
- package/README.md +144 -546
- package/README.zh-TW.md +143 -520
- package/dist/adapters/claude-code/capabilities.d.ts +14 -0
- package/dist/adapters/claude-code/capabilities.js +47 -0
- package/dist/adapters/claude-code/cli.d.ts +10 -0
- package/dist/adapters/claude-code/cli.js +51 -0
- package/dist/adapters/claude-code/doctor.d.ts +19 -0
- package/dist/adapters/claude-code/doctor.js +177 -0
- package/dist/adapters/claude-code/proxy.d.ts +47 -0
- package/dist/adapters/claude-code/proxy.js +348 -0
- package/dist/adapters/opencode/capabilities.d.ts +10 -0
- package/dist/adapters/opencode/capabilities.js +48 -0
- package/dist/adapters/opencode/doctor.d.ts +67 -0
- package/dist/{doctor.js → adapters/opencode/doctor.js} +26 -44
- package/dist/adapters/opencode/env-queries.d.ts +133 -0
- package/dist/adapters/opencode/env-tools.d.ts +20 -0
- package/dist/adapters/opencode/hooks.d.ts +4 -0
- package/dist/adapters/opencode/plugin.d.ts +12 -0
- package/dist/{opencode.js → adapters/opencode/plugin.js} +43 -14
- package/dist/adapters/opencode/tool-policy.d.ts +2 -0
- package/dist/adapters/opencode/tool-policy.js +23 -0
- package/dist/adapters/opencode/transparent-aisdk.d.ts +6 -0
- package/dist/{opencode-transparent-aisdk.js → adapters/opencode/transparent-aisdk.js} +1 -1
- package/dist/adapters/opencode/transparent.d.ts +70 -0
- package/dist/adapters/opencode/transparent.js +242 -0
- package/dist/adapters/opencode/v2.d.ts +23 -0
- package/dist/{opencode-v2.js → adapters/opencode/v2.js} +13 -8
- package/dist/adapters/pi/capabilities.d.ts +10 -0
- package/dist/adapters/pi/capabilities.js +50 -0
- package/dist/adapters/pi/doctor.d.ts +26 -0
- package/dist/adapters/pi/doctor.js +276 -0
- package/dist/adapters/pi/extension.d.ts +28 -0
- package/dist/adapters/pi/extension.js +109 -0
- package/dist/adapters/pi/guard.d.ts +33 -0
- package/dist/adapters/pi/guard.js +154 -0
- package/dist/adapters/pi/persistence.d.ts +9 -0
- package/dist/adapters/pi/persistence.js +69 -0
- package/dist/adapters/pi/settings.d.ts +51 -0
- package/dist/adapters/pi/settings.js +136 -0
- package/dist/adapters/pi/tool-policy.d.ts +11 -0
- package/dist/adapters/pi/tool-policy.js +114 -0
- package/dist/cli.d.ts +18 -0
- package/dist/cli.js +17 -6
- package/dist/config/loader.d.ts +70 -0
- package/dist/config/loader.js +8 -0
- package/dist/config/plain-object.d.ts +2 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.js +9 -0
- package/dist/core/session-guard.d.ts +62 -0
- package/dist/{opencode-transparent.js → core/session-guard.js} +56 -713
- package/dist/core/sse-recovery.d.ts +6 -0
- package/dist/core/tool-restore-policy.d.ts +153 -0
- package/dist/core/tool-restore-policy.js +453 -0
- package/dist/core/wire.d.ts +213 -0
- package/dist/{opencode-transparent-http.js → core/wire.js} +125 -42
- package/dist/core/ws.d.ts +26 -0
- package/dist/{opencode-transparent-ws.js → core/ws.js} +1 -1
- package/dist/decision/deidentify.d.ts +63 -0
- package/dist/decision/ingress.d.ts +40 -0
- package/dist/decision/mapping-key.d.ts +91 -0
- package/dist/decision/mapping-store.d.ts +166 -0
- package/dist/decision/model.d.ts +41 -0
- package/dist/decision/pseudonymization.d.ts +176 -0
- package/dist/decision/redaction.d.ts +56 -0
- package/dist/decision/redaction.js +3 -0
- package/dist/decision/schema-strings.d.ts +23 -0
- package/dist/decision/schema-strings.js +2 -2
- package/dist/decision/value-search.d.ts +39 -0
- package/dist/doctor-capabilities.d.ts +21 -0
- package/dist/doctor-capabilities.js +58 -0
- package/dist/doctor-hosts.d.ts +18 -0
- package/dist/doctor-hosts.js +144 -0
- package/dist/doctor-report.d.ts +45 -0
- package/dist/doctor-report.js +111 -0
- package/dist/forget.d.ts +18 -0
- package/dist/guard-options.d.ts +45 -0
- package/dist/{plugin-options.js → guard-options.js} +8 -6
- package/dist/help.d.ts +2 -0
- package/dist/help.js +15 -1
- package/dist/i18n/en.d.ts +65 -0
- package/dist/i18n/index.d.ts +15 -0
- package/dist/i18n/zh-TW.d.ts +65 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +1 -1
- package/dist/rules/builtin.d.ts +4 -0
- package/dist/rules/builtin.js +75 -2
- package/dist/rules/dynamic.d.ts +33 -0
- package/dist/scanner/allowlist.d.ts +22 -0
- package/dist/scanner/cidr.d.ts +39 -0
- package/dist/scanner/encoding.d.ts +87 -0
- package/dist/scanner/engine.d.ts +43 -0
- package/dist/scanner/exact-values.d.ts +11 -0
- package/dist/scanner/redaction-verifier.d.ts +24 -0
- package/dist/scanner/remotes.d.ts +29 -0
- package/dist/scanner/unicode.d.ts +57 -0
- package/dist/scanner/utils.d.ts +22 -0
- package/dist/transports/cli.d.ts +27 -0
- package/dist/transports/web-ui.d.ts +1 -0
- package/dist/transports/web-ui.js +1 -1
- package/dist/transports/web.d.ts +4 -0
- package/dist/types/capabilities.d.ts +67 -0
- package/dist/types/capabilities.js +133 -0
- package/dist/types.d.ts +149 -0
- package/dist/types.js +3 -0
- package/dist/utils/files.d.ts +3 -0
- package/dist/utils/path-security.d.ts +18 -0
- package/dist/utils/win-encoding.d.ts +16 -0
- package/package.json +15 -3
- /package/dist/{env-queries.js → adapters/opencode/env-queries.js} +0 -0
- /package/dist/{opencode-env-tools.js → adapters/opencode/env-tools.js} +0 -0
- /package/dist/{opencode-hooks.js → adapters/opencode/hooks.js} +0 -0
- /package/dist/{opencode-transparent-stream.js → core/sse-recovery.js} +0 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
interface RewrittenEvent {
|
|
2
|
+
output: string;
|
|
3
|
+
holding: boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare function createRecoveringSseStream(reader: ReadableStreamDefaultReader<Uint8Array>, rewrite: (lines: string[], terminated: boolean) => RewrittenEvent, abandon: () => void): ReadableStream<Uint8Array>;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/** Shell syntax excluded from unquoted literal words. Known tokens are
|
|
2
|
+
* removed before this check; a token-shaped prefix alone grants no exemption. */
|
|
3
|
+
export declare const UNSUPPORTED_SHELL_SYNTAX: RegExp;
|
|
4
|
+
export type ShellQuote = "none" | "single" | "double";
|
|
5
|
+
export interface LiteralShellWord {
|
|
6
|
+
start: number;
|
|
7
|
+
end: number;
|
|
8
|
+
value: string;
|
|
9
|
+
quote: ShellQuote;
|
|
10
|
+
redirect: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** A deliberately small literal-word lexer, not a general shell parser.
|
|
13
|
+
* Quotes must enclose the entire word. No escape decoding, expansions, quote
|
|
14
|
+
* concatenation or control characters are accepted. Decode command/flag
|
|
15
|
+
* quotes too, so `'sh' '-c' TOKEN` cannot bypass the interpreter gate. */
|
|
16
|
+
export declare function parseLiteralShellWords(text: string): LiteralShellWord[] | null;
|
|
17
|
+
export declare const ASSIGNMENT_WORD: RegExp;
|
|
18
|
+
export declare const SHELL_COMMAND_WRAPPERS: Set<string>;
|
|
19
|
+
export declare const INLINE_SCRIPT_FLAGS: Map<string, Set<string>>;
|
|
20
|
+
export declare const SCRIPT_ARGUMENT_INTERPRETERS: Set<string>;
|
|
21
|
+
export declare const SHELL_INTERPRETERS: Set<string>;
|
|
22
|
+
/**
|
|
23
|
+
* `rtk` is a command-output wrapper: `rtk <command> <args>` runs the command
|
|
24
|
+
* and condenses its output, so the command actually executed is the word
|
|
25
|
+
* after it. Without this, `rtk sh -c T` would be judged as plain arguments to
|
|
26
|
+
* `rtk` and restore a token into a second shell parse. These subcommands run
|
|
27
|
+
* another command themselves (`rtk err sh -c T`, `rtk proxy sh -c T`), so the
|
|
28
|
+
* word after them cannot be judged as the executed command and the form is
|
|
29
|
+
* refused. The list comes from `rtk --help` for rtk 1.1.0; a new rtk release
|
|
30
|
+
* may add runners. `rtk` counts however it's spelled (a path, quotes, any case),
|
|
31
|
+
* as the other command checks do.
|
|
32
|
+
*/
|
|
33
|
+
export declare const RTK_COMMAND_RUNNERS: Set<string>;
|
|
34
|
+
/**
|
|
35
|
+
* Index of the word that names the command actually executed: the first
|
|
36
|
+
* non-assignment word, or the word after a leading `rtk`. Returns -1 when
|
|
37
|
+
* there is no such word or the form must be refused (`rtk` alone, an option
|
|
38
|
+
* after `rtk`, `rtk` wrapping `rtk`, or an rtk subcommand that runs another
|
|
39
|
+
* command).
|
|
40
|
+
*/
|
|
41
|
+
export declare function resolveExecutedCommandIndex(words: readonly LiteralShellWord[]): number;
|
|
42
|
+
/** Returns only ordinary arguments, never command/assignment/redirect words. */
|
|
43
|
+
export declare function parseSupportedShellArguments(text: string): LiteralShellWord[] | null;
|
|
44
|
+
/** Characters forbidden inside restored shell argument words. */
|
|
45
|
+
export declare const SHELL_UNSAFE: RegExp;
|
|
46
|
+
/**
|
|
47
|
+
* Restore policy is decided per field, not per tool. A tool name that is in
|
|
48
|
+
* SHELL_TOOLS or has a shell-interpreter segment (bash, shell, sh, zsh, pwsh,
|
|
49
|
+
* powershell) makes every field "shell". Otherwise a field named like a
|
|
50
|
+
* command (command, cmd, commands, argv, ...) is "shell", a field named like
|
|
51
|
+
* code (code, script, ...) is "inert" (no parsing; originals with
|
|
52
|
+
* shell-unsafe characters are refused), and any other field restores
|
|
53
|
+
* verbatim. The list is built into the source, not configurable; an executing
|
|
54
|
+
* tool using other field names is not detected.
|
|
55
|
+
*/
|
|
56
|
+
export declare const SHELL_TOOLS: Set<string>;
|
|
57
|
+
export type ToolLeafPolicy = "verbatim" | "shell" | "inert";
|
|
58
|
+
/** Splits on non-alphanumerics and camelCase boundaries; lowercase segments. */
|
|
59
|
+
export declare const nameSegments: (raw: string) => string[];
|
|
60
|
+
export declare const SHELL_TOOL_SEGMENTS: Set<string>;
|
|
61
|
+
export declare const METADATA_KEYS: Set<string>;
|
|
62
|
+
export declare const SHELL_KEY_LAST_SEGMENTS: Set<string>;
|
|
63
|
+
export declare const SHELL_KEYS_JOINED: Set<string>;
|
|
64
|
+
export declare const CODE_KEYS_JOINED: Set<string>;
|
|
65
|
+
export declare const isShellTool: (rawTool: string) => boolean;
|
|
66
|
+
export declare const isShellKey: (key: string) => boolean;
|
|
67
|
+
export declare const isCodeKey: (key: string) => boolean;
|
|
68
|
+
export declare const keyPolicy: (key: string, inherited: ToolLeafPolicy) => ToolLeafPolicy;
|
|
69
|
+
/**
|
|
70
|
+
* Tools whose calls are observably read-only: an unresolved placeholder in
|
|
71
|
+
* their arguments reads/greps/lists against a literal string and matches
|
|
72
|
+
* nothing — no state change, no outbound transmission. Only these qualify
|
|
73
|
+
* for P1 (2026-10-06b) best-effort literal passthrough when restoration is
|
|
74
|
+
* incomplete. Everything else — writes, deletes, edits, network calls,
|
|
75
|
+
* shell, unknown tools — refuses the affected invocation.
|
|
76
|
+
*/
|
|
77
|
+
export declare const READ_ONLY_TOOLS: Set<string>;
|
|
78
|
+
/**
|
|
79
|
+
* Content-free degradation notice for the host log: one WARN line, a fixed
|
|
80
|
+
* context word plus a count, never request content. No side files, no
|
|
81
|
+
* provider-body notices (P1, 2026-10-06b).
|
|
82
|
+
*/
|
|
83
|
+
export declare const warnDegradation: (context: string) => void;
|
|
84
|
+
/** Preserve the existing quote context; never add quoting or reinterpret the
|
|
85
|
+
* original as code. Leading '-' stays forbidden even when quoted (options are
|
|
86
|
+
* interpreted after quote removal). Controls are forbidden in every context. */
|
|
87
|
+
export declare function isShellSafeOriginal(text: string, quote: ShellQuote): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* HTTP clients that take credentials inside an argument (`-H 'x-api-key: T'`,
|
|
90
|
+
* `-u user:T`, `--header=x:T`). `wget` is deliberately not listed: its `-e`
|
|
91
|
+
* option family keeps producing bypasses, so it follows the whole-word rule.
|
|
92
|
+
* `isShellSafeOriginal` only covers the first
|
|
93
|
+
* shell parse; commands such as `sh -c`, `python -c`, `ssh` and `eval` parse
|
|
94
|
+
* their arguments again, so embedding is decided by the command that actually
|
|
95
|
+
* executes: the first word, unquoted, exactly one of these names. Paths,
|
|
96
|
+
* quoted names, assignments and wrappers (`env`, `sudo`) do not qualify.
|
|
97
|
+
* `rtk curl ...` does not qualify either: rtk records command lines in its
|
|
98
|
+
* local history database in plaintext, so a restored secret would persist
|
|
99
|
+
* there. Under `rtk` only whole-word placeholders restore.
|
|
100
|
+
*/
|
|
101
|
+
export declare const EMBEDDED_PLACEHOLDER_COMMANDS: Set<string>;
|
|
102
|
+
export declare function commandAllowsEmbeddedTokens(text: string): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* `wget -e CMD` / `--execute CMD` hands its value to wgetrc parsing, a second
|
|
105
|
+
* parse that `isShellSafeOriginal` does not cover (the value can set
|
|
106
|
+
* `output_document`, `post_file` and the like). This check stays although
|
|
107
|
+
* `wget` no longer holds embedded tokens: `wget -e T u` would otherwise
|
|
108
|
+
* restore a whole-word token into wgetrc parsing. The command must be
|
|
109
|
+
* `wget` however it's spelled (a path, quotes, leading assignments), resolved
|
|
110
|
+
* as in `parseSupportedShellArguments`. `curl -e` is a referer and
|
|
111
|
+
* `curl -K`/`--config` only name a file.
|
|
112
|
+
* GNU long options accept unique abbreviations (`--exec`), so any prefix of
|
|
113
|
+
* `--execute` counts; the shorter ambiguous ones make wget exit with an error.
|
|
114
|
+
*/
|
|
115
|
+
export declare function isWgetExecuteCommand(text: string): boolean;
|
|
116
|
+
/** Matches a fully formed reserved token (either version), for per-token shell safety checks. */
|
|
117
|
+
export declare const TOKEN_SCAN: RegExp;
|
|
118
|
+
/**
|
|
119
|
+
* Relaxed (brackets optional, separators up to 4 non-alphanumerics, any case)
|
|
120
|
+
* forms of the SGP2 placeholder, used only to spot a placeholder the
|
|
121
|
+
* model reformatted. Every quantifier is bounded and there is no nested
|
|
122
|
+
* unbounded group. A candidate counts only if its canonical form restores in
|
|
123
|
+
* the session, so no token enumeration is needed.
|
|
124
|
+
*
|
|
125
|
+
* SGP2 reads the letters after the `SGP2` head, tolerating separator runs of
|
|
126
|
+
* up to 4 non-alphanumerics between letters (changed separators) or none at
|
|
127
|
+
* all (removed separators), and stopping at a digit, a longer run or about 80
|
|
128
|
+
* letters. Each known category, compared without underscores, is tried as a
|
|
129
|
+
* prefix of those letters; the next 12 letters become the three segments. The
|
|
130
|
+
* text after a stripped token is often more short words, which this ignores
|
|
131
|
+
* because only exactly 12 letters follow the category.
|
|
132
|
+
*/
|
|
133
|
+
export declare const NEAR_MISS_V2_HEAD: RegExp;
|
|
134
|
+
export declare const NEAR_MISS_V2_MAX_SEPARATOR = 4;
|
|
135
|
+
export declare const NEAR_MISS_V2_MAX_LETTERS = 80;
|
|
136
|
+
export declare const NEAR_MISS_V2_SEGMENT_LETTERS = 12;
|
|
137
|
+
export declare const NEAR_MISS_V2_CATEGORIES: ReadonlyMap<string, string>;
|
|
138
|
+
/** Letters following `from`, skipping short separator runs; stops at a digit or a long run. */
|
|
139
|
+
export declare function collectNearMissLetters(text: string, from: number): string;
|
|
140
|
+
/**
|
|
141
|
+
* Builds the position check for the placeholders of one code/script field.
|
|
142
|
+
* Call the result with each token's [start, end) in increasing order; it keeps
|
|
143
|
+
* its state between calls, so the whole field costs one forward pass however
|
|
144
|
+
* many tokens it holds, and a false result ends the field's check. A token's
|
|
145
|
+
* position passes unless `crossesLineBoundary` or `isInPlainStringLiteral`
|
|
146
|
+
* rejects it.
|
|
147
|
+
*
|
|
148
|
+
* Conservative, language-agnostic, line-based heuristic; it is not a parser
|
|
149
|
+
* and does not confirm the match is in a string. It can both over-refuse and
|
|
150
|
+
* miss. It guards the restoration context only and is not egress protection:
|
|
151
|
+
* code can still print, send or re-interpret the restored value.
|
|
152
|
+
*/
|
|
153
|
+
export declare function createInertPositionCheck(text: string): (start: number, end: number) => boolean;
|
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
import { containsTokenPrefix } from "../decision/pseudonymization.js";
|
|
2
|
+
import { FINDING_TYPES } from "../types.js";
|
|
3
|
+
/** Shell syntax excluded from unquoted literal words. Known tokens are
|
|
4
|
+
* removed before this check; a token-shaped prefix alone grants no exemption. */
|
|
5
|
+
export const UNSUPPORTED_SHELL_SYNTAX = /[\x00-\x1f\x7f'"`\\$;|&<>(){}*?\[\]!~#]/;
|
|
6
|
+
/** A deliberately small literal-word lexer, not a general shell parser.
|
|
7
|
+
* Quotes must enclose the entire word. No escape decoding, expansions, quote
|
|
8
|
+
* concatenation or control characters are accepted. Decode command/flag
|
|
9
|
+
* quotes too, so `'sh' '-c' TOKEN` cannot bypass the interpreter gate. */
|
|
10
|
+
export function parseLiteralShellWords(text) {
|
|
11
|
+
if (/[\x00-\x1f\x7f]/.test(text))
|
|
12
|
+
return null;
|
|
13
|
+
// The new quote/redirection subset is POSIX-specific. Keep the previous
|
|
14
|
+
// conservative behavior on Windows, where the host may launch cmd.exe.
|
|
15
|
+
if (process.platform === "win32" && /['">]/.test(text))
|
|
16
|
+
return null;
|
|
17
|
+
const words = [];
|
|
18
|
+
let at = 0;
|
|
19
|
+
while (at < text.length) {
|
|
20
|
+
if (text[at] === " ") {
|
|
21
|
+
at += 1;
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const start = at;
|
|
25
|
+
if (text[at] === ">") {
|
|
26
|
+
at += text[at + 1] === ">" ? 2 : 1;
|
|
27
|
+
words.push({ start, end: at, value: text.slice(start, at), quote: "none", redirect: true });
|
|
28
|
+
}
|
|
29
|
+
else if (text[at] === "'" || text[at] === '"') {
|
|
30
|
+
const delimiter = text[at++];
|
|
31
|
+
const valueStart = at;
|
|
32
|
+
while (at < text.length && text[at] !== delimiter)
|
|
33
|
+
at += 1;
|
|
34
|
+
if (at === text.length)
|
|
35
|
+
return null;
|
|
36
|
+
const value = text.slice(valueStart, at++);
|
|
37
|
+
const quote = delimiter === "'" ? "single" : "double";
|
|
38
|
+
if (quote === "double" && /[`\\$!]/.test(value))
|
|
39
|
+
return null;
|
|
40
|
+
words.push({ start, end: at, value, quote, redirect: false });
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
while (at < text.length && text[at] !== " ")
|
|
44
|
+
at += 1;
|
|
45
|
+
const value = text.slice(start, at);
|
|
46
|
+
if (UNSUPPORTED_SHELL_SYNTAX.test(value.replace(TOKEN_SCAN, "TOKEN")) || value.includes(">"))
|
|
47
|
+
return null;
|
|
48
|
+
words.push({ start, end: at, value, quote: "none", redirect: false });
|
|
49
|
+
}
|
|
50
|
+
if (at < text.length && text[at] !== " ")
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return words;
|
|
54
|
+
}
|
|
55
|
+
export const ASSIGNMENT_WORD = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
56
|
+
export const SHELL_COMMAND_WRAPPERS = new Set([
|
|
57
|
+
"builtin",
|
|
58
|
+
"command",
|
|
59
|
+
"doas",
|
|
60
|
+
"env",
|
|
61
|
+
"eval",
|
|
62
|
+
"exec",
|
|
63
|
+
"nice",
|
|
64
|
+
"nohup",
|
|
65
|
+
"pkexec",
|
|
66
|
+
"runuser",
|
|
67
|
+
"setsid",
|
|
68
|
+
"source",
|
|
69
|
+
"sudo",
|
|
70
|
+
"su",
|
|
71
|
+
"time",
|
|
72
|
+
"timeout",
|
|
73
|
+
"xargs",
|
|
74
|
+
]);
|
|
75
|
+
export const INLINE_SCRIPT_FLAGS = new Map([
|
|
76
|
+
["python", new Set(["-c"])],
|
|
77
|
+
["python3", new Set(["-c"])],
|
|
78
|
+
["node", new Set(["-e", "--eval", "-p", "--print"])],
|
|
79
|
+
["nodejs", new Set(["-e", "--eval", "-p", "--print"])],
|
|
80
|
+
["ruby", new Set(["-e", "-pe", "-ne"])],
|
|
81
|
+
["perl", new Set(["-e", "-pe", "-ne", "-E"])],
|
|
82
|
+
["php", new Set(["-r"])],
|
|
83
|
+
["lua", new Set(["-e"])],
|
|
84
|
+
["powershell", new Set(["-c", "-command", "-encodedcommand", "-e"])],
|
|
85
|
+
["pwsh", new Set(["-c", "-command", "-encodedcommand", "-e"])],
|
|
86
|
+
["cmd", new Set(["/c", "/k"])],
|
|
87
|
+
]);
|
|
88
|
+
export const SCRIPT_ARGUMENT_INTERPRETERS = new Set(["awk", "sed", "jq"]);
|
|
89
|
+
export const SHELL_INTERPRETERS = new Set(["sh", "bash", "dash", "zsh", "ksh", "ash", "csh", "tcsh", "fish"]);
|
|
90
|
+
/**
|
|
91
|
+
* `rtk` is a command-output wrapper: `rtk <command> <args>` runs the command
|
|
92
|
+
* and condenses its output, so the command actually executed is the word
|
|
93
|
+
* after it. Without this, `rtk sh -c T` would be judged as plain arguments to
|
|
94
|
+
* `rtk` and restore a token into a second shell parse. These subcommands run
|
|
95
|
+
* another command themselves (`rtk err sh -c T`, `rtk proxy sh -c T`), so the
|
|
96
|
+
* word after them cannot be judged as the executed command and the form is
|
|
97
|
+
* refused. The list comes from `rtk --help` for rtk 1.1.0; a new rtk release
|
|
98
|
+
* may add runners. `rtk` counts however it's spelled (a path, quotes, any case),
|
|
99
|
+
* as the other command checks do.
|
|
100
|
+
*/
|
|
101
|
+
export const RTK_COMMAND_RUNNERS = new Set(["err", "test", "summary", "run", "bypass", "proxy", "pipe"]);
|
|
102
|
+
/**
|
|
103
|
+
* Index of the word that names the command actually executed: the first
|
|
104
|
+
* non-assignment word, or the word after a leading `rtk`. Returns -1 when
|
|
105
|
+
* there is no such word or the form must be refused (`rtk` alone, an option
|
|
106
|
+
* after `rtk`, `rtk` wrapping `rtk`, or an rtk subcommand that runs another
|
|
107
|
+
* command).
|
|
108
|
+
*/
|
|
109
|
+
export function resolveExecutedCommandIndex(words) {
|
|
110
|
+
const commandIndex = words.findIndex((word) => word.quote !== "none" || !ASSIGNMENT_WORD.test(word.value));
|
|
111
|
+
if (commandIndex === -1)
|
|
112
|
+
return -1;
|
|
113
|
+
const command = words[commandIndex];
|
|
114
|
+
if (command.redirect || command.value.split("/").at(-1)?.toLowerCase() !== "rtk")
|
|
115
|
+
return commandIndex;
|
|
116
|
+
const next = words[commandIndex + 1];
|
|
117
|
+
if (next === undefined || next.redirect || next.value.startsWith("-"))
|
|
118
|
+
return -1;
|
|
119
|
+
if (next.value.split("/").at(-1)?.toLowerCase() === "rtk")
|
|
120
|
+
return -1;
|
|
121
|
+
if (RTK_COMMAND_RUNNERS.has(next.value.toLowerCase()))
|
|
122
|
+
return -1;
|
|
123
|
+
return commandIndex + 1;
|
|
124
|
+
}
|
|
125
|
+
/** Returns only ordinary arguments, never command/assignment/redirect words. */
|
|
126
|
+
export function parseSupportedShellArguments(text) {
|
|
127
|
+
const parsed = parseLiteralShellWords(text);
|
|
128
|
+
if (parsed === null)
|
|
129
|
+
return null;
|
|
130
|
+
const redirectIndex = parsed.findIndex((word) => word.redirect);
|
|
131
|
+
if (redirectIndex !== -1) {
|
|
132
|
+
const target = parsed[redirectIndex + 1];
|
|
133
|
+
if (redirectIndex === 0 || redirectIndex !== parsed.length - 2 ||
|
|
134
|
+
target.redirect || target.value.length === 0 || containsTokenPrefix(target.value))
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
const words = redirectIndex === -1 ? parsed : parsed.slice(0, redirectIndex);
|
|
138
|
+
const commandIndex = resolveExecutedCommandIndex(words);
|
|
139
|
+
if (commandIndex === -1)
|
|
140
|
+
return null;
|
|
141
|
+
const commandName = words[commandIndex].value.split("/").at(-1)?.toLowerCase() ?? "";
|
|
142
|
+
if (SHELL_COMMAND_WRAPPERS.has(commandName) || SCRIPT_ARGUMENT_INTERPRETERS.has(commandName))
|
|
143
|
+
return null;
|
|
144
|
+
const argumentsOnly = words.slice(commandIndex + 1);
|
|
145
|
+
if (SHELL_INTERPRETERS.has(commandName)) {
|
|
146
|
+
if (argumentsOnly.some((argument) => /^-[A-Za-z]*c[A-Za-z]*$/.test(argument.value)))
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
const inlineFlags = INLINE_SCRIPT_FLAGS.get(commandName) ??
|
|
150
|
+
(/^python(?:\d+(?:\.\d+)*)?$/.test(commandName) ? INLINE_SCRIPT_FLAGS.get("python") : undefined);
|
|
151
|
+
if (inlineFlags && argumentsOnly.some((argument) => inlineFlags.has(argument.value.toLowerCase())))
|
|
152
|
+
return null;
|
|
153
|
+
if (commandName === "deno" && argumentsOnly.some((argument) => argument.value === "eval"))
|
|
154
|
+
return null;
|
|
155
|
+
return argumentsOnly;
|
|
156
|
+
}
|
|
157
|
+
/** Characters forbidden inside restored shell argument words. */
|
|
158
|
+
export const SHELL_UNSAFE = /[\s'"`\\$;|&<>(){}*?\[\]!~#\x00-\x1F\x7F]/;
|
|
159
|
+
/**
|
|
160
|
+
* Restore policy is decided per field, not per tool. A tool name that is in
|
|
161
|
+
* SHELL_TOOLS or has a shell-interpreter segment (bash, shell, sh, zsh, pwsh,
|
|
162
|
+
* powershell) makes every field "shell". Otherwise a field named like a
|
|
163
|
+
* command (command, cmd, commands, argv, ...) is "shell", a field named like
|
|
164
|
+
* code (code, script, ...) is "inert" (no parsing; originals with
|
|
165
|
+
* shell-unsafe characters are refused), and any other field restores
|
|
166
|
+
* verbatim. The list is built into the source, not configurable; an executing
|
|
167
|
+
* tool using other field names is not detected.
|
|
168
|
+
*/
|
|
169
|
+
export const SHELL_TOOLS = new Set([
|
|
170
|
+
"bash",
|
|
171
|
+
"shell",
|
|
172
|
+
"sh",
|
|
173
|
+
"zsh",
|
|
174
|
+
"exec",
|
|
175
|
+
"command",
|
|
176
|
+
"run",
|
|
177
|
+
"pwsh",
|
|
178
|
+
"powershell",
|
|
179
|
+
]);
|
|
180
|
+
/** Splits on non-alphanumerics and camelCase boundaries; lowercase segments. */
|
|
181
|
+
export const nameSegments = (raw) => raw
|
|
182
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
183
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
|
|
184
|
+
.split(/[^A-Za-z0-9]+/)
|
|
185
|
+
.filter((segment) => segment.length > 0)
|
|
186
|
+
.map((segment) => segment.toLowerCase());
|
|
187
|
+
export const SHELL_TOOL_SEGMENTS = new Set(["bash", "shell", "sh", "zsh", "pwsh", "powershell"]);
|
|
188
|
+
export const METADATA_KEYS = new Set(["description", "title", "label"]);
|
|
189
|
+
export const SHELL_KEY_LAST_SEGMENTS = new Set(["command", "commands", "cmd", "cmds"]);
|
|
190
|
+
export const SHELL_KEYS_JOINED = new Set(["shell", "commandline", "cmdline", "argv"]);
|
|
191
|
+
export const CODE_KEYS_JOINED = new Set(["code", "script", "scripts", "sourcecode", "codesnippet", "inlinescript"]);
|
|
192
|
+
export const isShellTool = (rawTool) => SHELL_TOOLS.has(rawTool.toLowerCase()) || nameSegments(rawTool).some((segment) => SHELL_TOOL_SEGMENTS.has(segment));
|
|
193
|
+
export const isShellKey = (key) => {
|
|
194
|
+
const segments = nameSegments(key);
|
|
195
|
+
return ((segments.length > 0 && SHELL_KEY_LAST_SEGMENTS.has(segments[segments.length - 1])) ||
|
|
196
|
+
SHELL_KEYS_JOINED.has(segments.join("")));
|
|
197
|
+
};
|
|
198
|
+
export const isCodeKey = (key) => CODE_KEYS_JOINED.has(nameSegments(key).join(""));
|
|
199
|
+
export const keyPolicy = (key, inherited) => {
|
|
200
|
+
const segments = nameSegments(key);
|
|
201
|
+
if (segments.length === 1 && METADATA_KEYS.has(segments[0]))
|
|
202
|
+
return "verbatim";
|
|
203
|
+
if (isShellKey(key))
|
|
204
|
+
return "shell";
|
|
205
|
+
if (isCodeKey(key))
|
|
206
|
+
return inherited === "shell" ? "shell" : "inert";
|
|
207
|
+
return inherited;
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* Tools whose calls are observably read-only: an unresolved placeholder in
|
|
211
|
+
* their arguments reads/greps/lists against a literal string and matches
|
|
212
|
+
* nothing — no state change, no outbound transmission. Only these qualify
|
|
213
|
+
* for P1 (2026-10-06b) best-effort literal passthrough when restoration is
|
|
214
|
+
* incomplete. Everything else — writes, deletes, edits, network calls,
|
|
215
|
+
* shell, unknown tools — refuses the affected invocation.
|
|
216
|
+
*/
|
|
217
|
+
export const READ_ONLY_TOOLS = new Set([
|
|
218
|
+
"read",
|
|
219
|
+
"read_file",
|
|
220
|
+
"view",
|
|
221
|
+
"grep",
|
|
222
|
+
"glob",
|
|
223
|
+
"list",
|
|
224
|
+
"ls",
|
|
225
|
+
"find",
|
|
226
|
+
"todoread",
|
|
227
|
+
]);
|
|
228
|
+
/**
|
|
229
|
+
* Content-free degradation notice for the host log: one WARN line, a fixed
|
|
230
|
+
* context word plus a count, never request content. No side files, no
|
|
231
|
+
* provider-body notices (P1, 2026-10-06b).
|
|
232
|
+
*/
|
|
233
|
+
export const warnDegradation = (context) => {
|
|
234
|
+
console.warn(`[leakshield] degraded: ${context}`);
|
|
235
|
+
};
|
|
236
|
+
/** Preserve the existing quote context; never add quoting or reinterpret the
|
|
237
|
+
* original as code. Leading '-' stays forbidden even when quoted (options are
|
|
238
|
+
* interpreted after quote removal). Controls are forbidden in every context. */
|
|
239
|
+
export function isShellSafeOriginal(text, quote) {
|
|
240
|
+
if (text.length === 0 || text.startsWith("-") || /[\x00-\x1f\x7f]/.test(text))
|
|
241
|
+
return false;
|
|
242
|
+
if (quote === "single")
|
|
243
|
+
return !text.includes("'");
|
|
244
|
+
if (quote === "double")
|
|
245
|
+
return !/["`\\$!]/.test(text);
|
|
246
|
+
return !SHELL_UNSAFE.test(text);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* HTTP clients that take credentials inside an argument (`-H 'x-api-key: T'`,
|
|
250
|
+
* `-u user:T`, `--header=x:T`). `wget` is deliberately not listed: its `-e`
|
|
251
|
+
* option family keeps producing bypasses, so it follows the whole-word rule.
|
|
252
|
+
* `isShellSafeOriginal` only covers the first
|
|
253
|
+
* shell parse; commands such as `sh -c`, `python -c`, `ssh` and `eval` parse
|
|
254
|
+
* their arguments again, so embedding is decided by the command that actually
|
|
255
|
+
* executes: the first word, unquoted, exactly one of these names. Paths,
|
|
256
|
+
* quoted names, assignments and wrappers (`env`, `sudo`) do not qualify.
|
|
257
|
+
* `rtk curl ...` does not qualify either: rtk records command lines in its
|
|
258
|
+
* local history database in plaintext, so a restored secret would persist
|
|
259
|
+
* there. Under `rtk` only whole-word placeholders restore.
|
|
260
|
+
*/
|
|
261
|
+
export const EMBEDDED_PLACEHOLDER_COMMANDS = new Set(["curl", "http"]);
|
|
262
|
+
export function commandAllowsEmbeddedTokens(text) {
|
|
263
|
+
const first = parseLiteralShellWords(text)?.[0];
|
|
264
|
+
return (first !== undefined &&
|
|
265
|
+
first.quote === "none" &&
|
|
266
|
+
!first.redirect &&
|
|
267
|
+
EMBEDDED_PLACEHOLDER_COMMANDS.has(first.value));
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* `wget -e CMD` / `--execute CMD` hands its value to wgetrc parsing, a second
|
|
271
|
+
* parse that `isShellSafeOriginal` does not cover (the value can set
|
|
272
|
+
* `output_document`, `post_file` and the like). This check stays although
|
|
273
|
+
* `wget` no longer holds embedded tokens: `wget -e T u` would otherwise
|
|
274
|
+
* restore a whole-word token into wgetrc parsing. The command must be
|
|
275
|
+
* `wget` however it's spelled (a path, quotes, leading assignments), resolved
|
|
276
|
+
* as in `parseSupportedShellArguments`. `curl -e` is a referer and
|
|
277
|
+
* `curl -K`/`--config` only name a file.
|
|
278
|
+
* GNU long options accept unique abbreviations (`--exec`), so any prefix of
|
|
279
|
+
* `--execute` counts; the shorter ambiguous ones make wget exit with an error.
|
|
280
|
+
*/
|
|
281
|
+
export function isWgetExecuteCommand(text) {
|
|
282
|
+
const words = parseLiteralShellWords(text);
|
|
283
|
+
if (words === null)
|
|
284
|
+
return false;
|
|
285
|
+
const commandIndex = resolveExecutedCommandIndex(words);
|
|
286
|
+
if (commandIndex === -1)
|
|
287
|
+
return false;
|
|
288
|
+
if (words[commandIndex].value.split("/").at(-1)?.toLowerCase() !== "wget")
|
|
289
|
+
return false;
|
|
290
|
+
return words.slice(commandIndex + 1).some((word) => {
|
|
291
|
+
if (word.redirect)
|
|
292
|
+
return false;
|
|
293
|
+
const optionName = word.value.split("=", 1)[0];
|
|
294
|
+
return /^-[A-Za-z0-9]*e/.test(word.value) || (optionName.length >= 3 && "--execute".startsWith(optionName));
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
/** Matches a fully formed reserved token (either version), for per-token shell safety checks. */
|
|
298
|
+
export const TOKEN_SCAN = /\[SGP1:[0-9a-f]{32}-\d{8,12}\]|\[SGP2:[A-Z][A-Z_]{0,31}-[a-z]{4}-[a-z]{4}-[a-z]{4}\]/g;
|
|
299
|
+
/**
|
|
300
|
+
* Relaxed (brackets optional, separators up to 4 non-alphanumerics, any case)
|
|
301
|
+
* forms of the SGP2 placeholder, used only to spot a placeholder the
|
|
302
|
+
* model reformatted. Every quantifier is bounded and there is no nested
|
|
303
|
+
* unbounded group. A candidate counts only if its canonical form restores in
|
|
304
|
+
* the session, so no token enumeration is needed.
|
|
305
|
+
*
|
|
306
|
+
* SGP2 reads the letters after the `SGP2` head, tolerating separator runs of
|
|
307
|
+
* up to 4 non-alphanumerics between letters (changed separators) or none at
|
|
308
|
+
* all (removed separators), and stopping at a digit, a longer run or about 80
|
|
309
|
+
* letters. Each known category, compared without underscores, is tried as a
|
|
310
|
+
* prefix of those letters; the next 12 letters become the three segments. The
|
|
311
|
+
* text after a stripped token is often more short words, which this ignores
|
|
312
|
+
* because only exactly 12 letters follow the category.
|
|
313
|
+
*/
|
|
314
|
+
export const NEAR_MISS_V2_HEAD = /(?<![A-Za-z0-9])SGP2/gi;
|
|
315
|
+
export const NEAR_MISS_V2_MAX_SEPARATOR = 4;
|
|
316
|
+
export const NEAR_MISS_V2_MAX_LETTERS = 80;
|
|
317
|
+
export const NEAR_MISS_V2_SEGMENT_LETTERS = 12;
|
|
318
|
+
export const NEAR_MISS_V2_CATEGORIES = new Map(FINDING_TYPES.map((label) => [label.replaceAll("_", "").toUpperCase(), label]));
|
|
319
|
+
/** Letters following `from`, skipping short separator runs; stops at a digit or a long run. */
|
|
320
|
+
export function collectNearMissLetters(text, from) {
|
|
321
|
+
let letters = "";
|
|
322
|
+
let at = from;
|
|
323
|
+
while (at < text.length && letters.length < NEAR_MISS_V2_MAX_LETTERS) {
|
|
324
|
+
const char = text[at];
|
|
325
|
+
if (/[A-Za-z]/.test(char)) {
|
|
326
|
+
letters += char;
|
|
327
|
+
at += 1;
|
|
328
|
+
}
|
|
329
|
+
else if (/[0-9]/.test(char)) {
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
let run = 0;
|
|
334
|
+
while (at + run < text.length && /[^A-Za-z0-9]/.test(text[at + run]))
|
|
335
|
+
run += 1;
|
|
336
|
+
if (run > NEAR_MISS_V2_MAX_SEPARATOR)
|
|
337
|
+
break;
|
|
338
|
+
at += run;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return letters;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Builds the position check for the placeholders of one code/script field.
|
|
345
|
+
* Call the result with each token's [start, end) in increasing order; it keeps
|
|
346
|
+
* its state between calls, so the whole field costs one forward pass however
|
|
347
|
+
* many tokens it holds, and a false result ends the field's check. A token's
|
|
348
|
+
* position passes unless `crossesLineBoundary` or `isInPlainStringLiteral`
|
|
349
|
+
* rejects it.
|
|
350
|
+
*
|
|
351
|
+
* Conservative, language-agnostic, line-based heuristic; it is not a parser
|
|
352
|
+
* and does not confirm the match is in a string. It can both over-refuse and
|
|
353
|
+
* miss. It guards the restoration context only and is not egress protection:
|
|
354
|
+
* code can still print, send or re-interpret the restored value.
|
|
355
|
+
*/
|
|
356
|
+
export function createInertPositionCheck(text) {
|
|
357
|
+
const lineBreak = /[\n\r\u2028\u2029]/g;
|
|
358
|
+
// Current line is [lineStart, lineEnd); lineEnd sits on its separator.
|
|
359
|
+
let lineStart = 0;
|
|
360
|
+
let lineEnd = -1;
|
|
361
|
+
// Triple quotes in text[0, countedTo). A separator ends any run of quotes, so
|
|
362
|
+
// per-line counts add up to the non-overlapping count over the whole prefix.
|
|
363
|
+
let countedTo = 0;
|
|
364
|
+
let doubleTriples = 0;
|
|
365
|
+
let singleTriples = 0;
|
|
366
|
+
let crossesLineBoundary = false;
|
|
367
|
+
// Quote scan of the current line, resumed from `cursor` for each token.
|
|
368
|
+
let cursor = 0;
|
|
369
|
+
let quote = null;
|
|
370
|
+
let introducerInString = false;
|
|
371
|
+
let closingQuote = -1;
|
|
372
|
+
const enterLineOf = (index) => {
|
|
373
|
+
while (lineEnd < index) {
|
|
374
|
+
lineStart = lineEnd + 1;
|
|
375
|
+
lineBreak.lastIndex = lineStart;
|
|
376
|
+
lineEnd = lineBreak.exec(text)?.index ?? text.length;
|
|
377
|
+
}
|
|
378
|
+
const uncounted = text.slice(countedTo, lineStart);
|
|
379
|
+
doubleTriples += uncounted.match(/"""/g)?.length ?? 0;
|
|
380
|
+
singleTriples += uncounted.match(/'''/g)?.length ?? 0;
|
|
381
|
+
countedTo = lineStart;
|
|
382
|
+
const line = text.slice(lineStart, lineEnd);
|
|
383
|
+
// True when the line may belong to a multi-line construct, so the
|
|
384
|
+
// line-based string check cannot be trusted: the line holds a triple quote,
|
|
385
|
+
// an odd number of non-overlapping triple quotes precedes the line (inside
|
|
386
|
+
// an open multi-line string), or the previous line ends in a backslash
|
|
387
|
+
// (continuation; doubled backslashes are not distinguished, so refuse).
|
|
388
|
+
// Deliberately not a whole-field ban, so a script with a balanced docstring
|
|
389
|
+
// above the assignment still works.
|
|
390
|
+
crossesLineBoundary =
|
|
391
|
+
line.includes('"""') ||
|
|
392
|
+
line.includes("'''") ||
|
|
393
|
+
doubleTriples % 2 === 1 ||
|
|
394
|
+
singleTriples % 2 === 1 ||
|
|
395
|
+
/\\(?:\r\n|[\n\r\u2028\u2029])$/.test(text.slice(Math.max(0, lineStart - 3), lineStart));
|
|
396
|
+
cursor = lineStart;
|
|
397
|
+
quote = null;
|
|
398
|
+
introducerInString = false;
|
|
399
|
+
};
|
|
400
|
+
/**
|
|
401
|
+
* True when [start, end) looks like it sits inside an ordinary quoted string
|
|
402
|
+
* that opens and closes on the current line, with no interpolation or escape
|
|
403
|
+
* introducer between the opening quote and the token.
|
|
404
|
+
*/
|
|
405
|
+
const isInPlainStringLiteral = (start, end) => {
|
|
406
|
+
while (cursor < start) {
|
|
407
|
+
const c = text[cursor];
|
|
408
|
+
if (quote === null) {
|
|
409
|
+
if (c === "'" || c === '"') {
|
|
410
|
+
quote = c;
|
|
411
|
+
introducerInString = false;
|
|
412
|
+
}
|
|
413
|
+
else if (c === "`" || c === "#" || text.startsWith("//", cursor) || text.startsWith("/*", cursor) || text.startsWith("--", cursor)) {
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
else if (c === "\\") {
|
|
418
|
+
introducerInString = true;
|
|
419
|
+
cursor++;
|
|
420
|
+
}
|
|
421
|
+
else if (c === quote) {
|
|
422
|
+
quote = null;
|
|
423
|
+
}
|
|
424
|
+
else if (c === "{" || c === "$" || c === "`") {
|
|
425
|
+
introducerInString = true;
|
|
426
|
+
}
|
|
427
|
+
cursor++;
|
|
428
|
+
}
|
|
429
|
+
if (quote === null || introducerInString)
|
|
430
|
+
return false;
|
|
431
|
+
// A token holds no quote or backslash, so a search from an earlier token
|
|
432
|
+
// in this string walked through this one: its answer still holds, and each
|
|
433
|
+
// string on the line is searched once.
|
|
434
|
+
if (closingQuote >= end)
|
|
435
|
+
return true;
|
|
436
|
+
for (let i = end; i < lineEnd; i++) {
|
|
437
|
+
if (text[i] === "\\") {
|
|
438
|
+
i++;
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
if (text[i] === quote) {
|
|
442
|
+
closingQuote = i;
|
|
443
|
+
return true;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
return false;
|
|
447
|
+
};
|
|
448
|
+
return (start, end) => {
|
|
449
|
+
if (start > lineEnd)
|
|
450
|
+
enterLineOf(start);
|
|
451
|
+
return !crossesLineBoundary && isInPlainStringLiteral(start, end);
|
|
452
|
+
};
|
|
453
|
+
}
|