@picassohq/cli 0.0.0-stage → 1.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/agents/browser_agent.yaml +53 -0
- package/agents/code_reviewer.yaml +49 -0
- package/agents/codebase_investigator.yaml +70 -0
- package/agents/design_auditor.yaml +50 -0
- package/agents/devex_auditor.yaml +50 -0
- package/agents/doc_writer.yaml +54 -0
- package/agents/engineering_architect.yaml +51 -0
- package/agents/generalist.yaml +55 -0
- package/agents/migration_planner.yaml +51 -0
- package/agents/product_reviewer.yaml +48 -0
- package/agents/qa_operator.yaml +52 -0
- package/agents/release_engineer.yaml +50 -0
- package/agents/security_auditor.yaml +62 -0
- package/agents/test_writer.yaml +52 -0
- package/assets/first-light-lockup.png +0 -0
- package/assets/first-light-tagline.png +0 -0
- package/assets/house/README.md +26 -0
- package/assets/house/auraone-banner.png +0 -0
- package/assets/house/auraone-logo.png +0 -0
- package/assets/house/kiro-prism-banner.png +0 -0
- package/assets/house/kiro-prism-logo.png +0 -0
- package/assets/house/picasso-banner.png +0 -0
- package/assets/house/picasso-logo.png +0 -0
- package/assets/wordmark-app.png +0 -0
- package/assets/wordmark-tight.png +0 -0
- package/assets/wordmark.png +0 -0
- package/design/styles.css +1042 -0
- package/dist/build-info.json +37 -0
- package/dist/index.js +3062 -0
- package/goals/cli-hardening.yaml +40 -0
- package/goals/repo-maintenance.yaml +40 -0
- package/package.json +49 -4
- package/runtime/dsh/picasso-ecosystem.community.amendments.yml +24 -0
- package/runtime/dsh/picasso-ecosystem.community.json +185 -0
- package/runtime/dsh/picasso-ecosystem.community.patch.yml +40 -0
- package/runtime/dsh/picasso-ecosystem.cordis.patch.yml +143 -0
- package/runtime/dsh/picasso-isolation.patch.yml +25 -0
- package/runtime/dsh/picasso-providers.patch.yml +373 -0
- package/runtime/dsh/picasso-runtime.cordis.yml +738 -0
- package/runtime/dsh/picasso-sdk.patch.yml +204 -0
- package/runtime/engine-manifest.json +215 -0
- package/runtime/package.json +6 -0
- package/runtime/packages/picasso-cache-probe/cordis.patch.yml +5 -0
- package/runtime/packages/picasso-cache-probe/index.js +477 -0
- package/runtime/packages/picasso-cache-probe/package.json +38 -0
- package/runtime/packages/picasso-commands/cordis.patch.yml +4 -0
- package/runtime/packages/picasso-commands/fresh-review.js +573 -0
- package/runtime/packages/picasso-commands/index.js +497 -0
- package/runtime/packages/picasso-commands/package.json +28 -0
- package/runtime/packages/picasso-instructions/conflicts.js +115 -0
- package/runtime/packages/picasso-instructions/formats.js +145 -0
- package/runtime/packages/picasso-instructions/frontmatter.js +175 -0
- package/runtime/packages/picasso-instructions/glob.js +114 -0
- package/runtime/packages/picasso-instructions/globals.js +90 -0
- package/runtime/packages/picasso-instructions/imports.js +123 -0
- package/runtime/packages/picasso-instructions/index.js +326 -0
- package/runtime/packages/picasso-instructions/modes.js +64 -0
- package/runtime/packages/picasso-instructions/package.json +30 -0
- package/runtime/packages/picasso-instructions/parse.js +134 -0
- package/runtime/packages/picasso-instructions/report.js +161 -0
- package/runtime/packages/picasso-instructions/resolve.js +939 -0
- package/runtime/packages/picasso-instructions/scan.js +181 -0
- package/runtime/packages/picasso-instructions/settings.js +150 -0
- package/runtime/packages/picasso-instructions/skills.js +345 -0
- package/runtime/packages/picasso-lean-context/cordis.patch.yml +13 -0
- package/runtime/packages/picasso-lean-context/index.js +6280 -0
- package/runtime/packages/picasso-lean-context/package.json +26 -0
- package/runtime/packages/picasso-lean-context/read-image-recovery.js +306 -0
- package/runtime/packages/picasso-lean-context/timeline.js +418 -0
- package/runtime/packages/picasso-librarian/classify.js +146 -0
- package/runtime/packages/picasso-librarian/cordis.patch.yml +10 -0
- package/runtime/packages/picasso-librarian/index.js +878 -0
- package/runtime/packages/picasso-librarian/notes.js +159 -0
- package/runtime/packages/picasso-librarian/package.json +34 -0
- package/runtime/packages/picasso-librarian/state.js +478 -0
- package/runtime/packages/picasso-memory/index.js +1184 -0
- package/runtime/packages/picasso-memory/package.json +21 -0
- package/runtime/packages/picasso-model-router/connections.js +912 -0
- package/runtime/packages/picasso-model-router/cordis.patch.yml +17 -0
- package/runtime/packages/picasso-model-router/index.js +1083 -0
- package/runtime/packages/picasso-model-router/package.json +30 -0
- package/runtime/packages/picasso-options/cordis.patch.yml +4 -0
- package/runtime/packages/picasso-options/index.js +263 -0
- package/runtime/packages/picasso-options/package.json +28 -0
- package/runtime/packages/picasso-progress-reports/cordis.patch.yml +10 -0
- package/runtime/packages/picasso-progress-reports/index.js +267 -0
- package/runtime/packages/picasso-progress-reports/package.json +30 -0
- package/runtime/packages/picasso-progress-reports/report.js +235 -0
- package/runtime/packages/picasso-sdk-server/aside.js +142 -0
- package/runtime/packages/picasso-sdk-server/connections.js +38 -0
- package/runtime/packages/picasso-sdk-server/context-timeline.js +27 -0
- package/runtime/packages/picasso-sdk-server/egress-gate.js +520 -0
- package/runtime/packages/picasso-sdk-server/failure-policy.js +246 -0
- package/runtime/packages/picasso-sdk-server/file-input.js +169 -0
- package/runtime/packages/picasso-sdk-server/goal.js +131 -0
- package/runtime/packages/picasso-sdk-server/ignore-file.js +458 -0
- package/runtime/packages/picasso-sdk-server/ignore-plugin.js +12 -0
- package/runtime/packages/picasso-sdk-server/ignore-sandbox.js +122 -0
- package/runtime/packages/picasso-sdk-server/ignore-snapshot.js +131 -0
- package/runtime/packages/picasso-sdk-server/image-input.js +44 -0
- package/runtime/packages/picasso-sdk-server/index.js +891 -0
- package/runtime/packages/picasso-sdk-server/instructions.js +34 -0
- package/runtime/packages/picasso-sdk-server/jobs.js +113 -0
- package/runtime/packages/picasso-sdk-server/mcp-gate.js +51 -0
- package/runtime/packages/picasso-sdk-server/model-selection.js +162 -0
- package/runtime/packages/picasso-sdk-server/package.json +50 -0
- package/runtime/packages/picasso-sdk-server/picasso-records.js +50 -0
- package/runtime/packages/picasso-sdk-server/provider-reload.js +84 -0
- package/runtime/packages/picasso-sdk-server/queue.js +322 -0
- package/runtime/packages/picasso-sdk-server/requester.js +43 -0
- package/runtime/packages/picasso-sdk-server/rewind.js +108 -0
- package/runtime/packages/picasso-sdk-server/schedule.js +187 -0
- package/runtime/packages/picasso-sdk-server/session-grants.js +35 -0
- package/runtime/packages/picasso-sdk-server/session-state.js +118 -0
- package/runtime/packages/picasso-sdk-server/shell-paths.js +869 -0
- package/runtime/packages/picasso-sdk-server/stream-deltas.js +211 -0
- package/runtime/packages/picasso-sdk-server/team-tasks.js +451 -0
- package/runtime/packages/picasso-sdk-server/tool-output.js +128 -0
- package/runtime/packages/picasso-sdk-server/tools-list.js +51 -0
- package/runtime/packages/picasso-sdk-server/wave2-hooks.js +612 -0
- package/runtime/packages/picasso-sdk-server/workspace-archive.js +170 -0
- package/runtime/packages/picasso-sdk-server/workspace-restore.js +180 -0
- package/runtime/packages/picasso-web-search/backends.js +412 -0
- package/runtime/packages/picasso-web-search/config.js +121 -0
- package/runtime/packages/picasso-web-search/cordis.patch.yml +7 -0
- package/runtime/packages/picasso-web-search/failure.js +116 -0
- package/runtime/packages/picasso-web-search/format.js +42 -0
- package/runtime/packages/picasso-web-search/index.js +71 -0
- package/runtime/packages/picasso-web-search/package.json +38 -0
- package/runtime/packages/picasso-web-search/router.js +156 -0
- package/runtime/packages/picasso-x-reader/circle.js +324 -0
- package/runtime/packages/picasso-x-reader/command.js +169 -0
- package/runtime/packages/picasso-x-reader/cordis.patch.yml +8 -0
- package/runtime/packages/picasso-x-reader/index.js +170 -0
- package/runtime/packages/picasso-x-reader/package.json +39 -0
- package/runtime/packages/picasso-x-reader/read.js +370 -0
- package/runtime/packages/picasso-x-reader/rewrite.js +38 -0
- package/runtime/packages/picasso-x-reader/settings.js +131 -0
- package/runtime/packages/picasso-x-reader/urls.js +154 -0
- package/runtime/packages/picasso-x-reader/xmd.js +512 -0
- package/runtime/plugins/picasso-auth-bridge/index.js +380 -0
- package/runtime/plugins/picasso-auth-bridge/package.json +28 -0
- package/runtime/plugins/picasso-auto-approve/index.js +1003 -0
- package/runtime/plugins/picasso-auto-approve/package.json +28 -0
- package/runtime/plugins/picasso-browser/index.js +424 -0
- package/runtime/plugins/picasso-browser/package.json +28 -0
- package/runtime/plugins/picasso-checkpoints/evidence.js +210 -0
- package/runtime/plugins/picasso-checkpoints/index.js +885 -0
- package/runtime/plugins/picasso-checkpoints/package.json +29 -0
- package/runtime/plugins/picasso-commands/fresh-review.js +573 -0
- package/runtime/plugins/picasso-commands/index.js +497 -0
- package/runtime/plugins/picasso-commands/package.json +28 -0
- package/runtime/plugins/picasso-instructions/conflicts.js +115 -0
- package/runtime/plugins/picasso-instructions/formats.js +145 -0
- package/runtime/plugins/picasso-instructions/frontmatter.js +175 -0
- package/runtime/plugins/picasso-instructions/glob.js +114 -0
- package/runtime/plugins/picasso-instructions/globals.js +90 -0
- package/runtime/plugins/picasso-instructions/imports.js +123 -0
- package/runtime/plugins/picasso-instructions/index.js +326 -0
- package/runtime/plugins/picasso-instructions/modes.js +64 -0
- package/runtime/plugins/picasso-instructions/package.json +30 -0
- package/runtime/plugins/picasso-instructions/parse.js +134 -0
- package/runtime/plugins/picasso-instructions/report.js +161 -0
- package/runtime/plugins/picasso-instructions/resolve.js +939 -0
- package/runtime/plugins/picasso-instructions/scan.js +181 -0
- package/runtime/plugins/picasso-instructions/settings.js +150 -0
- package/runtime/plugins/picasso-instructions/skills.js +345 -0
- package/runtime/plugins/picasso-lean-context/index.js +6280 -0
- package/runtime/plugins/picasso-lean-context/package.json +26 -0
- package/runtime/plugins/picasso-lean-context/read-image-recovery.js +306 -0
- package/runtime/plugins/picasso-lean-context/timeline.js +418 -0
- package/runtime/plugins/picasso-librarian/classify.js +146 -0
- package/runtime/plugins/picasso-librarian/index.js +878 -0
- package/runtime/plugins/picasso-librarian/notes.js +159 -0
- package/runtime/plugins/picasso-librarian/package.json +34 -0
- package/runtime/plugins/picasso-librarian/state.js +478 -0
- package/runtime/plugins/picasso-lifecycle-hooks/index.js +186 -0
- package/runtime/plugins/picasso-lifecycle-hooks/package.json +25 -0
- package/runtime/plugins/picasso-mail/inbox.js +207 -0
- package/runtime/plugins/picasso-mail/index.js +293 -0
- package/runtime/plugins/picasso-mail/package.json +22 -0
- package/runtime/plugins/picasso-memory/index.js +1184 -0
- package/runtime/plugins/picasso-memory/package.json +21 -0
- package/runtime/plugins/picasso-model-router/connections.js +912 -0
- package/runtime/plugins/picasso-model-router/index.js +1083 -0
- package/runtime/plugins/picasso-model-router/package.json +30 -0
- package/runtime/plugins/picasso-options/index.js +263 -0
- package/runtime/plugins/picasso-options/package.json +28 -0
- package/runtime/plugins/picasso-progress-reports/index.js +267 -0
- package/runtime/plugins/picasso-progress-reports/package.json +30 -0
- package/runtime/plugins/picasso-progress-reports/report.js +235 -0
- package/runtime/plugins/picasso-provider-models/index.js +436 -0
- package/runtime/plugins/picasso-provider-models/package.json +28 -0
- package/runtime/plugins/picasso-repo-index/index.js +1850 -0
- package/runtime/plugins/picasso-repo-index/package.json +28 -0
- package/runtime/plugins/picasso-schedule-pause/index.js +344 -0
- package/runtime/plugins/picasso-schedule-pause/package.json +28 -0
- package/runtime/plugins/picasso-session-lifecycle/index.js +796 -0
- package/runtime/plugins/picasso-session-lifecycle/package.json +28 -0
- package/runtime/plugins/picasso-trackers/index.js +895 -0
- package/runtime/plugins/picasso-trackers/package.json +28 -0
- package/runtime/plugins/picasso-web-search/backends.js +412 -0
- package/runtime/plugins/picasso-web-search/config.js +121 -0
- package/runtime/plugins/picasso-web-search/failure.js +116 -0
- package/runtime/plugins/picasso-web-search/format.js +42 -0
- package/runtime/plugins/picasso-web-search/index.js +71 -0
- package/runtime/plugins/picasso-web-search/package.json +38 -0
- package/runtime/plugins/picasso-web-search/router.js +156 -0
- package/runtime/plugins/picasso-x-reader/circle.js +324 -0
- package/runtime/plugins/picasso-x-reader/command.js +169 -0
- package/runtime/plugins/picasso-x-reader/index.js +170 -0
- package/runtime/plugins/picasso-x-reader/package.json +39 -0
- package/runtime/plugins/picasso-x-reader/read.js +370 -0
- package/runtime/plugins/picasso-x-reader/rewrite.js +38 -0
- package/runtime/plugins/picasso-x-reader/settings.js +131 -0
- package/runtime/plugins/picasso-x-reader/urls.js +154 -0
- package/runtime/plugins/picasso-x-reader/xmd.js +512 -0
- package/runtime/relay/attachment-text.mjs +737 -0
- package/runtime/relay/companion/companion.css +126 -0
- package/runtime/relay/companion/companion.js +648 -0
- package/runtime/relay/companion/icon.svg +5 -0
- package/runtime/relay/companion/index.html +25 -0
- package/runtime/relay/companion/manifest.json +13 -0
- package/runtime/relay/companion/sw.js +41 -0
- package/runtime/relay/main.mjs +38580 -0
- package/runtime/relay/model-listing.mjs +614 -0
- package/runtime/relay/versions-routes.mjs +1400 -0
- package/runtime/verify-plugins.mjs +205 -0
- package/runtime-lock/package-lock.json +10779 -0
- package/runtime-lock/package.json +7 -0
- package/scripts/install-runtime.mjs +400 -0
- package/README.md +0 -3
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
// Per-session egress allowlist for sandboxed runs (upstream ask T4.9).
|
|
2
|
+
//
|
|
3
|
+
// dsh's sandbox vocabulary is file-effects only — SandboxPolicy carries
|
|
4
|
+
// { mode, workspaceRoot, sessionId } and "network and process visibility are
|
|
5
|
+
// outside this vocabulary" (dsh-sandbox/lib/types/index.d.ts), so the seam
|
|
6
|
+
// the ask names cannot be wired today. The community dsh-permission-rules
|
|
7
|
+
// plugin ships a network proxy but rewrites HTTP(S)_PROXY process-wide and
|
|
8
|
+
// binds with no authentication (dsh/picasso-ecosystem.community.json keeps
|
|
9
|
+
// network.enabled=false for exactly that reason). Picasso therefore gates
|
|
10
|
+
// egress here, client-side:
|
|
11
|
+
//
|
|
12
|
+
// sandbox/network/set { sessionId, allow: [domain…], failIfUnavailable?, ports? }
|
|
13
|
+
// sandbox/network/get { sessionId }
|
|
14
|
+
// sandbox/network/clear { sessionId }
|
|
15
|
+
//
|
|
16
|
+
// A gated session's shell commands (bash and pwsh alike — every executor
|
|
17
|
+
// registers as `ctx.shell` and every spec carries the calling session's id
|
|
18
|
+
// in `sandboxPolicy.sessionId`) are spawned with HTTP(S)_PROXY pointed at a
|
|
19
|
+
// loopback allowlist proxy this module runs once per process. The proxy
|
|
20
|
+
// authenticates each request by a per-session bearer token in the proxy
|
|
21
|
+
// URL's userinfo: an unauthenticated bind like the community plugin's would
|
|
22
|
+
// let any local process — or another session's command — ride an allowlist
|
|
23
|
+
// it was never granted.
|
|
24
|
+
//
|
|
25
|
+
// Fail-closed everywhere:
|
|
26
|
+
// * proxy unreachable + failIfUnavailable (the default) → the call is
|
|
27
|
+
// refused outright: `deny` at tools/pre-execute for model-facing tool
|
|
28
|
+
// calls, and a thrown error inside ctx.shell.execute for every other
|
|
29
|
+
// shell-spawning caller;
|
|
30
|
+
// * proxy unreachable + failIfUnavailable:false → the dead-proxy env is
|
|
31
|
+
// still injected, so proxy-honoring clients fail connection-refused
|
|
32
|
+
// rather than falling back to a direct route;
|
|
33
|
+
// * unknown domains → 403 at the proxy; unknown/revoked tokens → 407;
|
|
34
|
+
// * tools whose network cannot traverse child-process env (in-process
|
|
35
|
+
// web_fetch/web_search, the PTY terminal seam which carries no env, and
|
|
36
|
+
// run_code cells) are DENIED for gated sessions — they cannot be gated,
|
|
37
|
+
// so they cannot run while a rule is set;
|
|
38
|
+
// * `clear` revokes the session's token: still-running commands lose
|
|
39
|
+
// proxy access on their next request.
|
|
40
|
+
//
|
|
41
|
+
// Honest residual: the proxy-env contract is advisory. A spawned command can
|
|
42
|
+
// bypass it with raw sockets, `curl --noproxy`, or DNS lookups — the gate is
|
|
43
|
+
// deny-by-default routing for proxy-honoring clients plus a hard
|
|
44
|
+
// availability check, NOT a kernel network boundary. Namespaces, seccomp or
|
|
45
|
+
// firewall enforcement remains the upstream ask (docs/dsh-upstream-asks.md
|
|
46
|
+
// T4.9).
|
|
47
|
+
import { createServer, request as httpRequest } from 'node:http';
|
|
48
|
+
import { connect as netConnect } from 'node:net';
|
|
49
|
+
import { randomBytes } from 'node:crypto';
|
|
50
|
+
|
|
51
|
+
export const EGRESS_METHODS = Object.freeze(['sandbox/network/set', 'sandbox/network/get', 'sandbox/network/clear']);
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Tools whose network never traverses the child-process environment the gate
|
|
55
|
+
* writes, so a gated session must not run them (they would bypass the
|
|
56
|
+
* allowlist — fail-closed means denying them, not letting them through):
|
|
57
|
+
* in-process fetch/search ride the harness's own dispatcher, `terminal_*`
|
|
58
|
+
* PTYs spawn through `ctx.terminals` whose request carries no env, and
|
|
59
|
+
* `run_code` cells execute in worker threads deliberately excluded from
|
|
60
|
+
* proxy env.
|
|
61
|
+
*/
|
|
62
|
+
export const UNGATED_NETWORK_TOOLS = Object.freeze(new Set(['web_fetch', 'web_search', 'terminal_open', 'terminal_send', 'run_code']));
|
|
63
|
+
|
|
64
|
+
const LOOPBACK_HOST = '127.0.0.1';
|
|
65
|
+
const DEFAULT_PORTS = Object.freeze([80, 443]);
|
|
66
|
+
const PATCHED = Symbol.for('picasso.egress-gate');
|
|
67
|
+
const PROXY_ENV_VARS = ['http_proxy', 'https_proxy', 'all_proxy', 'HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY'];
|
|
68
|
+
const DOMAIN_RE = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/u;
|
|
69
|
+
const IPV4_RE = /^\d{1,3}(?:\.\d{1,3}){3}$/u;
|
|
70
|
+
const IPV6_RE = /^[0-9a-f]*:[0-9a-f:.%]+$/u;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Normalize one allowlist entry: lowercase, no scheme/path/port/userinfo, no
|
|
74
|
+
* IP literals (the allowlist is a DOMAIN policy — IP entries would silently
|
|
75
|
+
* turn it into an address ACL this seam cannot enforce). `*` and a `*.`
|
|
76
|
+
* prefix are accepted; `*.example.com` normalizes to `example.com` because an
|
|
77
|
+
* entry already covers its own subdomains.
|
|
78
|
+
*/
|
|
79
|
+
export function normalizeDomainEntry(raw) {
|
|
80
|
+
if (typeof raw !== 'string') throw new TypeError('sandbox/network/set: allow entries must be domain strings');
|
|
81
|
+
let entry = raw.trim().toLowerCase().replace(/\.+$/u, '');
|
|
82
|
+
if (entry === '*') return '*';
|
|
83
|
+
if (entry.startsWith('*.')) entry = entry.slice(2);
|
|
84
|
+
if (entry.startsWith('.')) entry = entry.slice(1);
|
|
85
|
+
if (
|
|
86
|
+
entry.length === 0 ||
|
|
87
|
+
entry.includes('/') ||
|
|
88
|
+
entry.includes(':') ||
|
|
89
|
+
entry.includes('@') ||
|
|
90
|
+
/\s/u.test(entry) ||
|
|
91
|
+
IPV4_RE.test(entry) ||
|
|
92
|
+
IPV6_RE.test(entry) ||
|
|
93
|
+
!DOMAIN_RE.test(entry)
|
|
94
|
+
) {
|
|
95
|
+
throw new TypeError(`sandbox/network/set: invalid allowlist domain ${JSON.stringify(raw)} (bare domains only, e.g. "api.github.com")`);
|
|
96
|
+
}
|
|
97
|
+
return entry;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Validate the full `allow` array; `[]` is meaningful (deny all egress through the gate). */
|
|
101
|
+
export function normalizeAllowlist(raw) {
|
|
102
|
+
if (!Array.isArray(raw)) throw new TypeError('sandbox/network/set requires an allow array of domains');
|
|
103
|
+
const normalized = [];
|
|
104
|
+
const seen = new Set();
|
|
105
|
+
for (const entry of raw) {
|
|
106
|
+
const domain = normalizeDomainEntry(entry);
|
|
107
|
+
if (seen.has(domain)) continue;
|
|
108
|
+
seen.add(domain);
|
|
109
|
+
normalized.push(domain);
|
|
110
|
+
}
|
|
111
|
+
return normalized;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Validate an optional CONNECT/forward port list (default: 80 and 443 only). */
|
|
115
|
+
export function normalizePorts(raw) {
|
|
116
|
+
if (raw === undefined) return [...DEFAULT_PORTS];
|
|
117
|
+
if (!Array.isArray(raw) || raw.length === 0) throw new TypeError('sandbox/network/set: ports must be a non-empty array of TCP ports');
|
|
118
|
+
const ports = [];
|
|
119
|
+
for (const entry of raw) {
|
|
120
|
+
if (!Number.isInteger(entry) || entry < 1 || entry > 65535) {
|
|
121
|
+
throw new TypeError(`sandbox/network/set: invalid port ${JSON.stringify(entry)}`);
|
|
122
|
+
}
|
|
123
|
+
if (!ports.includes(entry)) ports.push(entry);
|
|
124
|
+
}
|
|
125
|
+
return ports;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Whether `host` is covered by a normalized allowlist. An entry covers itself
|
|
130
|
+
* and every subdomain (`api.github.com` matches `github.com`; `evilgithub.com`
|
|
131
|
+
* does not — the boundary is the label dot, never a suffix match).
|
|
132
|
+
*/
|
|
133
|
+
export function hostAllowed(host, allow) {
|
|
134
|
+
const normalized = String(host ?? '').toLowerCase().replace(/\.+$/u, '');
|
|
135
|
+
if (normalized.length === 0) return false;
|
|
136
|
+
for (const entry of allow) {
|
|
137
|
+
if (entry === '*') return true;
|
|
138
|
+
if (normalized === entry || normalized.endsWith(`.${entry}`)) return true;
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The per-session bearer token a client presented, or undefined (Basic userinfo, password ignored). */
|
|
144
|
+
export function proxyAuthToken(headers) {
|
|
145
|
+
const header = headers?.['proxy-authorization'];
|
|
146
|
+
if (typeof header !== 'string') return undefined;
|
|
147
|
+
const match = /^basic\s+(\S+)$/iu.exec(header.trim());
|
|
148
|
+
if (match === null) return undefined;
|
|
149
|
+
let decoded;
|
|
150
|
+
try {
|
|
151
|
+
decoded = Buffer.from(match[1], 'base64').toString('utf8');
|
|
152
|
+
} catch {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
const colon = decoded.indexOf(':');
|
|
156
|
+
const token = colon < 0 ? decoded : decoded.slice(0, colon);
|
|
157
|
+
return token.length > 0 ? token : undefined;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Loopback TCP reachability check behind `isUp` (the honest proxy-down probe). */
|
|
161
|
+
export function tcpReachable(host, port, timeoutMs = 500) {
|
|
162
|
+
return new Promise((resolve) => {
|
|
163
|
+
const socket = netConnect({ host, port });
|
|
164
|
+
const settle = (ok) => {
|
|
165
|
+
socket.destroy();
|
|
166
|
+
resolve(ok);
|
|
167
|
+
};
|
|
168
|
+
socket.setTimeout(timeoutMs, () => settle(false));
|
|
169
|
+
socket.once('connect', () => settle(true));
|
|
170
|
+
socket.once('error', () => settle(false));
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Headers a proxy must never relay (RFC 9110 hop-by-hop, plus the proxy pair). */
|
|
175
|
+
const HOP_BY_HOP_HEADERS = new Set(['connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization', 'proxy-connection', 'te', 'trailer', 'transfer-encoding', 'upgrade']);
|
|
176
|
+
|
|
177
|
+
const writeDenial = (socket, status, headers = '') => {
|
|
178
|
+
try {
|
|
179
|
+
socket.end(`HTTP/1.1 ${status}\r\n${headers}Connection: close\r\nContent-Length: 0\r\n\r\n`);
|
|
180
|
+
} catch {
|
|
181
|
+
socket.destroy();
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The shared loopback allowlist proxy. One instance serves every gated
|
|
187
|
+
* session; `ruleForToken(token)` resolves the live rule a request's bearer
|
|
188
|
+
* token maps to, so allowlist edits apply to connections already in flight
|
|
189
|
+
* and `clear` revokes mid-command.
|
|
190
|
+
*
|
|
191
|
+
* Plain requests (forward proxy): absolute-form `http://host/path` only,
|
|
192
|
+
* host and port both must pass the rule. CONNECT tunnels: `host:port`, same
|
|
193
|
+
* checks — the tunnel opens only after the target is allowlisted, so the
|
|
194
|
+
* proxy never dials a denied name (and a denied name is never resolved).
|
|
195
|
+
*/
|
|
196
|
+
export function createEgressProxy({ ruleForToken, warn } = {}) {
|
|
197
|
+
if (typeof ruleForToken !== 'function') throw new TypeError('createEgressProxy requires ruleForToken');
|
|
198
|
+
const sockets = new Set();
|
|
199
|
+
const server = createServer();
|
|
200
|
+
const ruleOf = (req) => {
|
|
201
|
+
const token = proxyAuthToken(req.headers);
|
|
202
|
+
return token === undefined ? undefined : ruleForToken(token);
|
|
203
|
+
};
|
|
204
|
+
server.on('connection', (socket) => {
|
|
205
|
+
sockets.add(socket);
|
|
206
|
+
socket.on('close', () => sockets.delete(socket));
|
|
207
|
+
});
|
|
208
|
+
server.on('request', (req, res) => {
|
|
209
|
+
const rule = ruleOf(req);
|
|
210
|
+
if (rule === undefined) {
|
|
211
|
+
res.writeHead(407, { 'proxy-authenticate': 'Basic realm="picasso-egress"' });
|
|
212
|
+
res.end();
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
let url;
|
|
216
|
+
try {
|
|
217
|
+
url = new URL(req.url ?? '');
|
|
218
|
+
} catch {
|
|
219
|
+
res.writeHead(400);
|
|
220
|
+
res.end();
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const port = url.port === '' ? 80 : Number(url.port);
|
|
224
|
+
if (url.protocol !== 'http:' || !hostAllowed(url.hostname, rule.allow) || !rule.ports.includes(port)) {
|
|
225
|
+
res.writeHead(403, { 'content-type': 'text/plain' });
|
|
226
|
+
res.end(`picasso egress gate: ${url.hostname}:${port} is not in the session domain allowlist\n`);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const headers = { ...req.headers };
|
|
230
|
+
for (const name of Object.keys(headers)) {
|
|
231
|
+
if (name === 'proxy-authorization' || name === 'proxy-connection' || name === 'connection' || name === 'keep-alive' || name === 'te' || name === 'trailer' || name === 'transfer-encoding' || name === 'upgrade') {
|
|
232
|
+
delete headers[name];
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const upstream = httpRequest(
|
|
236
|
+
{ hostname: url.hostname, port, path: `${url.pathname}${url.search}`, method: req.method, headers },
|
|
237
|
+
(upRes) => {
|
|
238
|
+
// Hop-by-hop headers are not relayed: the upstream's keep-alive and
|
|
239
|
+
// transfer framing are the proxy's own to answer, and relaying them
|
|
240
|
+
// would either lie about the connection policy or double-frame the
|
|
241
|
+
// already-decoded body. `content-length` (end-to-end) survives.
|
|
242
|
+
const upHeaders = { ...upRes.headers };
|
|
243
|
+
for (const name of Object.keys(upHeaders)) {
|
|
244
|
+
if (HOP_BY_HOP_HEADERS.has(name)) delete upHeaders[name];
|
|
245
|
+
}
|
|
246
|
+
res.writeHead(upRes.statusCode ?? 502, upHeaders);
|
|
247
|
+
upRes.pipe(res);
|
|
248
|
+
},
|
|
249
|
+
);
|
|
250
|
+
upstream.on('error', (error) => {
|
|
251
|
+
warn?.(`picasso egress gate: forward to ${url.hostname}:${port} failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
252
|
+
if (!res.headersSent) res.writeHead(502);
|
|
253
|
+
res.end();
|
|
254
|
+
});
|
|
255
|
+
req.pipe(upstream);
|
|
256
|
+
});
|
|
257
|
+
server.on('connect', (req, clientSocket, head) => {
|
|
258
|
+
const rule = ruleOf(req);
|
|
259
|
+
if (rule === undefined) {
|
|
260
|
+
writeDenial(clientSocket, '407 Proxy Authentication Required', 'Proxy-Authenticate: Basic realm="picasso-egress"\r\n');
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const authority = String(req.url ?? '');
|
|
264
|
+
const split = authority.lastIndexOf(':');
|
|
265
|
+
const rawHost = split < 0 ? authority : authority.slice(0, split);
|
|
266
|
+
const host = rawHost.startsWith('[') ? rawHost.slice(1, rawHost.indexOf(']')) : rawHost;
|
|
267
|
+
const port = split < 0 ? Number.NaN : Number(authority.slice(split + 1));
|
|
268
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535 || !rule.ports.includes(port) || !hostAllowed(host, rule.allow)) {
|
|
269
|
+
writeDenial(clientSocket, '403 Forbidden');
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
const upstream = netConnect({ host, port });
|
|
273
|
+
upstream.once('connect', () => {
|
|
274
|
+
clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
|
|
275
|
+
if (head !== undefined && head.length > 0) upstream.write(head);
|
|
276
|
+
upstream.pipe(clientSocket);
|
|
277
|
+
clientSocket.pipe(upstream);
|
|
278
|
+
});
|
|
279
|
+
upstream.once('error', () => writeDenial(clientSocket, '502 Bad Gateway'));
|
|
280
|
+
clientSocket.once('error', () => upstream.destroy());
|
|
281
|
+
clientSocket.once('close', () => upstream.destroy());
|
|
282
|
+
});
|
|
283
|
+
let starting;
|
|
284
|
+
return {
|
|
285
|
+
/** Bind 127.0.0.1 on an ephemeral port; resolves the bound port. Idempotent. */
|
|
286
|
+
start() {
|
|
287
|
+
starting ??= new Promise((resolve, reject) => {
|
|
288
|
+
server.once('error', reject);
|
|
289
|
+
server.listen(0, LOOPBACK_HOST, () => {
|
|
290
|
+
const address = server.address();
|
|
291
|
+
if (address === null || typeof address === 'object' && typeof address.port !== 'number') {
|
|
292
|
+
reject(new Error('picasso egress gate: proxy bound without a port'));
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
resolve(address.port);
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
return starting;
|
|
299
|
+
},
|
|
300
|
+
/** The bound port, or undefined before `start()` resolves. */
|
|
301
|
+
port() {
|
|
302
|
+
const address = server.address();
|
|
303
|
+
return address === null || typeof address !== 'object' ? undefined : address.port;
|
|
304
|
+
},
|
|
305
|
+
/**
|
|
306
|
+
* The hard-fail probe: the proxy counts as available only while it is
|
|
307
|
+
* listening AND a loopback TCP connect succeeds (a wedged accept queue
|
|
308
|
+
* or a half-closed listener must not pass for "up").
|
|
309
|
+
*/
|
|
310
|
+
async isUp(probe = tcpReachable) {
|
|
311
|
+
if (!server.listening) return false;
|
|
312
|
+
const port = this.port();
|
|
313
|
+
if (typeof port !== 'number') return false;
|
|
314
|
+
try {
|
|
315
|
+
return await probe(LOOPBACK_HOST, port);
|
|
316
|
+
} catch {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
},
|
|
320
|
+
/** Stop accepting and tear down every in-flight connection/tunnel. */
|
|
321
|
+
async close() {
|
|
322
|
+
for (const socket of sockets) socket.destroy();
|
|
323
|
+
await new Promise((resolve) => {
|
|
324
|
+
if (!server.listening) {
|
|
325
|
+
resolve();
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
server.close(() => resolve());
|
|
329
|
+
});
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** The child-process environment a gated session's commands are spawned with. */
|
|
335
|
+
export function egressEnv(token, port) {
|
|
336
|
+
const url = `http://${token}@${LOOPBACK_HOST}:${port}`;
|
|
337
|
+
const env = { NODE_USE_ENV_PROXY: '1', no_proxy: '', NO_PROXY: '' };
|
|
338
|
+
for (const name of PROXY_ENV_VARS) env[name] = url;
|
|
339
|
+
return env;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function requireSessionId(params) {
|
|
343
|
+
if (typeof params?.sessionId !== 'string' || params.sessionId.length === 0) {
|
|
344
|
+
throw new TypeError('a non-empty sessionId is required');
|
|
345
|
+
}
|
|
346
|
+
return params.sessionId;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* The gate: per-session rules, the shared proxy, the JSON-RPC methods, the
|
|
351
|
+
* `tools/pre-execute` handler and the `ctx.shell.execute` wrapper. Pass
|
|
352
|
+
* `proxy` to inject a fake in tests; `random` mints session tokens.
|
|
353
|
+
*/
|
|
354
|
+
export function createEgressGate({ proxy, warn, random = () => randomBytes(18).toString('base64url') } = {}) {
|
|
355
|
+
/** sessionId -> { allow, failIfUnavailable, ports, token } — in-memory like session grants; the TUI re-sets after a restart/resume. */
|
|
356
|
+
const rules = new Map();
|
|
357
|
+
/** token -> sessionId (the proxy's auth index; `clear` revokes). */
|
|
358
|
+
const sessions = new Map();
|
|
359
|
+
const ownedProxy = proxy === undefined;
|
|
360
|
+
const egress = proxy ?? createEgressProxy({
|
|
361
|
+
ruleForToken(token) {
|
|
362
|
+
const sessionId = sessions.get(token);
|
|
363
|
+
return sessionId === undefined ? undefined : rules.get(sessionId);
|
|
364
|
+
},
|
|
365
|
+
warn,
|
|
366
|
+
});
|
|
367
|
+
const up = () => egress.isUp();
|
|
368
|
+
const methods = {
|
|
369
|
+
/**
|
|
370
|
+
* Set (or replace) the session's egress rule. Starts the shared proxy;
|
|
371
|
+
* a proxy that cannot bind fails the call and stores nothing — a rule
|
|
372
|
+
* is never left pointing at a dead gate.
|
|
373
|
+
*/
|
|
374
|
+
async 'sandbox/network/set'(params) {
|
|
375
|
+
const sessionId = requireSessionId(params);
|
|
376
|
+
const allow = normalizeAllowlist(params?.allow);
|
|
377
|
+
const failIfUnavailable = params?.failIfUnavailable !== false;
|
|
378
|
+
const ports = normalizePorts(params?.ports);
|
|
379
|
+
await egress.start();
|
|
380
|
+
const existing = rules.get(sessionId);
|
|
381
|
+
const token = existing?.token ?? random();
|
|
382
|
+
sessions.set(token, sessionId);
|
|
383
|
+
rules.set(sessionId, { allow, failIfUnavailable, ports, token });
|
|
384
|
+
return {
|
|
385
|
+
sessionId,
|
|
386
|
+
gated: true,
|
|
387
|
+
allow,
|
|
388
|
+
failIfUnavailable,
|
|
389
|
+
ports: [...ports],
|
|
390
|
+
proxy: { host: LOOPBACK_HOST, port: egress.port() },
|
|
391
|
+
};
|
|
392
|
+
},
|
|
393
|
+
async 'sandbox/network/get'(params) {
|
|
394
|
+
const sessionId = requireSessionId(params);
|
|
395
|
+
const rule = rules.get(sessionId);
|
|
396
|
+
if (rule === undefined) return { sessionId, gated: false };
|
|
397
|
+
return { sessionId, gated: true, allow: [...rule.allow], failIfUnavailable: rule.failIfUnavailable, ports: [...rule.ports] };
|
|
398
|
+
},
|
|
399
|
+
async 'sandbox/network/clear'(params) {
|
|
400
|
+
const sessionId = requireSessionId(params);
|
|
401
|
+
const rule = rules.get(sessionId);
|
|
402
|
+
if (rule === undefined) return { sessionId, cleared: false };
|
|
403
|
+
rules.delete(sessionId);
|
|
404
|
+
sessions.delete(rule.token);
|
|
405
|
+
return { sessionId, cleared: true };
|
|
406
|
+
},
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* `tools/pre-execute` handler: denies what the env seam cannot reach and
|
|
411
|
+
* hard-fails gated calls while the proxy is unreachable and the session
|
|
412
|
+
* demands it. Ungated sessions pass through untouched — the gate is
|
|
413
|
+
* opt-in per session, never a silent policy change.
|
|
414
|
+
*/
|
|
415
|
+
async function preExecute(exec, next) {
|
|
416
|
+
const sessionId = exec?.agent?.session?.id;
|
|
417
|
+
const rule = sessionId === undefined ? undefined : rules.get(sessionId);
|
|
418
|
+
if (rule === undefined) return next();
|
|
419
|
+
if (UNGATED_NETWORK_TOOLS.has(exec?.name)) {
|
|
420
|
+
return {
|
|
421
|
+
kind: 'deny',
|
|
422
|
+
reason: `Picasso egress gate: "${exec.name}" performs network access the session domain allowlist cannot reach (it does not run through the gated shell environment), so it is denied while the session is egress-gated. Use a gated bash command (e.g. curl) instead.`,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
if (rule.failIfUnavailable && !(await up())) {
|
|
426
|
+
return {
|
|
427
|
+
kind: 'deny',
|
|
428
|
+
reason: 'Picasso egress gate: the domain-allowlist proxy is unreachable and this session requires it (failIfUnavailable); the call is refused rather than allowed to run ungated.',
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
return next();
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Wrap `shell.execute` once (idempotent per instance, ignore-sandbox.js
|
|
436
|
+
* precedent): every resolved spec carries `sandboxPolicy.sessionId`, so
|
|
437
|
+
* gated sessions get the proxy env regardless of which tool spawned the
|
|
438
|
+
* command (bash, pwsh, background jobs, slash-command shells). When the
|
|
439
|
+
* session demands availability and the proxy is unreachable the spawn
|
|
440
|
+
* itself fails — the hard fail covers callers that bypass
|
|
441
|
+
* tools/pre-execute entirely.
|
|
442
|
+
*/
|
|
443
|
+
function wrapShellExecutor(shell) {
|
|
444
|
+
if (shell === undefined || shell === null || typeof shell.execute !== 'function') return false;
|
|
445
|
+
if (shell[PATCHED] === true) return true;
|
|
446
|
+
const execute = shell.execute;
|
|
447
|
+
const wrapped = async function picassoEgressExecute(spec) {
|
|
448
|
+
const sessionId = spec?.sandboxPolicy?.sessionId;
|
|
449
|
+
const rule = sessionId === undefined ? undefined : rules.get(sessionId);
|
|
450
|
+
if (rule === undefined) return execute.call(this ?? shell, spec);
|
|
451
|
+
if (rule.failIfUnavailable && !(await up())) {
|
|
452
|
+
const error = new Error(`picasso egress gate unavailable for session ${sessionId}: the domain-allowlist proxy is not listening and the session's egress policy requires it`);
|
|
453
|
+
error.code = 'EGRESS_GATE_UNAVAILABLE';
|
|
454
|
+
throw error;
|
|
455
|
+
}
|
|
456
|
+
const port = egress.port();
|
|
457
|
+
if (typeof port !== 'number') {
|
|
458
|
+
// A rule exists but the proxy lost its port — never spawn ungated.
|
|
459
|
+
const error = new Error(`picasso egress gate unavailable for session ${sessionId}: the domain-allowlist proxy has no bound port`);
|
|
460
|
+
error.code = 'EGRESS_GATE_UNAVAILABLE';
|
|
461
|
+
throw error;
|
|
462
|
+
}
|
|
463
|
+
return execute.call(this ?? shell, { ...spec, env: { ...(spec.env ?? {}), ...egressEnv(rule.token, port) } });
|
|
464
|
+
};
|
|
465
|
+
try {
|
|
466
|
+
shell.execute = wrapped;
|
|
467
|
+
Object.defineProperty(shell, PATCHED, { value: true, enumerable: false, configurable: true });
|
|
468
|
+
} catch {
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
return shell.execute === wrapped;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Install on the plugin context: wrap `ctx.shell` now if mounted and retry
|
|
476
|
+
* before each tool call (the executor may mount after this plugin —
|
|
477
|
+
* installIgnoreSandbox precedent), then run the gate's own pre-execute
|
|
478
|
+
* policy. Returns the disposer.
|
|
479
|
+
*/
|
|
480
|
+
function install(ctx) {
|
|
481
|
+
const attempt = () => {
|
|
482
|
+
let shell;
|
|
483
|
+
try {
|
|
484
|
+
shell = ctx.get?.('shell') ?? ctx.shell;
|
|
485
|
+
} catch {
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
if (!wrapShellExecutor(shell)) {
|
|
489
|
+
warn?.('Picasso egress gate: ctx.shell could not be wrapped; gated sessions cannot receive the proxy environment.');
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
attempt();
|
|
493
|
+
return ctx.on('tools/pre-execute', async (exec, next) => {
|
|
494
|
+
attempt();
|
|
495
|
+
return preExecute(exec, next);
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
return {
|
|
500
|
+
methods,
|
|
501
|
+
preExecute,
|
|
502
|
+
wrapShellExecutor,
|
|
503
|
+
install,
|
|
504
|
+
ruleFor: (sessionId) => rules.get(sessionId),
|
|
505
|
+
up,
|
|
506
|
+
proxy: egress,
|
|
507
|
+
/** Drop a session's rule (session teardown); revokes its token. */
|
|
508
|
+
drop: (sessionId) => {
|
|
509
|
+
const rule = rules.get(sessionId);
|
|
510
|
+
if (rule === undefined) return false;
|
|
511
|
+
rules.delete(sessionId);
|
|
512
|
+
sessions.delete(rule.token);
|
|
513
|
+
return true;
|
|
514
|
+
},
|
|
515
|
+
/** Close the shared proxy when this plugin owns it. */
|
|
516
|
+
async close() {
|
|
517
|
+
if (ownedProxy) await egress.close();
|
|
518
|
+
},
|
|
519
|
+
};
|
|
520
|
+
}
|