@vite-hub/shell 0.0.0 → 0.0.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/README.md +54 -1
- package/dist/analyze-Bu5Ueqph.js +126 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +3 -0
- package/dist/just-bash-D84udCFy.d.ts +27 -0
- package/dist/parse-D9VT4P6N.js +41 -0
- package/dist/providers/cloudflare.d.ts +28 -0
- package/dist/providers/cloudflare.js +44 -0
- package/dist/providers/just-bash.d.ts +2 -0
- package/dist/providers/just-bash.js +277 -0
- package/dist/session-0OrrlmE9.js +178 -0
- package/dist/types-CNRdehmi.d.ts +101 -0
- package/dist/workspace.d.ts +80 -0
- package/dist/workspace.js +896 -0
- package/package.json +50 -5
package/README.md
CHANGED
|
@@ -1,3 +1,56 @@
|
|
|
1
1
|
# @vite-hub/shell
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://vitehub.dev"><img alt="ViteHub" src="https://img.shields.io/badge/ViteHub-vitehub.dev-646cff?style=flat-square"></a>
|
|
5
|
+
<img alt="TypeScript" src="https://img.shields.io/badge/TypeScript-ready-3178c6?style=flat-square">
|
|
6
|
+
<img alt="Shell" src="https://img.shields.io/badge/Shell-controlled%20runtime-18181b?style=flat-square">
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
`@vite-hub/shell` gives agents and workspace tools structured command execution with explicit boundaries.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
pnpm add @vite-hub/shell
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Minimal API
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// server/utils/shell.ts
|
|
21
|
+
import { analyzeShellCommand, createShellRuntime } from "@vite-hub/shell"
|
|
22
|
+
import type { ShellExecutionProvider } from "@vite-hub/shell"
|
|
23
|
+
|
|
24
|
+
const provider: ShellExecutionProvider = {
|
|
25
|
+
boundary: {
|
|
26
|
+
cwd: true,
|
|
27
|
+
env: true,
|
|
28
|
+
filesystem: { writable: false },
|
|
29
|
+
network: false,
|
|
30
|
+
processes: { background: false, interactive: false },
|
|
31
|
+
streaming: false,
|
|
32
|
+
timeout: { enforcedBy: "runtime", supported: true },
|
|
33
|
+
},
|
|
34
|
+
async exec(command) {
|
|
35
|
+
return {
|
|
36
|
+
command,
|
|
37
|
+
event: "command_finished",
|
|
38
|
+
exitCode: 0,
|
|
39
|
+
stderr: "",
|
|
40
|
+
stdout: "ok\n",
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const runtime = createShellRuntime({ provider, policy: { maxShellCalls: 5 } })
|
|
46
|
+
const analysis = await analyzeShellCommand("echo ok")
|
|
47
|
+
const result = analysis.ok ? await runtime.exec("echo ok") : undefined
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Used by
|
|
51
|
+
|
|
52
|
+
`workspaceShell()` in [`@vite-hub/agent`](../agent/README.md) uses this package to expose scoped shell work to an agent. Workspace adapters live in `@vite-hub/shell/workspace`.
|
|
53
|
+
|
|
54
|
+
Built on [just-bash](https://www.npmjs.com/package/just-bash) for the built-in shell provider and [sh-syntax](https://www.npmjs.com/package/sh-syntax) for command analysis.
|
|
55
|
+
|
|
56
|
+
Learn more at [vitehub.dev](https://vitehub.dev).
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
//#region src/command/analyze.ts
|
|
2
|
+
const defaultMaxInputBytes = 64 * 1024;
|
|
3
|
+
const defaultTimeoutMs = 100;
|
|
4
|
+
async function analyzeShellCommand(command, options = {}) {
|
|
5
|
+
const maxInputBytes = options.maxInputBytes ?? defaultMaxInputBytes;
|
|
6
|
+
if (new TextEncoder().encode(command).byteLength > maxInputBytes) return {
|
|
7
|
+
error: `Shell command exceeds ${maxInputBytes} bytes.`,
|
|
8
|
+
ok: false,
|
|
9
|
+
parser: "sh-syntax"
|
|
10
|
+
};
|
|
11
|
+
try {
|
|
12
|
+
await withTimeout(parseWithShSyntax(command), options.timeoutMs ?? defaultTimeoutMs);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
return {
|
|
15
|
+
commands: detectCommandNames(command),
|
|
16
|
+
error: error instanceof Error ? error.message : String(error),
|
|
17
|
+
ok: false,
|
|
18
|
+
parser: "sh-syntax"
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
commands: detectCommandNames(command),
|
|
23
|
+
hasCommandSubstitution: /(?:\$\(|`)/.test(command),
|
|
24
|
+
hasHeredocs: /<<-?/.test(command),
|
|
25
|
+
hasPipelines: hasUnquoted(command, "|"),
|
|
26
|
+
hasRedirects: /(?:^|[^<])(?:>>?|<)/.test(command),
|
|
27
|
+
ok: true,
|
|
28
|
+
parser: "sh-syntax"
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async function parseWithShSyntax(command) {
|
|
32
|
+
const { parse, LangVariant } = await import("sh-syntax");
|
|
33
|
+
await parse(command, { variant: LangVariant.LangBash });
|
|
34
|
+
}
|
|
35
|
+
async function withTimeout(promise, timeoutMs) {
|
|
36
|
+
if (timeoutMs <= 0) throw new Error(`Shell analysis timed out after ${timeoutMs}ms.`);
|
|
37
|
+
let timeout;
|
|
38
|
+
try {
|
|
39
|
+
return await Promise.race([promise, new Promise((_, reject) => {
|
|
40
|
+
timeout = setTimeout(() => reject(/* @__PURE__ */ new Error(`Shell analysis timed out after ${timeoutMs}ms.`)), timeoutMs);
|
|
41
|
+
})]);
|
|
42
|
+
} finally {
|
|
43
|
+
if (timeout) clearTimeout(timeout);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function detectCommandNames(command) {
|
|
47
|
+
const names = [];
|
|
48
|
+
for (const segment of splitShellCommandSegments(command)) {
|
|
49
|
+
const name = firstCommandWord(segment);
|
|
50
|
+
if (name && !names.includes(name)) names.push(name);
|
|
51
|
+
}
|
|
52
|
+
return names;
|
|
53
|
+
}
|
|
54
|
+
function splitShellCommandSegments(command) {
|
|
55
|
+
const segments = [];
|
|
56
|
+
let current = "";
|
|
57
|
+
let quote;
|
|
58
|
+
let escaped = false;
|
|
59
|
+
for (let index = 0; index < command.length; index++) {
|
|
60
|
+
const char = command[index];
|
|
61
|
+
const next = command[index + 1];
|
|
62
|
+
if (escaped) {
|
|
63
|
+
current += char;
|
|
64
|
+
escaped = false;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (char === "\\") {
|
|
68
|
+
current += char;
|
|
69
|
+
escaped = true;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (quote) {
|
|
73
|
+
if (char === quote) quote = void 0;
|
|
74
|
+
current += char;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (char === "'" || char === "\"") {
|
|
78
|
+
quote = char;
|
|
79
|
+
current += char;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (char === "|" || char === ";" || char === "\n" || char === "&" && next === "&" || char === "|" && next === "|") {
|
|
83
|
+
segments.push(current);
|
|
84
|
+
current = "";
|
|
85
|
+
if ((char === "&" || char === "|") && next === char) index += 1;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
current += char;
|
|
89
|
+
}
|
|
90
|
+
segments.push(current);
|
|
91
|
+
return segments;
|
|
92
|
+
}
|
|
93
|
+
function firstCommandWord(segment) {
|
|
94
|
+
const words = segment.trim().match(/[^\s]+/g) || [];
|
|
95
|
+
for (const word of words) {
|
|
96
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(word)) continue;
|
|
97
|
+
if (/^\d*(?:>>?|<<?-?|<&|>&)/.test(word)) continue;
|
|
98
|
+
return word.replace(/^command$/, "") || void 0;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function hasUnquoted(command, target) {
|
|
102
|
+
let quote;
|
|
103
|
+
let escaped = false;
|
|
104
|
+
for (const char of command) {
|
|
105
|
+
if (escaped) {
|
|
106
|
+
escaped = false;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (char === "\\") {
|
|
110
|
+
escaped = true;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (quote) {
|
|
114
|
+
if (char === quote) quote = void 0;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (char === "'" || char === "\"") {
|
|
118
|
+
quote = char;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (char === target) return true;
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
export { analyzeShellCommand as t };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { a as ShellBoundary, c as ShellObservationEvent, d as ShellRuntimeExecOptions, f as ShellSession, i as ShellAnalyzeResult, l as ShellProcess, n as CreateShellSessionOptions, o as ShellExecutionProvider, p as ShellSessionPolicy, r as ShellAnalyzeOptions, s as ShellObservation, t as CreateShellRuntimeOptions, u as ShellRuntime } from "./types-CNRdehmi.js";
|
|
2
|
+
|
|
3
|
+
//#region src/command/analyze.d.ts
|
|
4
|
+
declare function analyzeShellCommand(command: string, options?: ShellAnalyzeOptions): Promise<ShellAnalyzeResult>;
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region src/runtime/session.d.ts
|
|
7
|
+
declare function createShellRuntime(options: CreateShellRuntimeOptions): ShellRuntime;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { type CreateShellRuntimeOptions, type CreateShellSessionOptions, type ShellAnalyzeOptions, type ShellAnalyzeResult, type ShellBoundary, type ShellExecutionProvider, type ShellObservation, type ShellObservationEvent, type ShellProcess, type ShellRuntime, type ShellRuntimeExecOptions, type ShellSession, type ShellSessionPolicy, analyzeShellCommand, createShellRuntime };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { o as ShellExecutionProvider } from "./types-CNRdehmi.js";
|
|
2
|
+
import { IFileSystem } from "just-bash";
|
|
3
|
+
|
|
4
|
+
//#region src/providers/just-bash.d.ts
|
|
5
|
+
interface JustBashFileSystem extends IFileSystem {
|
|
6
|
+
readonly writeFs: boolean;
|
|
7
|
+
}
|
|
8
|
+
interface JustBashProviderOptions {
|
|
9
|
+
commands?: string[];
|
|
10
|
+
cwd?: string;
|
|
11
|
+
fs: JustBashFileSystem;
|
|
12
|
+
networkGrants?: ShellNetworkGrantExecutor;
|
|
13
|
+
}
|
|
14
|
+
interface ShellNetworkGrantExecutor {
|
|
15
|
+
executeSourceRequest(input: ShellNetworkRequest): Promise<ShellNetworkRequestResult>;
|
|
16
|
+
}
|
|
17
|
+
interface ShellNetworkRequest {
|
|
18
|
+
body?: unknown;
|
|
19
|
+
method: "GET" | "HEAD" | "POST";
|
|
20
|
+
url: string;
|
|
21
|
+
}
|
|
22
|
+
interface ShellNetworkRequestResult {
|
|
23
|
+
content: string | Uint8Array;
|
|
24
|
+
}
|
|
25
|
+
declare function createJustBashProvider(options: JustBashProviderOptions): ShellExecutionProvider;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { ShellNetworkRequestResult as a, ShellNetworkRequest as i, JustBashProviderOptions as n, createJustBashProvider as o, ShellNetworkGrantExecutor as r, JustBashFileSystem as t };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//#region src/command/parse.ts
|
|
2
|
+
function parseShellCommand(command) {
|
|
3
|
+
const words = [];
|
|
4
|
+
let current = "";
|
|
5
|
+
let quote;
|
|
6
|
+
let escaped = false;
|
|
7
|
+
for (const char of command) {
|
|
8
|
+
if (escaped) {
|
|
9
|
+
current += char;
|
|
10
|
+
escaped = false;
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
if (char === "\\") {
|
|
14
|
+
escaped = true;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (quote) {
|
|
18
|
+
if (char === quote) quote = void 0;
|
|
19
|
+
else current += char;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (char === "'" || char === "\"") {
|
|
23
|
+
quote = char;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (/\s/.test(char)) {
|
|
27
|
+
if (current) {
|
|
28
|
+
words.push(current);
|
|
29
|
+
current = "";
|
|
30
|
+
}
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
current += char;
|
|
34
|
+
}
|
|
35
|
+
if (escaped) current += "\\";
|
|
36
|
+
if (quote) throw new Error("unterminated quote");
|
|
37
|
+
if (current) words.push(current);
|
|
38
|
+
return words;
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
export { parseShellCommand as t };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { o as ShellExecutionProvider } from "../types-CNRdehmi.js";
|
|
2
|
+
|
|
3
|
+
//#region src/providers/cloudflare.d.ts
|
|
4
|
+
interface CloudflareShellClient {
|
|
5
|
+
exec: (command: string, args?: string[], options?: {
|
|
6
|
+
cwd?: string;
|
|
7
|
+
env?: Record<string, string>;
|
|
8
|
+
onStderr?: (data: string) => void;
|
|
9
|
+
onStdout?: (data: string) => void;
|
|
10
|
+
stdin?: string;
|
|
11
|
+
timeout?: number;
|
|
12
|
+
}) => Promise<{
|
|
13
|
+
code?: number | null;
|
|
14
|
+
exitCode?: number | null;
|
|
15
|
+
stderr: string;
|
|
16
|
+
stdout: string;
|
|
17
|
+
}>;
|
|
18
|
+
supports: {
|
|
19
|
+
execCwd: boolean;
|
|
20
|
+
execEnv: boolean;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
interface CloudflareShellProviderOptions {
|
|
24
|
+
sandbox: CloudflareShellClient;
|
|
25
|
+
}
|
|
26
|
+
declare function createCloudflareShellProvider(options: CloudflareShellProviderOptions): ShellExecutionProvider;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { CloudflareShellClient, CloudflareShellProviderOptions, createCloudflareShellProvider };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { t as analyzeShellCommand } from "../analyze-Bu5Ueqph.js";
|
|
2
|
+
import { t as parseShellCommand } from "../parse-D9VT4P6N.js";
|
|
3
|
+
//#region src/providers/cloudflare.ts
|
|
4
|
+
function createCloudflareShellProvider(options) {
|
|
5
|
+
return {
|
|
6
|
+
analyze: analyzeShellCommand,
|
|
7
|
+
boundary: {
|
|
8
|
+
cwd: options.sandbox.supports.execCwd,
|
|
9
|
+
env: options.sandbox.supports.execEnv,
|
|
10
|
+
filesystem: { writable: true },
|
|
11
|
+
network: "unknown",
|
|
12
|
+
processes: {
|
|
13
|
+
background: false,
|
|
14
|
+
interactive: false
|
|
15
|
+
},
|
|
16
|
+
streaming: true,
|
|
17
|
+
timeout: {
|
|
18
|
+
enforcedBy: "provider",
|
|
19
|
+
supported: true
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
async exec(command, execOptions = {}) {
|
|
23
|
+
const [cmd = "", ...args] = parseShellCommand(command);
|
|
24
|
+
const result = await options.sandbox.exec(cmd, args, {
|
|
25
|
+
cwd: execOptions.cwd,
|
|
26
|
+
env: execOptions.env,
|
|
27
|
+
onStderr: execOptions.onStderr,
|
|
28
|
+
onStdout: execOptions.onStdout,
|
|
29
|
+
stdin: execOptions.stdin,
|
|
30
|
+
timeout: execOptions.timeout
|
|
31
|
+
});
|
|
32
|
+
return {
|
|
33
|
+
command,
|
|
34
|
+
cwd: execOptions.cwd,
|
|
35
|
+
event: "command_finished",
|
|
36
|
+
exitCode: result.exitCode ?? result.code ?? null,
|
|
37
|
+
stderr: result.stderr,
|
|
38
|
+
stdout: result.stdout
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
export { createCloudflareShellProvider };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as ShellNetworkRequestResult, i as ShellNetworkRequest, n as JustBashProviderOptions, o as createJustBashProvider, r as ShellNetworkGrantExecutor, t as JustBashFileSystem } from "../just-bash-D84udCFy.js";
|
|
2
|
+
export { JustBashFileSystem, JustBashProviderOptions, ShellNetworkGrantExecutor, ShellNetworkRequest, ShellNetworkRequestResult, createJustBashProvider };
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { t as analyzeShellCommand } from "../analyze-Bu5Ueqph.js";
|
|
2
|
+
import { t as parseShellCommand } from "../parse-D9VT4P6N.js";
|
|
3
|
+
//#region src/providers/just-bash.ts
|
|
4
|
+
function createJustBashProvider(options) {
|
|
5
|
+
return {
|
|
6
|
+
analyze: analyzeShellCommand,
|
|
7
|
+
boundary: {
|
|
8
|
+
cwd: true,
|
|
9
|
+
env: true,
|
|
10
|
+
filesystem: {
|
|
11
|
+
mountPoint: "/workspace",
|
|
12
|
+
writable: options.fs.writeFs
|
|
13
|
+
},
|
|
14
|
+
network: Boolean(options.networkGrants),
|
|
15
|
+
processes: {
|
|
16
|
+
background: false,
|
|
17
|
+
interactive: false
|
|
18
|
+
},
|
|
19
|
+
streaming: false,
|
|
20
|
+
timeout: {
|
|
21
|
+
enforcedBy: "provider",
|
|
22
|
+
supported: true
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
async exec(command, execOptions = {}) {
|
|
26
|
+
const result = await withProviderTimeout(command, execOptions, async () => {
|
|
27
|
+
const curlResult = await runControlledCurlCommand(command, {
|
|
28
|
+
cwd: execOptions.cwd || options.cwd,
|
|
29
|
+
networkGrants: options.networkGrants
|
|
30
|
+
});
|
|
31
|
+
if (curlResult) return curlResult;
|
|
32
|
+
const { Bash } = await import("just-bash/browser");
|
|
33
|
+
const bash = new Bash({
|
|
34
|
+
commands: options.commands,
|
|
35
|
+
cwd: options.cwd,
|
|
36
|
+
fs: options.fs
|
|
37
|
+
});
|
|
38
|
+
const signal = typeof execOptions.timeout === "number" ? AbortSignal.timeout(execOptions.timeout) : void 0;
|
|
39
|
+
return await bash.exec(command, {
|
|
40
|
+
cwd: execOptions.cwd,
|
|
41
|
+
env: execOptions.env,
|
|
42
|
+
signal
|
|
43
|
+
}).then((result) => ({
|
|
44
|
+
command,
|
|
45
|
+
cwd: execOptions.cwd,
|
|
46
|
+
event: "command_finished",
|
|
47
|
+
exitCode: result.exitCode,
|
|
48
|
+
stderr: result.stderr,
|
|
49
|
+
stdout: result.stdout
|
|
50
|
+
}));
|
|
51
|
+
});
|
|
52
|
+
execOptions.onStdout?.(result.stdout);
|
|
53
|
+
execOptions.onStderr?.(result.stderr);
|
|
54
|
+
return {
|
|
55
|
+
command: result.command ?? command,
|
|
56
|
+
cwd: result.cwd ?? execOptions.cwd,
|
|
57
|
+
event: result.timedOut ? "command_timed_out" : result.event,
|
|
58
|
+
exitCode: result.exitCode,
|
|
59
|
+
stderr: result.stderr,
|
|
60
|
+
stdout: result.stdout,
|
|
61
|
+
timedOut: result.timedOut
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
async function runControlledCurlCommand(command, options) {
|
|
67
|
+
if (!mentionsCurlCommand(command)) return void 0;
|
|
68
|
+
const [parseError, parsed] = parseControlledCurlCommand(command);
|
|
69
|
+
if (parseError) return policyDeniedCurl(command, options.cwd, parseError.message);
|
|
70
|
+
if (!options.networkGrants) return policyDeniedCurl(command, options.cwd, "No API-backed Source request descriptors are visible in this workspace.");
|
|
71
|
+
try {
|
|
72
|
+
const result = await options.networkGrants.executeSourceRequest({
|
|
73
|
+
body: parsed.body,
|
|
74
|
+
method: parsed.method,
|
|
75
|
+
url: parsed.url
|
|
76
|
+
});
|
|
77
|
+
return {
|
|
78
|
+
command,
|
|
79
|
+
cwd: options.cwd,
|
|
80
|
+
event: "command_finished",
|
|
81
|
+
exitCode: 0,
|
|
82
|
+
stderr: "",
|
|
83
|
+
stdout: typeof result.content === "string" ? result.content : new TextDecoder().decode(result.content)
|
|
84
|
+
};
|
|
85
|
+
} catch (error) {
|
|
86
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
87
|
+
const httpFailure = message.includes("HTTP request failed");
|
|
88
|
+
return {
|
|
89
|
+
command,
|
|
90
|
+
cwd: options.cwd,
|
|
91
|
+
event: httpFailure ? "command_finished" : "policy_denied",
|
|
92
|
+
exitCode: httpFailure ? 22 : 126,
|
|
93
|
+
stderr: `${message}\n`,
|
|
94
|
+
stdout: ""
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function parseFailure(message) {
|
|
99
|
+
return [new Error(message), void 0];
|
|
100
|
+
}
|
|
101
|
+
function parseControlledCurlCommand(command) {
|
|
102
|
+
if (hasUnsupportedCurlShellSyntax(command)) return parseFailure("Controlled curl must be a single command without pipes, redirects, chaining, command substitution, or heredocs.");
|
|
103
|
+
let words;
|
|
104
|
+
try {
|
|
105
|
+
words = parseShellCommand(command);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
return [error instanceof Error ? error : new Error(String(error)), void 0];
|
|
108
|
+
}
|
|
109
|
+
if (words[0] !== "curl") return parseFailure("Controlled curl command must start with curl.");
|
|
110
|
+
let body;
|
|
111
|
+
let method;
|
|
112
|
+
let url;
|
|
113
|
+
try {
|
|
114
|
+
for (let index = 1; index < words.length; index += 1) {
|
|
115
|
+
const word = words[index];
|
|
116
|
+
if (word === "-s" || word === "-S" || word === "-sS" || word === "--silent" || word === "--show-error" || word === "-L" || word === "--location") continue;
|
|
117
|
+
if (word === "-X" || word === "--request") {
|
|
118
|
+
const value = words[++index];
|
|
119
|
+
if (!value) return parseFailure(`${word} requires a method.`);
|
|
120
|
+
method = parseCurlMethod(value);
|
|
121
|
+
if (!method) return parseFailure(`Unsupported curl request method: ${value}.`);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (word.startsWith("--request=")) {
|
|
125
|
+
method = parseCurlMethod(word.slice(10));
|
|
126
|
+
if (!method) return parseFailure(`Unsupported curl request method: ${word.slice(10)}.`);
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (word === "--url") {
|
|
130
|
+
const value = words[++index];
|
|
131
|
+
if (!value) return parseFailure("--url requires a URL.");
|
|
132
|
+
url = setCurlUrl(url, value);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (word.startsWith("--url=")) {
|
|
136
|
+
url = setCurlUrl(url, word.slice(6));
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (word === "--json") {
|
|
140
|
+
const value = words[++index];
|
|
141
|
+
if (!value) return parseFailure("--json requires a JSON body.");
|
|
142
|
+
const [error, parsedBody] = parseJsonCurlBody(value);
|
|
143
|
+
if (error) return [error, void 0];
|
|
144
|
+
body = parsedBody;
|
|
145
|
+
method ??= "POST";
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (word.startsWith("--json=")) {
|
|
149
|
+
const [error, parsedBody] = parseJsonCurlBody(word.slice(7));
|
|
150
|
+
if (error) return [error, void 0];
|
|
151
|
+
body = parsedBody;
|
|
152
|
+
method ??= "POST";
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (word === "-d" || word === "--data" || word === "--data-raw" || word === "--data-binary") {
|
|
156
|
+
const value = words[++index];
|
|
157
|
+
if (!value) return parseFailure(`${word} requires a body.`);
|
|
158
|
+
body = parseCurlDataBody(value);
|
|
159
|
+
method ??= "POST";
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (word.startsWith("-d") && word !== "-d") {
|
|
163
|
+
body = parseCurlDataBody(word.slice(2));
|
|
164
|
+
method ??= "POST";
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (word.startsWith("--data=") || word.startsWith("--data-raw=") || word.startsWith("--data-binary=")) {
|
|
168
|
+
body = parseCurlDataBody(word.slice(word.indexOf("=") + 1));
|
|
169
|
+
method ??= "POST";
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (word === "-H" || word === "--header" || word === "-b" || word === "--cookie" || word.startsWith("--header=") || word.startsWith("--cookie=")) return parseFailure("Controlled curl injects Source credentials itself; do not pass headers or cookies in the command.");
|
|
173
|
+
if (word.startsWith("-")) return parseFailure(`Unsupported curl flag: ${word}.`);
|
|
174
|
+
url = setCurlUrl(url, word);
|
|
175
|
+
}
|
|
176
|
+
} catch (error) {
|
|
177
|
+
return [error instanceof Error ? error : new Error(String(error)), void 0];
|
|
178
|
+
}
|
|
179
|
+
if (!url) return parseFailure("Controlled curl requires a URL.");
|
|
180
|
+
try {
|
|
181
|
+
url = new URL(url).toString();
|
|
182
|
+
} catch {
|
|
183
|
+
return parseFailure(`Controlled curl URL is invalid: ${url}.`);
|
|
184
|
+
}
|
|
185
|
+
return [null, {
|
|
186
|
+
body,
|
|
187
|
+
method: method ?? "GET",
|
|
188
|
+
url
|
|
189
|
+
}];
|
|
190
|
+
}
|
|
191
|
+
function mentionsCurlCommand(command) {
|
|
192
|
+
return parseShellCommandLenient(command)[0] === "curl";
|
|
193
|
+
}
|
|
194
|
+
function hasUnsupportedCurlShellSyntax(command) {
|
|
195
|
+
let quote;
|
|
196
|
+
let escaped = false;
|
|
197
|
+
for (let index = 0; index < command.length; index += 1) {
|
|
198
|
+
const char = command[index];
|
|
199
|
+
const next = command[index + 1];
|
|
200
|
+
if (escaped) {
|
|
201
|
+
escaped = false;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (char === "\\") {
|
|
205
|
+
escaped = true;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (quote) {
|
|
209
|
+
if (char === quote) quote = void 0;
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (char === "'" || char === "\"") {
|
|
213
|
+
quote = char;
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (char === "|" || char === ";" || char === "<" || char === ">" || char === "`" || char === "$" && next === "(") return true;
|
|
217
|
+
if ((char === "&" || char === "|") && next === char) return true;
|
|
218
|
+
}
|
|
219
|
+
return /<<-?/.test(command);
|
|
220
|
+
}
|
|
221
|
+
function parseShellCommandLenient(command) {
|
|
222
|
+
try {
|
|
223
|
+
return parseShellCommand(command);
|
|
224
|
+
} catch {
|
|
225
|
+
return command.trim().split(/\s+/).filter(Boolean);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function parseCurlDataBody(value) {
|
|
229
|
+
const [error, body] = parseJsonCurlBody(value);
|
|
230
|
+
return error ? value : body;
|
|
231
|
+
}
|
|
232
|
+
function parseCurlMethod(value) {
|
|
233
|
+
const method = value.toUpperCase();
|
|
234
|
+
return method === "GET" || method === "HEAD" || method === "POST" ? method : void 0;
|
|
235
|
+
}
|
|
236
|
+
function parseJsonCurlBody(value) {
|
|
237
|
+
try {
|
|
238
|
+
return [null, JSON.parse(value)];
|
|
239
|
+
} catch {
|
|
240
|
+
return parseFailure("--json body must be valid JSON.");
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function setCurlUrl(current, next) {
|
|
244
|
+
if (current) throw new Error("Controlled curl accepts exactly one URL.");
|
|
245
|
+
return next;
|
|
246
|
+
}
|
|
247
|
+
function policyDeniedCurl(command, cwd, message) {
|
|
248
|
+
return {
|
|
249
|
+
command,
|
|
250
|
+
cwd,
|
|
251
|
+
event: "policy_denied",
|
|
252
|
+
exitCode: 126,
|
|
253
|
+
stderr: `[vitehub] ${message}\n`,
|
|
254
|
+
stdout: ["[vitehub] Controlled curl request was not run.", "Expected a single curl command matching a visible Source Request Descriptor at `.vitehub/sources/<sourceKey>.json`."].join("\n") + "\n"
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
async function withProviderTimeout(command, options, run) {
|
|
258
|
+
if (typeof options.timeout !== "number") return await run();
|
|
259
|
+
let timeout;
|
|
260
|
+
try {
|
|
261
|
+
return await Promise.race([run(), new Promise((resolve) => {
|
|
262
|
+
timeout = setTimeout(() => resolve({
|
|
263
|
+
command,
|
|
264
|
+
cwd: options.cwd,
|
|
265
|
+
event: "command_timed_out",
|
|
266
|
+
exitCode: null,
|
|
267
|
+
stderr: `[vitehub] Workspace shell command timed out after ${options.timeout}ms.`,
|
|
268
|
+
stdout: "",
|
|
269
|
+
timedOut: true
|
|
270
|
+
}), options.timeout);
|
|
271
|
+
})]);
|
|
272
|
+
} finally {
|
|
273
|
+
if (timeout) clearTimeout(timeout);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
//#endregion
|
|
277
|
+
export { createJustBashProvider };
|