passport-bridge 0.7.0 → 0.7.1
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 +80 -48
- package/dist/boundedFile.js +46 -0
- package/dist/boundedMcpFetch.js +98 -0
- package/dist/boundedResponse.js +56 -0
- package/dist/bridge.js +426 -99
- package/dist/bridgeConfig.js +19 -0
- package/dist/bridgeShutdown.js +30 -0
- package/dist/cli.js +193 -37
- package/dist/cliArgs.js +2 -0
- package/dist/cliClient.js +24 -46
- package/dist/cliDeadline.js +27 -0
- package/dist/cliLogin.js +103 -0
- package/dist/cliOffline.js +53 -0
- package/dist/cliOutput.js +106 -0
- package/dist/cliPagination.js +55 -0
- package/dist/cliResult.js +87 -0
- package/dist/cliSession.js +163 -0
- package/dist/cliSupport.js +191 -0
- package/dist/completion.js +22 -9
- package/dist/credentials.js +584 -83
- package/dist/discover.js +60 -34
- package/dist/doctor.js +56 -13
- package/dist/legacyCliSecurity.js +26 -0
- package/dist/passport.js +87 -469
- package/dist/privateStableFile.js +170 -0
- package/dist/recoveryCleanup.js +17 -0
- package/dist/recoveryIdentity.js +80 -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
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { chmodSync, closeSync, constants, fchmodSync, fstatSync, fsyncSync, linkSync, lstatSync, mkdirSync, openSync, readSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
function isCode(error, code) {
|
|
5
|
+
return error.code === code;
|
|
6
|
+
}
|
|
7
|
+
function enforceOwner(stat, label) {
|
|
8
|
+
if (typeof process.getuid === "function" && stat.uid !== process.getuid()) {
|
|
9
|
+
throw new Error(`Passport ${label} is owned by another account.`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function ensurePrivateDirectory(path, label) {
|
|
13
|
+
try {
|
|
14
|
+
const stat = lstatSync(path);
|
|
15
|
+
if (stat.isSymbolicLink() || !stat.isDirectory()) {
|
|
16
|
+
throw new Error(`Passport ${label} directory must be a regular directory, not a symlink.`);
|
|
17
|
+
}
|
|
18
|
+
enforceOwner(stat, `${label} directory`);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
if (!isCode(error, "ENOENT"))
|
|
22
|
+
throw error;
|
|
23
|
+
mkdirSync(path, { recursive: true, mode: 0o700 });
|
|
24
|
+
const stat = lstatSync(path);
|
|
25
|
+
if (stat.isSymbolicLink() || !stat.isDirectory()) {
|
|
26
|
+
throw new Error(`Passport ${label} directory must be a regular directory, not a symlink.`);
|
|
27
|
+
}
|
|
28
|
+
enforceOwner(stat, `${label} directory`);
|
|
29
|
+
}
|
|
30
|
+
if (process.platform !== "win32")
|
|
31
|
+
chmodSync(path, 0o700);
|
|
32
|
+
}
|
|
33
|
+
function readPrivateFile(path, label, maxBytes) {
|
|
34
|
+
let pathStat;
|
|
35
|
+
try {
|
|
36
|
+
pathStat = lstatSync(path);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
if (isCode(error, "ENOENT"))
|
|
40
|
+
return undefined;
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
if (pathStat.isSymbolicLink() || !pathStat.isFile()) {
|
|
44
|
+
throw new Error(`Passport ${label} must be a regular file, not a symlink.`);
|
|
45
|
+
}
|
|
46
|
+
enforceOwner(pathStat, label);
|
|
47
|
+
if (pathStat.size > maxBytes)
|
|
48
|
+
throw new Error(`Passport ${label} is unexpectedly large.`);
|
|
49
|
+
let descriptor;
|
|
50
|
+
try {
|
|
51
|
+
descriptor = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
52
|
+
const stat = fstatSync(descriptor);
|
|
53
|
+
if (!stat.isFile() || stat.dev !== pathStat.dev || stat.ino !== pathStat.ino) {
|
|
54
|
+
throw new Error(`Passport ${label} changed while it was being opened.`);
|
|
55
|
+
}
|
|
56
|
+
enforceOwner(stat, label);
|
|
57
|
+
if (stat.size > maxBytes)
|
|
58
|
+
throw new Error(`Passport ${label} is unexpectedly large.`);
|
|
59
|
+
if (process.platform !== "win32")
|
|
60
|
+
fchmodSync(descriptor, 0o600);
|
|
61
|
+
const contents = Buffer.alloc(maxBytes + 1);
|
|
62
|
+
let offset = 0;
|
|
63
|
+
for (;;) {
|
|
64
|
+
const count = readSync(descriptor, contents, offset, contents.length - offset, null);
|
|
65
|
+
if (count === 0)
|
|
66
|
+
break;
|
|
67
|
+
offset += count;
|
|
68
|
+
if (offset > maxBytes)
|
|
69
|
+
throw new Error(`Passport ${label} is unexpectedly large.`);
|
|
70
|
+
}
|
|
71
|
+
return contents.subarray(0, offset).toString("utf8");
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (isCode(error, "ENOENT"))
|
|
75
|
+
return undefined;
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
if (descriptor !== undefined)
|
|
80
|
+
closeSync(descriptor);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function syncDirectory(path) {
|
|
84
|
+
if (process.platform === "win32")
|
|
85
|
+
return;
|
|
86
|
+
const descriptor = openSync(path, constants.O_RDONLY);
|
|
87
|
+
try {
|
|
88
|
+
fsyncSync(descriptor);
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
closeSync(descriptor);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** Create an owner-only stable file exactly once. Hard-link publication is an
|
|
95
|
+
* atomic no-replace operation, so every concurrent process rereads the same
|
|
96
|
+
* winner rather than overwriting it with its own candidate. */
|
|
97
|
+
export function readOrCreatePrivateStableFile(options) {
|
|
98
|
+
if (!Number.isSafeInteger(options.maxBytes) || options.maxBytes < 1 || options.maxBytes > 16 * 1024 * 1024) {
|
|
99
|
+
throw new Error("Invalid private file size limit.");
|
|
100
|
+
}
|
|
101
|
+
const directory = dirname(options.target);
|
|
102
|
+
ensurePrivateDirectory(directory, options.label);
|
|
103
|
+
const parseExisting = () => {
|
|
104
|
+
const contents = readPrivateFile(options.target, options.label, options.maxBytes);
|
|
105
|
+
if (contents === undefined)
|
|
106
|
+
return undefined;
|
|
107
|
+
const parsed = options.parse(contents);
|
|
108
|
+
if (parsed === undefined)
|
|
109
|
+
throw new Error(`Passport ${options.label} is invalid; refusing to overwrite it.`);
|
|
110
|
+
return parsed;
|
|
111
|
+
};
|
|
112
|
+
const existing = parseExisting();
|
|
113
|
+
if (existing !== undefined)
|
|
114
|
+
return existing;
|
|
115
|
+
const contents = options.create();
|
|
116
|
+
if (Buffer.byteLength(contents) > options.maxBytes || options.parse(contents) === undefined) {
|
|
117
|
+
throw new Error(`Passport ${options.label} candidate is invalid.`);
|
|
118
|
+
}
|
|
119
|
+
const temporary = join(directory, `${options.temporaryPrefix}.${process.pid}.${randomBytes(8).toString("hex")}.tmp`);
|
|
120
|
+
let descriptor;
|
|
121
|
+
let failure;
|
|
122
|
+
try {
|
|
123
|
+
descriptor = openSync(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
124
|
+
if (process.platform !== "win32")
|
|
125
|
+
fchmodSync(descriptor, 0o600);
|
|
126
|
+
writeFileSync(descriptor, contents, "utf8");
|
|
127
|
+
fsyncSync(descriptor);
|
|
128
|
+
closeSync(descriptor);
|
|
129
|
+
descriptor = undefined;
|
|
130
|
+
try {
|
|
131
|
+
linkSync(temporary, options.target);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
if (!isCode(error, "EEXIST"))
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
failure = error;
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
if (descriptor !== undefined) {
|
|
143
|
+
try {
|
|
144
|
+
closeSync(descriptor);
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
failure ??= error;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
unlinkSync(temporary);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
if (!isCode(error, "ENOENT"))
|
|
155
|
+
failure ??= error;
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
syncDirectory(directory);
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
failure ??= error;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (failure)
|
|
165
|
+
throw failure;
|
|
166
|
+
const winner = parseExisting();
|
|
167
|
+
if (winner === undefined)
|
|
168
|
+
throw new Error(`Passport ${options.label} was not durably published.`);
|
|
169
|
+
return winner;
|
|
170
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { legacyFetch } from "./legacyCliSecurity.js";
|
|
2
|
+
/** Revoke a replacement after a local recovery failure. This intentionally
|
|
3
|
+
* does not accept the recovery flow's AbortSignal: cancellation must not leave
|
|
4
|
+
* a newly issued server credential live, and legacyFetch supplies its own
|
|
5
|
+
* bounded timeout. */
|
|
6
|
+
export async function revokeIssuedRecoveryCredential(cloud, refreshToken, fetcher = fetch) {
|
|
7
|
+
try {
|
|
8
|
+
await legacyFetch(`${cloud}/api/token/revoke`, {
|
|
9
|
+
method: "POST",
|
|
10
|
+
headers: { "content-type": "application/json" },
|
|
11
|
+
body: JSON.stringify({ refreshToken })
|
|
12
|
+
}, fetcher);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
// Best effort: the local profile remains unchanged when recovery fails.
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const VALID_INSTANCE_ID = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
2
|
+
const VALID_CLIENT = /^[A-Za-z0-9._-]{1,40}$/;
|
|
3
|
+
const QUALIFIED_PROFILE = /^([A-Za-z0-9._-]{1,40})(?:@([A-Za-z0-9_-]{1,80}))?$/;
|
|
4
|
+
const TARGET_KEY = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
5
|
+
const BARE_DESKTOP_CLIENTS = new Set(["claude", "claude-code", "cursor", "vscode", "codex"]);
|
|
6
|
+
function targetFromEndpoint(raw) {
|
|
7
|
+
try {
|
|
8
|
+
const parsed = new URL(raw);
|
|
9
|
+
if ((parsed.protocol !== "http:" && parsed.protocol !== "https:") ||
|
|
10
|
+
parsed.username ||
|
|
11
|
+
parsed.password ||
|
|
12
|
+
parsed.search ||
|
|
13
|
+
parsed.hash) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
if (parsed.pathname === "/mcp")
|
|
17
|
+
return "workspace";
|
|
18
|
+
const app = parsed.pathname.match(/^\/s\/([A-Za-z0-9][A-Za-z0-9._-]{0,127})\/mcp$/)?.[1];
|
|
19
|
+
if (app && TARGET_KEY.test(app))
|
|
20
|
+
return `app:${app}`;
|
|
21
|
+
const bundle = parsed.pathname.match(/^\/b\/([A-Za-z0-9][A-Za-z0-9._-]{0,127})\/mcp$/)?.[1];
|
|
22
|
+
if (bundle && TARGET_KEY.test(bundle))
|
|
23
|
+
return `bundle:${bundle}`;
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Resolve the identity of a saved profile without inventing a new device.
|
|
32
|
+
*
|
|
33
|
+
* Desktop profiles created before recovery metadata was persisted have a
|
|
34
|
+
* qualified slot name (`<client>@<workspace>`), which is enough to recover
|
|
35
|
+
* the policy client but not the installation id. The server receives the
|
|
36
|
+
* previous credential as a private recovery hint and resolves that exact
|
|
37
|
+
* device. A metadata-free profile derives its route target from the saved
|
|
38
|
+
* endpoint; an unrecognized endpoint is intentionally rejected instead of
|
|
39
|
+
* silently falling back to the workspace.
|
|
40
|
+
*
|
|
41
|
+
* The old recovery bug could persist the whole qualified profile name as the
|
|
42
|
+
* client. Treat that exact shape as repairable poison; any other disagreement
|
|
43
|
+
* between a stored client and its slot name is unsafe to guess through.
|
|
44
|
+
*/
|
|
45
|
+
export function recoveryIdentity(profile, storedProfile, expectedWorkspace, endpoint) {
|
|
46
|
+
const qualified = profile.match(QUALIFIED_PROFILE);
|
|
47
|
+
const profileClient = qualified && (qualified[2] !== undefined || BARE_DESKTOP_CLIENTS.has(qualified[1])) ? qualified[1] : undefined;
|
|
48
|
+
if (qualified?.[2] && expectedWorkspace && qualified[2] !== expectedWorkspace) {
|
|
49
|
+
throw new Error("Passport couldn't safely recover this profile's workspace identity. Reconnect this client from the Passport app.");
|
|
50
|
+
}
|
|
51
|
+
const storedClient = storedProfile?.client;
|
|
52
|
+
const exactQualifiedPoison = Boolean(storedClient && storedClient === profile && profileClient && profileClient !== profile);
|
|
53
|
+
if (storedClient !== undefined && !VALID_CLIENT.test(storedClient) && !exactQualifiedPoison) {
|
|
54
|
+
throw new Error("Passport couldn't safely recover this profile's client identity. Reconnect this client from the Passport app.");
|
|
55
|
+
}
|
|
56
|
+
// Explicit metadata is authoritative even when a user-chosen profile name
|
|
57
|
+
// has a different prefix (for example a dynamic CLI profile). Only the
|
|
58
|
+
// exact old poison shape above is rewritten from the qualified slot name.
|
|
59
|
+
const client = exactQualifiedPoison ? profileClient : (storedClient ?? profileClient);
|
|
60
|
+
if (!client || !VALID_CLIENT.test(client)) {
|
|
61
|
+
throw new Error("Passport couldn't safely infer this profile's client identity. Reconnect this client from the Passport app.");
|
|
62
|
+
}
|
|
63
|
+
const endpointTarget = endpoint ? targetFromEndpoint(endpoint) : undefined;
|
|
64
|
+
const target = storedProfile?.target ?? endpointTarget;
|
|
65
|
+
if (!target ||
|
|
66
|
+
target.length > 160 ||
|
|
67
|
+
/[\u0000-\u001f\u007f]/.test(target) ||
|
|
68
|
+
(endpoint !== undefined && endpointTarget === undefined) ||
|
|
69
|
+
(endpointTarget !== undefined && target !== endpointTarget)) {
|
|
70
|
+
throw new Error("Passport couldn't safely recover this profile's target. Reconnect this client from the Passport app.");
|
|
71
|
+
}
|
|
72
|
+
if (storedProfile?.instanceId !== undefined && !VALID_INSTANCE_ID.test(storedProfile.instanceId)) {
|
|
73
|
+
throw new Error("Passport couldn't safely recover this profile's installation identity. Reconnect this client from the Passport app.");
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
client,
|
|
77
|
+
target,
|
|
78
|
+
...(storedProfile?.instanceId ? { instanceId: storedProfile.instanceId } : {})
|
|
79
|
+
};
|
|
80
|
+
}
|
package/dist/version.js
CHANGED