passport-bridge 0.7.0 → 0.8.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 +115 -54
- package/dist/activeProfile.js +122 -0
- package/dist/boundedFile.js +46 -0
- package/dist/boundedMcpFetch.js +98 -0
- package/dist/boundedResponse.js +56 -0
- package/dist/bridge.js +427 -107
- package/dist/bridgeConfig.js +19 -0
- package/dist/bridgeShutdown.js +30 -0
- package/dist/cli.js +195 -37
- package/dist/cliArgs.js +8 -1
- package/dist/cliClient.js +24 -46
- package/dist/cliConnect.js +161 -0
- package/dist/cliDeadline.js +27 -0
- package/dist/cliLogin.js +175 -0
- package/dist/cliOffline.js +119 -0
- package/dist/cliOutput.js +116 -0
- package/dist/cliPagination.js +55 -0
- package/dist/cliResult.js +111 -0
- package/dist/cliSession.js +163 -0
- package/dist/cliSupport.js +232 -0
- package/dist/completion.js +25 -10
- package/dist/credentialStorage.js +293 -0
- package/dist/credentials.js +364 -108
- package/dist/discover.js +60 -34
- package/dist/doctor.js +126 -16
- package/dist/legacyCliSecurity.js +26 -0
- package/dist/passport.js +169 -584
- package/dist/privateStableFile.js +170 -0
- package/dist/recoveryCleanup.js +17 -0
- package/dist/recoveryIdentity.js +80 -0
- package/dist/tokenWorkspace.js +5 -0
- package/dist/version.js +1 -1
- package/npm-shrinkwrap.json +1740 -0
- package/package.json +21 -7
- package/dist/discover.test.js +0 -51
package/README.md
CHANGED
|
@@ -10,48 +10,65 @@ Both binaries use the Passport gateway. Member-authenticated CLI calls share too
|
|
|
10
10
|
npm install --global passport-bridge
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
Node.js
|
|
13
|
+
Node.js 22.23.2 or newer is required.
|
|
14
14
|
|
|
15
15
|
## Passport CLI
|
|
16
16
|
|
|
17
17
|
Sign in once, then search and run the tools your workspace permits:
|
|
18
18
|
|
|
19
19
|
```sh
|
|
20
|
-
passport login --cloud https://passport.example.com --workspace ws_example
|
|
20
|
+
passport login --cloud https://passport.example.com --workspace ws_example --profile cli@ws_example
|
|
21
21
|
passport status
|
|
22
22
|
passport search "open issues"
|
|
23
23
|
passport describe github_search_issues
|
|
24
24
|
passport run github_search_issues '{"query":"SSO"}'
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
+
A successful login makes the new profile active. On a machine with more than
|
|
28
|
+
one stored profile, inspect or switch the active profile without a network
|
|
29
|
+
request:
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
passport profiles
|
|
33
|
+
passport use cli@ws_example
|
|
34
|
+
passport status
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
For Passport Cloud, `passport login --workspace <id>` can omit `--cloud`.
|
|
38
|
+
Self-hosted deployments pass their origin explicitly. Add `--no-open` to print
|
|
39
|
+
the device URL without launching a browser.
|
|
40
|
+
|
|
27
41
|
`passport help [command]` documents every option. `passport help` and `passport version` work without a profile or network connection.
|
|
28
42
|
|
|
29
43
|
Remote Passport servers must use HTTPS. Plain HTTP is accepted only for local
|
|
30
44
|
development on `localhost`, `127.0.0.0/8`, or `::1`. Self-hosted deployments
|
|
31
45
|
should terminate HTTPS before using interactive profiles or agent keys.
|
|
32
46
|
|
|
33
|
-
Passport CLI 0.
|
|
47
|
+
Passport CLI 0.8.0 requires gateway MCP serverInfo `passport` version 0.5.0 or
|
|
34
48
|
newer and the exact four-tool governed CLI protocol. Older servers fail clearly
|
|
35
49
|
before the CLI makes a tool call.
|
|
36
50
|
|
|
37
51
|
### Commands
|
|
38
52
|
|
|
39
|
-
| Command
|
|
40
|
-
|
|
|
41
|
-
| `login`
|
|
42
|
-
| `logout`
|
|
43
|
-
| `status`
|
|
44
|
-
| `doctor`
|
|
45
|
-
| `search [query]`
|
|
46
|
-
| `describe <tool>`
|
|
47
|
-
| `run <tool> [json]`
|
|
48
|
-
| `connect [app...]`
|
|
49
|
-
| `resources [uri]`
|
|
50
|
-
| `prompts [name]`
|
|
51
|
-
| `discover`
|
|
52
|
-
| `
|
|
53
|
-
| `
|
|
54
|
-
| `
|
|
53
|
+
| Command | Purpose |
|
|
54
|
+
| -------------------- | -------------------------------------------------------------------------------------------------------------------- |
|
|
55
|
+
| `login` | Complete device sign-in and save a rotating CLI credential. |
|
|
56
|
+
| `logout` | Revoke the credential and remove its local profile. |
|
|
57
|
+
| `status` | Show the workspace, member, client, and visible MCP connection states. |
|
|
58
|
+
| `doctor` | Check runtime, credential-store safety, configuration, reachability, authentication, and CLI protocol compatibility. |
|
|
59
|
+
| `search [query]` | Search permitted tool definitions. An empty query lists the complete catalog. |
|
|
60
|
+
| `describe <tool>` | Print one exact definition and its full parameter schema. |
|
|
61
|
+
| `run <tool> [json]` | Run a tool through Passport governance. |
|
|
62
|
+
| `connect [app...]` | Show secure member connection links and optionally wait for completion. |
|
|
63
|
+
| `resources [uri]` | List or read MCP resources. |
|
|
64
|
+
| `prompts [name]` | List or get MCP prompts. |
|
|
65
|
+
| `discover` | Scan supported global MCP configuration and submit a privacy-minimized fleet report. |
|
|
66
|
+
| `session` | Process NDJSON commands through one authenticated MCP session. |
|
|
67
|
+
| `completion <shell>` | Generate completion for Bash, Zsh, or Fish without a profile or network. |
|
|
68
|
+
| `profiles` | List stored profiles and show the active profile without a network request. |
|
|
69
|
+
| `use <name>` | Select the active stored profile without a network request. |
|
|
70
|
+
| `help [command]` | Show general or command-specific help. |
|
|
71
|
+
| `version` | Show the package version and locally resolvable profile context. |
|
|
55
72
|
|
|
56
73
|
Every command accepts `--profile <name>`, `--json`, `--timeout <seconds>`, `--quiet`, and `--caller <id>` where applicable.
|
|
57
74
|
|
|
@@ -71,6 +88,11 @@ passport doctor --profile cli@ws_example
|
|
|
71
88
|
passport doctor --profile cli@ws_example --json
|
|
72
89
|
```
|
|
73
90
|
|
|
91
|
+
When profile selection is unclear, start with `passport profiles` and choose
|
|
92
|
+
one with `passport use <name>`. Use `--profile <name>` for a one-off command or
|
|
93
|
+
`PASSPORT_PROFILE=<name>` for a shell scope; explicit selectors take precedence
|
|
94
|
+
over the active profile.
|
|
95
|
+
|
|
74
96
|
`doctor` checks Node.js, the credential file's shape/ownership/mode, auth
|
|
75
97
|
configuration, HTTPS policy, `/healthz`, the authenticated MCP handshake,
|
|
76
98
|
serverInfo compatibility, and the exact four-tool CLI protocol. It never prints
|
|
@@ -118,6 +140,34 @@ passport run github_search_issues --set query=SSO --set-json limit=10
|
|
|
118
140
|
|
|
119
141
|
These forms are mutually exclusive. `--set` produces strings and `--set-json` accepts any valid JSON value.
|
|
120
142
|
|
|
143
|
+
### Persistent agent sessions
|
|
144
|
+
|
|
145
|
+
Agents performing several operations can avoid repeated process startup, token
|
|
146
|
+
exchange, MCP initialization, and protocol checks by keeping one CLI process
|
|
147
|
+
open:
|
|
148
|
+
|
|
149
|
+
```sh
|
|
150
|
+
printf '%s\n' \
|
|
151
|
+
'{"id":"find","argv":["search","open issues"]}' \
|
|
152
|
+
'{"id":"call","argv":["run","github_search_issues","{\"query\":\"SSO\"}"]}' \
|
|
153
|
+
| passport session --profile cli@ws_example
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
`passport session` reads one `{"id"?:string|number,"argv":string[]}` request
|
|
157
|
+
per line and emits one JSON response per non-empty line. Requests run
|
|
158
|
+
sequentially. Each response contains `ok`, `exitCode`, and either `result` or
|
|
159
|
+
the standard CLI error fields; `id` is echoed for every valid request. A request failure
|
|
160
|
+
does not close the session. EOF or SIGINT/SIGTERM closes the MCP transport
|
|
161
|
+
cleanly.
|
|
162
|
+
|
|
163
|
+
Session input is limited to 1 MiB per line and 128 arguments. Set global
|
|
164
|
+
`--profile`, `--caller`, and `--timeout` options on `passport session`, not in
|
|
165
|
+
individual request `argv` values. Supported request commands are `status`,
|
|
166
|
+
`search`, `describe`, `run`, `connect`, `resources`, `prompts`, and `discover`.
|
|
167
|
+
The mode does not weaken authorization: every operation still executes through
|
|
168
|
+
the governed gateway, and cached access tokens retain the normal expiry and
|
|
169
|
+
401-refresh behavior.
|
|
170
|
+
|
|
121
171
|
### Approvals and connections
|
|
122
172
|
|
|
123
173
|
An approval-required call exits immediately with code 4 and its approval ID. `--wait <seconds>` retries only after a definite structured `approval_pending` response:
|
|
@@ -131,7 +181,7 @@ The CLI never retries a call after a network failure or timeout because the exec
|
|
|
131
181
|
For an MCP that needs member sign-in:
|
|
132
182
|
|
|
133
183
|
```sh
|
|
134
|
-
passport connect github --
|
|
184
|
+
passport connect github --wait 120
|
|
135
185
|
```
|
|
136
186
|
|
|
137
187
|
Without app names, `connect` uses one aggregate link when several visible MCPs need sign-in. Agent-key principals receive status and member guidance but cannot mint browser links.
|
|
@@ -153,40 +203,45 @@ Human mode writes text content to stdout. Binary or other non-text content requi
|
|
|
153
203
|
|
|
154
204
|
With `--json`, stdout contains exactly one JSON value and no progress text. Hints and warnings use stderr. The success value for each command is:
|
|
155
205
|
|
|
156
|
-
| Command
|
|
157
|
-
|
|
|
158
|
-
| `login`
|
|
159
|
-
| `
|
|
160
|
-
| `
|
|
161
|
-
| `
|
|
162
|
-
| `
|
|
163
|
-
| `
|
|
164
|
-
| `
|
|
165
|
-
| `
|
|
166
|
-
| `
|
|
167
|
-
| `
|
|
168
|
-
| `
|
|
169
|
-
| `
|
|
170
|
-
| `
|
|
171
|
-
| `
|
|
172
|
-
|
|
173
|
-
|
|
206
|
+
| Command | JSON value |
|
|
207
|
+
| ------------ | --------------------------------------------------------------------------------------------------------- |
|
|
208
|
+
| `login` | `{profile, endpoint, client, surface, active, next}` |
|
|
209
|
+
| `profiles` | `{profiles:[{name,active,client?}],active?}` |
|
|
210
|
+
| `use` | `{profile,active:true}` |
|
|
211
|
+
| `logout` | `{profile, removed, revoked}` |
|
|
212
|
+
| `status` | `{workspace, member, endpoint, auth, profile?, client, mcps, next?}` |
|
|
213
|
+
| `doctor` | `{version,ok,auth,profile?,profileCount,endpointOrigin?,server?,checks}` (also emitted when checks fail). |
|
|
214
|
+
| `search` | The gateway search payload, including `results` or `catalog` and `note`. |
|
|
215
|
+
| `describe` | One tool definition. |
|
|
216
|
+
| `run` | The complete governed MCP tool result, preserving every content block and upstream `structuredContent`. |
|
|
217
|
+
| `connect` | The gateway connection payload, including `connections` and optional `connectAllUrl`. |
|
|
218
|
+
| `resources` | The MCP list or read result. |
|
|
219
|
+
| `prompts` | The MCP list or get result. |
|
|
220
|
+
| `discover` | `{snapshot,report,compliant}`. The snapshot contains privacy-minimized metadata only. |
|
|
221
|
+
| `session` | NDJSON envelopes: `{id?,ok,exitCode,result?}` or `{id?,ok:false,error,cause,exitCode,...}`. |
|
|
222
|
+
| `completion` | `{shell,script}`. Human mode emits the raw completion script. |
|
|
223
|
+
| `help` | `{command, help}` |
|
|
224
|
+
| `version` | `{version, endpoint?, profile?}` |
|
|
225
|
+
|
|
226
|
+
JSON errors have a stable base shape and may add safe remediation fields such
|
|
227
|
+
as `hint`, `profiles`, `next` (`argv` plus a description), `remediation`,
|
|
228
|
+
`retry`, `requiredInput`, or `connectUrl`:
|
|
174
229
|
|
|
175
230
|
```json
|
|
176
|
-
{"error":"...","cause":"blocked","exitCode":5,"app":"github"}
|
|
231
|
+
{ "error": "...", "cause": "blocked", "exitCode": 5, "app": "github" }
|
|
177
232
|
```
|
|
178
233
|
|
|
179
234
|
`approvalId` and `app` are included when supplied by the gateway.
|
|
180
235
|
|
|
181
|
-
| Exit | Meaning
|
|
182
|
-
| ---: |
|
|
183
|
-
|
|
|
184
|
-
|
|
|
185
|
-
|
|
|
186
|
-
|
|
|
187
|
-
|
|
|
188
|
-
|
|
|
189
|
-
|
|
|
236
|
+
| Exit | Meaning |
|
|
237
|
+
| ---: | ------------------------------------------------------ |
|
|
238
|
+
| 0 | Success |
|
|
239
|
+
| 1 | Network, timeout, server, or unsupported-content error |
|
|
240
|
+
| 2 | Missing, expired, or revoked authentication |
|
|
241
|
+
| 3 | The MCP needs member sign-in |
|
|
242
|
+
| 4 | Approval is pending |
|
|
243
|
+
| 5 | Policy, guardrail, selection, or approval denial |
|
|
244
|
+
| 6 | Invalid usage or unknown tool/app |
|
|
190
245
|
|
|
191
246
|
## Profiles
|
|
192
247
|
|
|
@@ -194,9 +249,13 @@ Credentials are named slots in one file, `~/.passport/credentials.json`, with mo
|
|
|
194
249
|
|
|
195
250
|
1. `--profile <name>`
|
|
196
251
|
2. `PASSPORT_PROFILE`
|
|
197
|
-
3. the
|
|
252
|
+
3. the active profile selected by `passport use <name>`
|
|
253
|
+
4. the sole stored profile
|
|
198
254
|
|
|
199
|
-
If several profiles exist, select one explicitly. The
|
|
255
|
+
If several profiles exist and none is active, select one explicitly. The
|
|
256
|
+
credential store is shared with `passport-bridge` and refresh exchanges are
|
|
257
|
+
coordinated across processes. The active-profile pointer contains no
|
|
258
|
+
credential and is local to this Passport credential store.
|
|
200
259
|
|
|
201
260
|
Profile names are 1-80 letters, numbers, dots, underscores, `@` signs, or hyphens, and must start with a letter or number. Credential updates are atomically replaced at mode `0600`, including when several profiles rotate concurrently.
|
|
202
261
|
|
|
@@ -253,10 +312,12 @@ access and points support/source inquiries at Passport's public release repo.
|
|
|
253
312
|
|
|
254
313
|
The CLI deliberately performs no background registry request, telemetry, or
|
|
255
314
|
automatic self-update. Check explicitly with `npm view passport-bridge version`
|
|
256
|
-
and upgrade with `npm install --global passport-bridge@latest`.
|
|
257
|
-
|
|
258
|
-
OIDC
|
|
259
|
-
|
|
315
|
+
and upgrade with `npm install --global passport-bridge@latest`. Releases publish
|
|
316
|
+
only through the repository's npm trusted-publisher workflow with short-lived
|
|
317
|
+
OIDC credentials, exact-source gates, verified tarball integrity, and registry
|
|
318
|
+
signature checks. npm does not expose public provenance attestations for a
|
|
319
|
+
private source repository, so Passport does not claim one. Platform signing is
|
|
320
|
+
not applicable to these JavaScript npm bins.
|
|
260
321
|
|
|
261
322
|
## MCP stdio bridge
|
|
262
323
|
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { closeSync, constants, fchmodSync, fstatSync, lstatSync, openSync, readFileSync, unlinkSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { credentialDir, enforceOwner, ensurePrivateDirectory, isMissing, syncDirectory, writePrivateAtomic } from "./credentialStorage.js";
|
|
4
|
+
const MAX_ACTIVE_PROFILE_BYTES = 512;
|
|
5
|
+
function activeFile() {
|
|
6
|
+
return join(credentialDir(), ".active-profile");
|
|
7
|
+
}
|
|
8
|
+
export function activeProfilePath() {
|
|
9
|
+
return activeFile();
|
|
10
|
+
}
|
|
11
|
+
export function readActiveProfileDocument(assertName) {
|
|
12
|
+
if (!ensurePrivateDirectory(false))
|
|
13
|
+
return { kind: "missing" };
|
|
14
|
+
let pathStat;
|
|
15
|
+
try {
|
|
16
|
+
pathStat = lstatSync(activeFile());
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
if (isMissing(error))
|
|
20
|
+
return { kind: "missing" };
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
if (pathStat.isSymbolicLink() || !pathStat.isFile()) {
|
|
24
|
+
throw new Error("Passport active profile must be a regular file, not a symlink.");
|
|
25
|
+
}
|
|
26
|
+
enforceOwner(pathStat, "active profile");
|
|
27
|
+
if (pathStat.size > MAX_ACTIVE_PROFILE_BYTES) {
|
|
28
|
+
throw new Error("Passport active profile is unexpectedly large; refusing to read it.");
|
|
29
|
+
}
|
|
30
|
+
let descriptor;
|
|
31
|
+
try {
|
|
32
|
+
descriptor = openSync(activeFile(), constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
33
|
+
const stat = fstatSync(descriptor);
|
|
34
|
+
if (!stat.isFile())
|
|
35
|
+
throw new Error("Passport active profile must be a regular file, not a symlink.");
|
|
36
|
+
if (stat.dev !== pathStat.dev || stat.ino !== pathStat.ino) {
|
|
37
|
+
throw new Error("Passport active profile changed while it was being opened; refusing to read it.");
|
|
38
|
+
}
|
|
39
|
+
enforceOwner(stat, "active profile");
|
|
40
|
+
if (stat.size > MAX_ACTIVE_PROFILE_BYTES) {
|
|
41
|
+
throw new Error("Passport active profile is unexpectedly large; refusing to read it.");
|
|
42
|
+
}
|
|
43
|
+
if (process.platform !== "win32")
|
|
44
|
+
fchmodSync(descriptor, 0o600);
|
|
45
|
+
const profile = readFileSync(descriptor, "utf8").trim();
|
|
46
|
+
if (!profile)
|
|
47
|
+
return { kind: "missing" };
|
|
48
|
+
try {
|
|
49
|
+
assertName(profile);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
throw new Error("Passport active profile is invalid; choose a profile with `passport use <name>`.");
|
|
53
|
+
}
|
|
54
|
+
return { kind: "present", profile };
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
if (isMissing(error))
|
|
58
|
+
return { kind: "missing" };
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
if (descriptor !== undefined)
|
|
63
|
+
closeSync(descriptor);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** Best-effort read for command resolution. A stale or damaged pointer must
|
|
67
|
+
* never make the CLI guess a credential; callers can fall back to the normal
|
|
68
|
+
* sole-profile rule or report ambiguity. */
|
|
69
|
+
export function readActiveProfile(assertName) {
|
|
70
|
+
try {
|
|
71
|
+
const result = readActiveProfileDocument(assertName);
|
|
72
|
+
return result.kind === "missing" ? undefined : result.profile;
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export function writeActiveProfileAtomic(name, assertName) {
|
|
79
|
+
assertName(name);
|
|
80
|
+
writePrivateAtomic(activeFile(), ".active-profile", `${name}\n`, "active profile");
|
|
81
|
+
}
|
|
82
|
+
/** The active-profile pointer contains no credential material. If an old or
|
|
83
|
+
* damaged installation left a symlink at that exact path, selecting a profile
|
|
84
|
+
* may safely unlink the symlink itself (never its target) before recreating the
|
|
85
|
+
* owner-only regular file. Other non-regular targets still fail closed. */
|
|
86
|
+
export function prepareActiveProfileForSelection() {
|
|
87
|
+
let stat;
|
|
88
|
+
try {
|
|
89
|
+
stat = lstatSync(activeFile());
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (isMissing(error))
|
|
93
|
+
return;
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
if (stat.isSymbolicLink()) {
|
|
97
|
+
unlinkSync(activeFile());
|
|
98
|
+
syncDirectory();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (!stat.isFile()) {
|
|
102
|
+
throw new Error("Passport active profile must be a regular file, not a symlink.");
|
|
103
|
+
}
|
|
104
|
+
enforceOwner(stat, "active profile");
|
|
105
|
+
}
|
|
106
|
+
export function clearActiveProfileAtomic() {
|
|
107
|
+
let stat;
|
|
108
|
+
try {
|
|
109
|
+
stat = lstatSync(activeFile());
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
if (isMissing(error))
|
|
113
|
+
return;
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
if (stat.isSymbolicLink() || !stat.isFile()) {
|
|
117
|
+
throw new Error("Passport active profile must be a regular file, not a symlink.");
|
|
118
|
+
}
|
|
119
|
+
enforceOwner(stat, "active profile");
|
|
120
|
+
unlinkSync(activeFile());
|
|
121
|
+
syncDirectory();
|
|
122
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { closeSync, constants, fstatSync, lstatSync, openSync, readSync } from "node:fs";
|
|
2
|
+
const READ_CHUNK_BYTES = 64 * 1024;
|
|
3
|
+
export function readBoundedUtf8Descriptor(descriptor, maxBytes) {
|
|
4
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 1)
|
|
5
|
+
throw new Error("Invalid local file size limit.");
|
|
6
|
+
const chunks = [];
|
|
7
|
+
let total = 0;
|
|
8
|
+
for (;;) {
|
|
9
|
+
const chunk = Buffer.allocUnsafe(Math.min(READ_CHUNK_BYTES, maxBytes - total + 1));
|
|
10
|
+
const count = readSync(descriptor, chunk, 0, chunk.length, null);
|
|
11
|
+
if (count === 0)
|
|
12
|
+
break;
|
|
13
|
+
total += count;
|
|
14
|
+
if (total > maxBytes)
|
|
15
|
+
throw new Error("Local input exceeded Passport's safety limit.");
|
|
16
|
+
chunks.push(chunk.subarray(0, count));
|
|
17
|
+
}
|
|
18
|
+
return Buffer.concat(chunks, total).toString("utf8");
|
|
19
|
+
}
|
|
20
|
+
export function readBoundedUtf8File(path, maxBytes) {
|
|
21
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 1)
|
|
22
|
+
throw new Error("Invalid local file size limit.");
|
|
23
|
+
// Inspect before open so a named pipe cannot block this synchronous boundary.
|
|
24
|
+
// O_NONBLOCK contains a replacement race; O_NOFOLLOW rejects a symlink at the
|
|
25
|
+
// actual open boundary. The descriptor identity check also covers platforms
|
|
26
|
+
// where O_NOFOLLOW is unavailable.
|
|
27
|
+
const before = lstatSync(path);
|
|
28
|
+
if (before.isSymbolicLink() || !before.isFile())
|
|
29
|
+
throw new Error("Local input must be a regular file.");
|
|
30
|
+
const noFollow = typeof constants.O_NOFOLLOW === "number" ? constants.O_NOFOLLOW : 0;
|
|
31
|
+
const descriptor = openSync(path, constants.O_RDONLY | constants.O_NONBLOCK | noFollow);
|
|
32
|
+
try {
|
|
33
|
+
const stat = fstatSync(descriptor);
|
|
34
|
+
if (!stat.isFile())
|
|
35
|
+
throw new Error("Local input must be a regular file.");
|
|
36
|
+
if (stat.dev !== before.dev || stat.ino !== before.ino) {
|
|
37
|
+
throw new Error("Local input changed while it was being opened.");
|
|
38
|
+
}
|
|
39
|
+
if (stat.size > maxBytes)
|
|
40
|
+
throw new Error("Local input exceeded Passport's safety limit.");
|
|
41
|
+
return readBoundedUtf8Descriptor(descriptor, maxBytes);
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
closeSync(descriptor);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export const CLI_MCP_RESPONSE_MAX_BYTES = 8 * 1024 * 1024;
|
|
2
|
+
/** Bound one-shot MCP JSON responses in total and long-lived SSE responses per
|
|
3
|
+
* event. The wrapper always cancels and unlocks the original reader on every
|
|
4
|
+
* terminal path so failed or abandoned CLI calls release their socket. */
|
|
5
|
+
export async function boundMcpResponse(response, maxBytes = CLI_MCP_RESPONSE_MAX_BYTES) {
|
|
6
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 1)
|
|
7
|
+
throw new Error("Invalid MCP response size limit.");
|
|
8
|
+
const eventStream = response.headers.get("content-type")?.toLowerCase().startsWith("text/event-stream") ?? false;
|
|
9
|
+
const rawLength = response.headers.get("content-length");
|
|
10
|
+
if (!eventStream && rawLength !== null) {
|
|
11
|
+
if (!/^\d+$/.test(rawLength) || !Number.isSafeInteger(Number(rawLength)) || Number(rawLength) > maxBytes) {
|
|
12
|
+
await response.body?.cancel().catch(() => undefined);
|
|
13
|
+
throw new Error("MCP response exceeded Passport's safety limit.");
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (!response.body)
|
|
17
|
+
return response;
|
|
18
|
+
const reader = response.body.getReader();
|
|
19
|
+
let released = false;
|
|
20
|
+
const release = () => {
|
|
21
|
+
if (released)
|
|
22
|
+
return;
|
|
23
|
+
released = true;
|
|
24
|
+
reader.releaseLock();
|
|
25
|
+
};
|
|
26
|
+
const cancel = async (reason) => {
|
|
27
|
+
try {
|
|
28
|
+
await reader.cancel(reason);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
// The original stream may already be errored; releasing the lock remains
|
|
32
|
+
// mandatory even when cancellation itself cannot complete cleanly.
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
release();
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
let bytes = 0;
|
|
39
|
+
let lineHasContent = false;
|
|
40
|
+
let previousWasCarriageReturn = false;
|
|
41
|
+
const body = new ReadableStream({
|
|
42
|
+
async pull(controller) {
|
|
43
|
+
try {
|
|
44
|
+
const next = await reader.read();
|
|
45
|
+
if (next.done) {
|
|
46
|
+
release();
|
|
47
|
+
controller.close();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (eventStream) {
|
|
51
|
+
for (const byte of next.value) {
|
|
52
|
+
bytes++;
|
|
53
|
+
if (byte === 13) {
|
|
54
|
+
if (!lineHasContent)
|
|
55
|
+
bytes = 0;
|
|
56
|
+
lineHasContent = false;
|
|
57
|
+
previousWasCarriageReturn = true;
|
|
58
|
+
}
|
|
59
|
+
else if (byte === 10) {
|
|
60
|
+
if (!previousWasCarriageReturn && !lineHasContent)
|
|
61
|
+
bytes = 0;
|
|
62
|
+
lineHasContent = false;
|
|
63
|
+
previousWasCarriageReturn = false;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
lineHasContent = true;
|
|
67
|
+
previousWasCarriageReturn = false;
|
|
68
|
+
}
|
|
69
|
+
if (bytes > maxBytes)
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
bytes += next.value.byteLength;
|
|
75
|
+
}
|
|
76
|
+
if (bytes > maxBytes) {
|
|
77
|
+
await cancel(new Error("MCP response exceeded Passport's safety limit."));
|
|
78
|
+
controller.error(new Error("MCP response exceeded Passport's safety limit."));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
controller.enqueue(next.value);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
await cancel();
|
|
85
|
+
controller.error(new Error("MCP response stream failed."));
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
cancel
|
|
89
|
+
});
|
|
90
|
+
return new Response(body, {
|
|
91
|
+
status: response.status,
|
|
92
|
+
statusText: response.statusText,
|
|
93
|
+
headers: response.headers
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
export function boundedMcpFetch(base, maxBytes = CLI_MCP_RESPONSE_MAX_BYTES) {
|
|
97
|
+
return (async (input, init) => boundMcpResponse(await base(input, init), maxBytes));
|
|
98
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export const DEFAULT_API_RESPONSE_BYTES = 8 * 1024 * 1024;
|
|
2
|
+
/** Parse a Fetch JSON response without trusting Content-Length or allowing a
|
|
3
|
+
* chunked Passport response to grow the client process without bound. */
|
|
4
|
+
export async function readBoundedJson(response, maxBytes = DEFAULT_API_RESPONSE_BYTES) {
|
|
5
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 1) {
|
|
6
|
+
throw new Error("Passport response size limit is invalid.");
|
|
7
|
+
}
|
|
8
|
+
const rawLength = response.headers.get("content-length");
|
|
9
|
+
if (rawLength !== null && !/^\d+$/.test(rawLength)) {
|
|
10
|
+
throw new Error("Passport returned an invalid or oversized response.");
|
|
11
|
+
}
|
|
12
|
+
const declared = rawLength === null ? undefined : Number(rawLength);
|
|
13
|
+
if (declared !== undefined && (!Number.isSafeInteger(declared) || declared > maxBytes)) {
|
|
14
|
+
await response.body?.cancel().catch(() => undefined);
|
|
15
|
+
throw new Error("Passport returned an invalid or oversized response.");
|
|
16
|
+
}
|
|
17
|
+
if (!response.body)
|
|
18
|
+
throw new Error("Passport returned an invalid or oversized response.");
|
|
19
|
+
const reader = response.body.getReader();
|
|
20
|
+
const chunks = [];
|
|
21
|
+
let total = 0;
|
|
22
|
+
try {
|
|
23
|
+
for (;;) {
|
|
24
|
+
const { done, value } = await reader.read();
|
|
25
|
+
if (done)
|
|
26
|
+
break;
|
|
27
|
+
if (!(value instanceof Uint8Array) || total + value.byteLength > maxBytes) {
|
|
28
|
+
await reader.cancel().catch(() => undefined);
|
|
29
|
+
throw new Error("Passport returned an invalid or oversized response.");
|
|
30
|
+
}
|
|
31
|
+
chunks.push(value);
|
|
32
|
+
total += value.byteLength;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
throw new Error("Passport returned an invalid or oversized response.");
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
reader.releaseLock();
|
|
40
|
+
}
|
|
41
|
+
if (declared !== undefined && declared !== total) {
|
|
42
|
+
throw new Error("Passport returned an invalid or oversized response.");
|
|
43
|
+
}
|
|
44
|
+
const bytes = new Uint8Array(total);
|
|
45
|
+
let offset = 0;
|
|
46
|
+
for (const chunk of chunks) {
|
|
47
|
+
bytes.set(chunk, offset);
|
|
48
|
+
offset += chunk.byteLength;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
throw new Error("Passport returned invalid JSON.");
|
|
55
|
+
}
|
|
56
|
+
}
|