@wonderwhy-er/desktop-commander 0.3.2 → 0.3.3
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/dist/remote-device/device-authenticator.js +2 -0
- package/dist/remote-device/device.d.ts +2 -1
- package/dist/remote-device/device.js +46 -29
- package/dist/remote-device/remote-channel.js +1 -1
- package/dist/remote-device/remote-utils.d.ts +20 -0
- package/dist/remote-device/remote-utils.js +79 -5
- package/dist/remote-device/rollouts.js +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -40,6 +40,7 @@ export class DeviceAuthenticator {
|
|
|
40
40
|
try {
|
|
41
41
|
response = await this.fetchFn(`${this.baseServerUrl}/device/start`, {
|
|
42
42
|
method: 'POST',
|
|
43
|
+
redirect: 'error',
|
|
43
44
|
headers: { 'Content-Type': 'application/json' },
|
|
44
45
|
body: JSON.stringify({
|
|
45
46
|
client_id: CLIENT_ID,
|
|
@@ -116,6 +117,7 @@ export class DeviceAuthenticator {
|
|
|
116
117
|
try {
|
|
117
118
|
response = await this.fetchFn(`${this.baseServerUrl}/device/poll`, {
|
|
118
119
|
method: 'POST',
|
|
120
|
+
redirect: 'error',
|
|
119
121
|
headers: { 'Content-Type': 'application/json' },
|
|
120
122
|
body: JSON.stringify({
|
|
121
123
|
device_code: deviceAuth.device_code,
|
|
@@ -13,6 +13,7 @@ export declare class MCPDevice {
|
|
|
13
13
|
private deviceId?;
|
|
14
14
|
private isShuttingDown;
|
|
15
15
|
private configPath;
|
|
16
|
+
private trustedAuthContext;
|
|
16
17
|
private persistSession;
|
|
17
18
|
private desktop;
|
|
18
19
|
/**
|
|
@@ -47,7 +48,7 @@ export declare class MCPDevice {
|
|
|
47
48
|
savePersistedConfig(rotated?: AuthSession): Promise<void>;
|
|
48
49
|
private writePersistedConfig;
|
|
49
50
|
fetchSupabaseConfig(): Promise<{
|
|
50
|
-
supabaseUrl:
|
|
51
|
+
supabaseUrl: string;
|
|
51
52
|
anonKey: any;
|
|
52
53
|
}>;
|
|
53
54
|
/**
|
|
@@ -15,7 +15,7 @@ import { observeTransport } from './transport-telemetry.js';
|
|
|
15
15
|
import { startTerminalTimestamps } from '../utils/terminal-timestamps.js';
|
|
16
16
|
import { VERSION } from '../version.js';
|
|
17
17
|
import { rolloutManager } from './rollouts.js';
|
|
18
|
-
import { getRemoteDeviceConfigPath,
|
|
18
|
+
import { DEFAULT_REMOTE_SERVER_URL, getRemoteDeviceConfigPath, savedSessionMatchesContext, validateRemoteAuthContext, validateRemoteServerUrl } from './remote-utils.js';
|
|
19
19
|
export { getRemoteDeviceConfigPath };
|
|
20
20
|
/**
|
|
21
21
|
* How many recently-handled call ids to remember for duplicate-delivery
|
|
@@ -76,14 +76,14 @@ export async function logoutRemoteDevice(configPath = getRemoteDeviceConfigPath(
|
|
|
76
76
|
!config.session?.access_token) {
|
|
77
77
|
throw new Error("No usable saved device session. Revoke this device in the dashboard; local credentials were kept");
|
|
78
78
|
}
|
|
79
|
-
const backend =
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
backend.
|
|
85
|
-
|
|
86
|
-
throw new Error("
|
|
79
|
+
const backend = validateRemoteServerUrl(process.env.MCP_SERVER_URL || DEFAULT_REMOTE_SERVER_URL);
|
|
80
|
+
// Logout sends a bearer token too: refuse to send a saved session to
|
|
81
|
+
// a different MCP backend. Only the pinned hosted origin may use an
|
|
82
|
+
// old, unbound device.json from before this security fix.
|
|
83
|
+
if (config.authContext === undefined
|
|
84
|
+
? backend.origin !== DEFAULT_REMOTE_SERVER_URL
|
|
85
|
+
: config.authContext?.serverOrigin !== backend.origin) {
|
|
86
|
+
throw new Error("Saved session belongs to another or unknown backend; credentials were kept");
|
|
87
87
|
}
|
|
88
88
|
const revoke = () => fetch(new URL("/device/revoke", backend), {
|
|
89
89
|
method: "POST",
|
|
@@ -104,13 +104,11 @@ export async function logoutRemoteDevice(configPath = getRemoteDeviceConfigPath(
|
|
|
104
104
|
if (!infoResponse.ok)
|
|
105
105
|
throw new Error("Could not refresh the saved session; credentials were kept");
|
|
106
106
|
const info = await infoResponse.json();
|
|
107
|
-
const
|
|
108
|
-
if ((
|
|
109
|
-
|
|
110
|
-
authUrl.password) {
|
|
111
|
-
throw new Error("Session refresh requires HTTPS or local HTTP; credentials were kept");
|
|
107
|
+
const authContext = validateRemoteAuthContext(backend.origin, info?.supabaseUrl);
|
|
108
|
+
if (!savedSessionMatchesContext(config.authContext, authContext)) {
|
|
109
|
+
throw new Error("Refusing to refresh credentials with a different Supabase backend");
|
|
112
110
|
}
|
|
113
|
-
const client = createClient(
|
|
111
|
+
const client = createClient(authContext.supabaseUrl, info.supabasePublishableKey, {
|
|
114
112
|
auth: {
|
|
115
113
|
persistSession: false,
|
|
116
114
|
autoRefreshToken: false,
|
|
@@ -159,6 +157,7 @@ export async function logoutRemoteDevice(configPath = getRemoteDeviceConfigPath(
|
|
|
159
157
|
}
|
|
160
158
|
export class MCPDevice {
|
|
161
159
|
constructor(options = {}) {
|
|
160
|
+
this.trustedAuthContext = null;
|
|
162
161
|
/**
|
|
163
162
|
* Serialises config writes. Rotations are 45 minutes apart in normal
|
|
164
163
|
* running, but a save that stalls must not land after a newer one and
|
|
@@ -170,7 +169,7 @@ export class MCPDevice {
|
|
|
170
169
|
this.loggedOutLocally = false;
|
|
171
170
|
/** Call ids already handled by THIS process (insertion-ordered, bounded). */
|
|
172
171
|
this.seenCallIds = new Set();
|
|
173
|
-
this.baseServerUrl = process.env.MCP_SERVER_URL ||
|
|
172
|
+
this.baseServerUrl = validateRemoteServerUrl(process.env.MCP_SERVER_URL || DEFAULT_REMOTE_SERVER_URL).origin;
|
|
174
173
|
this.deviceId = undefined;
|
|
175
174
|
this.isShuttingDown = false;
|
|
176
175
|
this.configPath = getRemoteDeviceConfigPath();
|
|
@@ -424,6 +423,14 @@ export class MCPDevice {
|
|
|
424
423
|
return null;
|
|
425
424
|
}
|
|
426
425
|
if (config.session && this.persistSession) {
|
|
426
|
+
if (!this.trustedAuthContext ||
|
|
427
|
+
!savedSessionMatchesContext(config.authContext, this.trustedAuthContext)) {
|
|
428
|
+
// Preserve the old file until new authorization succeeds, but
|
|
429
|
+
// never send its tokens or reuse its device id on this backend.
|
|
430
|
+
console.warn('⚠️ Saved Remote MCP session belongs to another or unknown backend; re-authorization required');
|
|
431
|
+
this.deviceId = undefined;
|
|
432
|
+
return null;
|
|
433
|
+
}
|
|
427
434
|
console.log('💾 Found persisted session for device ' + this.deviceId);
|
|
428
435
|
console.debug('[DEBUG] Session found in config, returning session');
|
|
429
436
|
return config.session;
|
|
@@ -438,6 +445,7 @@ export class MCPDevice {
|
|
|
438
445
|
else {
|
|
439
446
|
console.debug('[DEBUG] No session in config');
|
|
440
447
|
}
|
|
448
|
+
this.deviceId = undefined;
|
|
441
449
|
return null;
|
|
442
450
|
}
|
|
443
451
|
catch (error) {
|
|
@@ -482,6 +490,10 @@ export class MCPDevice {
|
|
|
482
490
|
* outlive it - shutdown() - can await it.
|
|
483
491
|
*/
|
|
484
492
|
async savePersistedConfig(rotated) {
|
|
493
|
+
if (!this.trustedAuthContext) {
|
|
494
|
+
console.error('Refusing to persist a session before validating the remote backend');
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
485
497
|
this.configWriteChain = this.configWriteChain.then(() => this.writePersistedConfig(rotated));
|
|
486
498
|
return this.configWriteChain;
|
|
487
499
|
}
|
|
@@ -503,6 +515,7 @@ export class MCPDevice {
|
|
|
503
515
|
}
|
|
504
516
|
const config = {
|
|
505
517
|
deviceId: this.deviceId,
|
|
518
|
+
authContext: this.trustedAuthContext,
|
|
506
519
|
// Only save session if --persist-session flag is set
|
|
507
520
|
session: (session && this.persistSession) ? {
|
|
508
521
|
access_token: session.access_token,
|
|
@@ -551,24 +564,28 @@ export class MCPDevice {
|
|
|
551
564
|
}
|
|
552
565
|
}
|
|
553
566
|
async fetchSupabaseConfig() {
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
567
|
+
const backend = validateRemoteServerUrl(this.baseServerUrl);
|
|
568
|
+
this.trustedAuthContext = null;
|
|
569
|
+
// Reject redirects before any saved credentials are passed to Supabase.
|
|
570
|
+
console.debug('[DEBUG] Fetching Supabase config from:', new URL('/api/mcp-info', backend).href);
|
|
571
|
+
const response = await fetch(new URL('/api/mcp-info', backend), {
|
|
572
|
+
redirect: 'error',
|
|
573
|
+
signal: AbortSignal.timeout(30000),
|
|
574
|
+
});
|
|
575
|
+
// auth-js checks token expiry against Date.now(), which may be skewed.
|
|
576
|
+
// Observe the trusted server's Date header before restoring a session.
|
|
561
577
|
observeServerDate(response.headers.get('date'));
|
|
562
578
|
if (!response.ok) {
|
|
563
|
-
console.debug('[DEBUG] Supabase config fetch failed, status:', response.status, response.statusText);
|
|
564
579
|
throw new Error(`Failed to fetch Supabase config: ${response.statusText}`);
|
|
565
580
|
}
|
|
566
581
|
const config = await response.json();
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
582
|
+
const trusted = validateRemoteAuthContext(backend.origin, config?.supabaseUrl);
|
|
583
|
+
if (typeof config?.supabasePublishableKey !== 'string' || !config.supabasePublishableKey.trim()) {
|
|
584
|
+
throw new Error('Missing Supabase publishable key from Remote MCP configuration');
|
|
585
|
+
}
|
|
586
|
+
this.trustedAuthContext = trusted;
|
|
587
|
+
console.debug('[DEBUG] Validated Supabase backend:', trusted.supabaseUrl);
|
|
588
|
+
return { supabaseUrl: trusted.supabaseUrl, anonKey: config.supabasePublishableKey };
|
|
572
589
|
}
|
|
573
590
|
// Methods moved to RemoteChannel
|
|
574
591
|
/**
|
|
@@ -177,7 +177,7 @@ export function observeServerDate(dateHeader) {
|
|
|
177
177
|
}
|
|
178
178
|
}
|
|
179
179
|
async function clockAwareFetch(input, init) {
|
|
180
|
-
const response = await fetch(input, init);
|
|
180
|
+
const response = await fetch(input, { ...init, redirect: 'error' });
|
|
181
181
|
observeServerDate(response.headers.get('date'));
|
|
182
182
|
return response;
|
|
183
183
|
}
|
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
/** This is the hosted MCP API, not a value learned from /api/mcp-info. */
|
|
2
|
+
export declare const DEFAULT_REMOTE_SERVER_URL = "https://mcp.desktopcommander.app";
|
|
3
|
+
/** Trusted project endpoints; never learned from /api/mcp-info. */
|
|
4
|
+
export declare const SUPABASE1_ORIGIN = "https://olvbkozcufcbptfogatw.supabase.co";
|
|
5
|
+
export declare const SUPABASE2_ORIGIN = "https://xnalhwxchxhthlmzkhal.supabase.co";
|
|
6
|
+
export declare const SUPABASE3_ORIGIN = "https://example.supabase.co";
|
|
7
|
+
export interface RemoteAuthContext {
|
|
8
|
+
serverOrigin: string;
|
|
9
|
+
supabaseUrl: string;
|
|
10
|
+
}
|
|
1
11
|
export declare function isBackendLocal(url: URL): boolean;
|
|
12
|
+
/** Validate before requesting configuration or sending any device credentials. */
|
|
13
|
+
export declare function validateRemoteServerUrl(value: string): URL;
|
|
14
|
+
/**
|
|
15
|
+
* Validate the Supabase endpoint BEFORE setSession() can send persisted tokens.
|
|
16
|
+
* Accept only the explicitly listed project endpoints. Saved sessions are
|
|
17
|
+
* separately bound to their exact MCP and Supabase endpoints before restoration.
|
|
18
|
+
*/
|
|
19
|
+
export declare function validateRemoteAuthContext(serverUrl: string, supabaseUrl: unknown): RemoteAuthContext;
|
|
20
|
+
/** Legacy unbound sessions may be restored ONLY against the original default MCP/supabase1 pair. */
|
|
21
|
+
export declare function savedSessionMatchesContext(saved: unknown, expected: RemoteAuthContext): boolean;
|
|
2
22
|
export declare function getRemoteDeviceConfigDirectory(home?: string): string;
|
|
3
23
|
export declare function getRemoteDeviceConfigPath(home?: string): string;
|
|
@@ -1,17 +1,91 @@
|
|
|
1
1
|
import os from "node:os";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
|
|
3
|
+
/** This is the hosted MCP API, not a value learned from /api/mcp-info. */
|
|
4
|
+
export const DEFAULT_REMOTE_SERVER_URL = "https://mcp.desktopcommander.app";
|
|
5
|
+
/** Trusted project endpoints; never learned from /api/mcp-info. */
|
|
6
|
+
export const SUPABASE1_ORIGIN = "https://olvbkozcufcbptfogatw.supabase.co";
|
|
7
|
+
export const SUPABASE2_ORIGIN = "https://xnalhwxchxhthlmzkhal.supabase.co";
|
|
8
|
+
export const SUPABASE3_ORIGIN = "https://example.supabase.co";
|
|
9
|
+
const LOCAL_BACKEND_HOSTNAMES = ["localhost", "127.0.0.1", "[::1]", "mcp.localhost"];
|
|
10
|
+
// This checks the hostname, not its resolved address. HTTP is only for the
|
|
11
|
+
// explicit local development hosts below; all remote endpoints require TLS.
|
|
4
12
|
export function isBackendLocal(url) {
|
|
5
13
|
return (url.protocol === "http:" &&
|
|
6
|
-
|
|
14
|
+
LOCAL_BACKEND_HOSTNAMES.includes(url.hostname));
|
|
15
|
+
}
|
|
16
|
+
function parseTrustedEndpoint(value, label) {
|
|
17
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
18
|
+
throw new Error(`Invalid ${label}: expected a URL`);
|
|
19
|
+
}
|
|
20
|
+
let url;
|
|
21
|
+
try {
|
|
22
|
+
url = new URL(value);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
throw new Error(`Invalid ${label}: expected a URL`);
|
|
26
|
+
}
|
|
27
|
+
if ((url.protocol !== "https:" && !isBackendLocal(url)) ||
|
|
28
|
+
url.username ||
|
|
29
|
+
url.password ||
|
|
30
|
+
url.search ||
|
|
31
|
+
url.hash) {
|
|
32
|
+
throw new Error(`Unsafe ${label}: HTTPS or local HTTP required, without credentials, query, or fragment`);
|
|
33
|
+
}
|
|
34
|
+
return url;
|
|
35
|
+
}
|
|
36
|
+
/** Validate before requesting configuration or sending any device credentials. */
|
|
37
|
+
export function validateRemoteServerUrl(value) {
|
|
38
|
+
const url = parseTrustedEndpoint(value, "MCP_SERVER_URL");
|
|
39
|
+
// Match a complete DNS label so lookalikes such as evildesktopcommander.app
|
|
40
|
+
// and desktopcommander.app.example.com cannot become credential recipients.
|
|
41
|
+
const isDesktopCommanderHost = url.hostname === "desktopcommander.app" ||
|
|
42
|
+
url.hostname.endsWith(".desktopcommander.app");
|
|
43
|
+
if (!isDesktopCommanderHost && !LOCAL_BACKEND_HOSTNAMES.includes(url.hostname)) {
|
|
44
|
+
throw new Error("Unsafe MCP_SERVER_URL: use desktopcommander.app, one of its subdomains, or a supported local host");
|
|
45
|
+
}
|
|
46
|
+
if (url.pathname !== "/") {
|
|
47
|
+
throw new Error("Unsafe MCP_SERVER_URL: paths are not supported");
|
|
48
|
+
}
|
|
49
|
+
return url;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Validate the Supabase endpoint BEFORE setSession() can send persisted tokens.
|
|
53
|
+
* Accept only the explicitly listed project endpoints. Saved sessions are
|
|
54
|
+
* separately bound to their exact MCP and Supabase endpoints before restoration.
|
|
55
|
+
*/
|
|
56
|
+
export function validateRemoteAuthContext(serverUrl, supabaseUrl) {
|
|
57
|
+
const server = validateRemoteServerUrl(serverUrl);
|
|
58
|
+
const auth = parseTrustedEndpoint(supabaseUrl, "Supabase URL");
|
|
59
|
+
// Compare the complete origin, including scheme and port; paths are never endpoints.
|
|
60
|
+
if ((auth.origin !== SUPABASE1_ORIGIN && auth.origin !== SUPABASE2_ORIGIN &&
|
|
61
|
+
auth.origin !== SUPABASE3_ORIGIN) ||
|
|
62
|
+
auth.pathname !== "/") {
|
|
63
|
+
throw new Error("Unsafe Supabase URL: only the supabase1, supabase2, and supabase3 project URLs are allowed, without paths");
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
serverOrigin: server.origin,
|
|
67
|
+
supabaseUrl: auth.origin,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/** Legacy unbound sessions may be restored ONLY against the original default MCP/supabase1 pair. */
|
|
71
|
+
export function savedSessionMatchesContext(saved, expected) {
|
|
72
|
+
if (saved === undefined) {
|
|
73
|
+
return expected.serverOrigin === DEFAULT_REMOTE_SERVER_URL &&
|
|
74
|
+
expected.supabaseUrl === SUPABASE1_ORIGIN;
|
|
75
|
+
}
|
|
76
|
+
if (!saved || typeof saved !== "object" || Array.isArray(saved))
|
|
77
|
+
return false;
|
|
78
|
+
const previous = saved;
|
|
79
|
+
return previous.serverOrigin === expected.serverOrigin &&
|
|
80
|
+
previous.supabaseUrl === expected.supabaseUrl;
|
|
7
81
|
}
|
|
8
82
|
export function getRemoteDeviceConfigDirectory(home = os.homedir()) {
|
|
9
83
|
const suffix = "";
|
|
10
|
-
// TODO: uncomment
|
|
84
|
+
// TODO: uncomment when staging/dev profiles are fully isolated
|
|
11
85
|
// if (process.env.DC_ENV === "staging") {
|
|
12
|
-
//
|
|
86
|
+
// suffix = "-staging";
|
|
13
87
|
// } else if (process.env.DC_ENV === "development") {
|
|
14
|
-
//
|
|
88
|
+
// suffix = "-dev";
|
|
15
89
|
// }
|
|
16
90
|
return path.join(home, `.desktop-commander-device${suffix}`);
|
|
17
91
|
}
|
|
@@ -82,6 +82,7 @@ class RolloutManager {
|
|
|
82
82
|
throw new Error('No session token');
|
|
83
83
|
const response = await fetch(`${this.serverUrl}/device/rollouts`, {
|
|
84
84
|
headers: { Authorization: `Bearer ${token}` },
|
|
85
|
+
redirect: 'error',
|
|
85
86
|
});
|
|
86
87
|
if (!response.ok)
|
|
87
88
|
throw new Error(`HTTP ${response.status}`);
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = "0.3.
|
|
1
|
+
export declare const VERSION = "0.3.3";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.3.
|
|
1
|
+
export const VERSION = '0.3.3';
|
package/package.json
CHANGED