@mindstone/mcp-server-google-workspace 0.5.11 → 0.5.13
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 +28 -3
- package/dist/connectorRenewalClient.d.ts +162 -0
- package/dist/connectorRenewalClient.js +212 -0
- package/dist/modules/accounts/manager.js +24 -4
- package/dist/modules/accounts/oauth.js +5 -2
- package/dist/modules/accounts/token.d.ts +1 -0
- package/dist/modules/accounts/token.js +18 -8
- package/dist/modules/accounts/unauthorized.d.ts +4 -0
- package/dist/modules/accounts/unauthorized.js +46 -0
- package/dist/modules/calendar/service.js +2 -2
- package/dist/modules/docs/service.d.ts +1 -1
- package/dist/modules/docs/service.js +16 -8
- package/dist/modules/docs/types.d.ts +0 -1
- package/dist/oauth/coordinatedClient.d.ts +14 -0
- package/dist/oauth/coordinatedClient.js +103 -0
- package/dist/resources/compose-email-template.d.ts +1 -1
- package/dist/resources/compose-email-template.js +4 -0
- package/dist/tools/definitions/chat.js +1 -2
- package/dist/tools/definitions/docs.js +4 -8
- package/dist/tools/definitions/drive.js +0 -1
- package/dist/tools/docs-handlers.d.ts +0 -2
- package/dist/tools/docs-handlers.js +0 -2
- package/dist/tools/param-normalizer.js +0 -1
- package/dist/tools/server.js +8 -23
- package/dist/tools/type-guards.d.ts +0 -1
- package/dist/tools/type-guards.js +1 -3
- package/dist/utils/apiError.d.ts +4 -4
- package/dist/utils/apiError.js +7 -6
- package/dist/utils/google-retry.d.ts +6 -0
- package/dist/utils/google-retry.js +18 -1
- package/node_modules/cross-spawn/README.md +19 -3
- package/node_modules/cross-spawn/lib/parse.js +38 -10
- package/node_modules/cross-spawn/lib/util/escape.js +84 -25
- package/node_modules/cross-spawn/lib/util/readShebang.js +8 -2
- package/node_modules/cross-spawn/lib/util/resolveCommand.js +8 -1
- package/node_modules/cross-spawn/package.json +21 -24
- package/package.json +1 -1
|
@@ -125,8 +125,6 @@ interface CopyDocumentTabArgs {
|
|
|
125
125
|
sourceTabId?: string;
|
|
126
126
|
target_tab_id?: string;
|
|
127
127
|
targetTabId?: string;
|
|
128
|
-
overwrite_existing?: boolean;
|
|
129
|
-
overwriteExisting?: boolean;
|
|
130
128
|
new_tab_title?: string;
|
|
131
129
|
newTabTitle?: string;
|
|
132
130
|
new_tab_parent_id?: string;
|
|
@@ -602,7 +602,6 @@ export async function handleCopyDocumentTab(args) {
|
|
|
602
602
|
const documentId = readAliasedString(rawArgs, 'document_id', 'documentId');
|
|
603
603
|
const sourceTabId = readAliasedString(rawArgs, 'source_tab_id', 'sourceTabId');
|
|
604
604
|
const targetTabId = readAliasedString(rawArgs, 'target_tab_id', 'targetTabId');
|
|
605
|
-
const overwriteExisting = readAliasedBoolean(rawArgs, 'overwrite_existing', 'overwriteExisting');
|
|
606
605
|
const newTabTitle = readAliasedString(rawArgs, 'new_tab_title', 'newTabTitle');
|
|
607
606
|
const newTabParentId = readAliasedString(rawArgs, 'new_tab_parent_id', 'newTabParentId');
|
|
608
607
|
const newTabPosition = readAliasedNumber(rawArgs, 'new_tab_position', 'newTabPosition');
|
|
@@ -618,7 +617,6 @@ export async function handleCopyDocumentTab(args) {
|
|
|
618
617
|
documentId: documentId,
|
|
619
618
|
sourceTabId: sourceTabId,
|
|
620
619
|
...(targetTabId !== undefined ? { targetTabId } : {}),
|
|
621
|
-
...(overwriteExisting !== undefined ? { overwriteExisting } : {}),
|
|
622
620
|
...(newTabTitle !== undefined ? { newTabTitle } : {}),
|
|
623
621
|
...(newTabParentId !== undefined ? { newTabParentId } : {}),
|
|
624
622
|
...(newTabPosition !== undefined ? { newTabPosition } : {}),
|
|
@@ -43,7 +43,6 @@ const LEGACY_ALIASES = {
|
|
|
43
43
|
deleteSubTabs: 'delete_sub_tabs',
|
|
44
44
|
sourceTabId: 'source_tab_id',
|
|
45
45
|
targetTabId: 'target_tab_id',
|
|
46
|
-
overwriteExisting: 'overwrite_existing',
|
|
47
46
|
newTabTitle: 'new_tab_title',
|
|
48
47
|
newTabParentId: 'new_tab_parent_id',
|
|
49
48
|
newTabPosition: 'new_tab_position',
|
package/dist/tools/server.js
CHANGED
|
@@ -3,8 +3,9 @@ import { createRequire } from 'node:module';
|
|
|
3
3
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
4
4
|
import { createStdioTransport } from "./stdio-transport.js";
|
|
5
5
|
import logger, { renderLogArg } from '../utils/logger.js';
|
|
6
|
-
import { isGoogleRecoveryExhausted } from '../utils/google-retry.js';
|
|
6
|
+
import { isGoogleRecoveryExhausted, mayHaveWritten } from '../utils/google-retry.js';
|
|
7
7
|
import { GENERIC_NEXT_STEP, classifyGoogleFailure, envelopeErrorDetail, googleFailureNextStep, hasErrorDetails, summariseGoogleError, } from '../utils/apiError.js';
|
|
8
|
+
import { isGoogleAuthFailure } from '../modules/accounts/unauthorized.js';
|
|
8
9
|
import { CallToolRequestSchema, ListToolsRequestSchema, ReadResourceRequestSchema, ErrorCode, McpError } from "@modelcontextprotocol/sdk/types.js";
|
|
9
10
|
const require = createRequire(import.meta.url);
|
|
10
11
|
const pkg = require('../../package.json');
|
|
@@ -757,34 +758,18 @@ export class GSuiteServer {
|
|
|
757
758
|
// the string auth codes checked below).
|
|
758
759
|
const rawCode = error?.code;
|
|
759
760
|
const code = typeof rawCode === 'string' ? rawCode : undefined;
|
|
760
|
-
|
|
761
|
-
? error.response?.status
|
|
762
|
-
?? error.status
|
|
763
|
-
?? error.code
|
|
764
|
-
: undefined;
|
|
765
|
-
const numericStatus = typeof status === 'number'
|
|
766
|
-
? status
|
|
767
|
-
: typeof status === 'string' && /^\d+$/.test(status)
|
|
768
|
-
? Number(status)
|
|
769
|
-
: undefined;
|
|
770
|
-
if (code === 'AUTH_REQUIRED' || code === 'HOST_ORCHESTRATED_AUTH_REQUIRED') {
|
|
761
|
+
if (code === 'AUTH_REQUIRED' || code === 'HOST_ORCHESTRATED_AUTH_REQUIRED' || isGoogleAuthFailure(error)) {
|
|
771
762
|
return {
|
|
772
763
|
status: 'auth_required',
|
|
773
764
|
user_action: { id: 'google.connect_account' },
|
|
774
765
|
agent_action: {
|
|
775
766
|
instruction: "Connect Google Workspace to continue. The user will be redirected to Google's sign-in."
|
|
776
767
|
},
|
|
777
|
-
setupToolName: 'authenticate_workspace_account'
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
status: 'auth_required',
|
|
783
|
-
user_action: { id: 'google.connect_account' },
|
|
784
|
-
agent_action: {
|
|
785
|
-
instruction: "Connect Google Workspace to continue. The user will be redirected to Google's sign-in."
|
|
786
|
-
},
|
|
787
|
-
setupToolName: 'authenticate_workspace_account'
|
|
768
|
+
setupToolName: 'authenticate_workspace_account',
|
|
769
|
+
// The request may already have partly run: say what happened so it is checked, not repeated.
|
|
770
|
+
...(mayHaveWritten(error)
|
|
771
|
+
? errorDetailField(`${errorDetailText(error)} This request may already have partly run; check the result before trying it again.`)
|
|
772
|
+
: {})
|
|
788
773
|
};
|
|
789
774
|
}
|
|
790
775
|
// Connector input checks: the connector's own guidance about the caller's input.
|
|
@@ -654,7 +654,6 @@ export function isCopyDocumentTabArgs(args) {
|
|
|
654
654
|
const documentId = readAliasedArg(rawParams, 'document_id', 'documentId');
|
|
655
655
|
const sourceTabId = readAliasedArg(rawParams, 'source_tab_id', 'sourceTabId');
|
|
656
656
|
const targetTabId = readAliasedArg(rawParams, 'target_tab_id', 'targetTabId');
|
|
657
|
-
const overwriteExisting = readAliasedArg(rawParams, 'overwrite_existing', 'overwriteExisting');
|
|
658
657
|
const newTabTitle = readAliasedArg(rawParams, 'new_tab_title', 'newTabTitle');
|
|
659
658
|
const newTabParentId = readAliasedArg(rawParams, 'new_tab_parent_id', 'newTabParentId');
|
|
660
659
|
const newTabPosition = readAliasedArg(rawParams, 'new_tab_position', 'newTabPosition');
|
|
@@ -663,14 +662,13 @@ export function isCopyDocumentTabArgs(args) {
|
|
|
663
662
|
typeof documentId === 'string' &&
|
|
664
663
|
typeof sourceTabId === 'string' &&
|
|
665
664
|
isOptional(targetTabId, 'string') &&
|
|
666
|
-
isOptional(overwriteExisting, 'boolean') &&
|
|
667
665
|
isOptional(newTabTitle, 'string') &&
|
|
668
666
|
isOptional(newTabParentId, 'string') &&
|
|
669
667
|
isOptional(newTabPosition, 'number');
|
|
670
668
|
}
|
|
671
669
|
export function assertCopyDocumentTabArgs(args) {
|
|
672
670
|
if (!isCopyDocumentTabArgs(args)) {
|
|
673
|
-
throw new Error('Invalid copy document tab parameters. Required: document_id, source_tab_id, and either target_tab_id
|
|
671
|
+
throw new Error('Invalid copy document tab parameters. Required: document_id, source_tab_id, and either target_tab_id or new_tab_title. Optional with new_tab_title: new_tab_parent_id (string), new_tab_position (number)');
|
|
674
672
|
}
|
|
675
673
|
}
|
|
676
674
|
export function isBatchUpdateDocumentArgs(args) {
|
package/dist/utils/apiError.d.ts
CHANGED
|
@@ -90,10 +90,10 @@ export declare function hasErrorDetails(error: unknown): error is {
|
|
|
90
90
|
*/
|
|
91
91
|
export declare function isAuthHandoffError(error: unknown): boolean;
|
|
92
92
|
/**
|
|
93
|
-
* True when the server's `formatErrorResponse` owns this error's tool result:
|
|
94
|
-
* auth hand-off code (the `auth_required` reconnect,
|
|
95
|
-
* `CREDENTIAL_FILE_REFUSED`) or any `AccountError`, whose
|
|
96
|
-
* text. A handler's
|
|
93
|
+
* True when the server's `formatErrorResponse` owns this error's tool result: a
|
|
94
|
+
* wrapped Google auth failure, an auth hand-off code (the `auth_required` reconnect,
|
|
95
|
+
* or a pass-through such as `CREDENTIAL_FILE_REFUSED`) or any `AccountError`, whose
|
|
96
|
+
* `resolution` is connector text. A handler's result builder rethrows these instead of summarising them,
|
|
97
97
|
* so the resolution and the hand-off are written in one place.
|
|
98
98
|
*/
|
|
99
99
|
export declare function isServerFormattedError(error: unknown): boolean;
|
package/dist/utils/apiError.js
CHANGED
|
@@ -3,6 +3,7 @@ import { wrapUntrustedContent } from './untrusted-controls.js';
|
|
|
3
3
|
import { unwrapUntrusted } from './untrusted-content.js';
|
|
4
4
|
import { GOOGLE_RATE_LIMIT_NEXT_STEP, LONG_WINDOW_QUOTA, errorText, isGoogleRecoveryExhausted, isShortWindowRateLimit, preserveGoogleRetryDecision, } from './google-retry.js';
|
|
5
5
|
import { AccountError } from '../modules/accounts/types.js';
|
|
6
|
+
import { isGoogleAuthFailure } from '../modules/accounts/unauthorized.js';
|
|
6
7
|
/**
|
|
7
8
|
* Links `target` to the original thrown value as a non-enumerable `cause`, so
|
|
8
9
|
* status and reason stay reachable (cause-walking readers) without the original
|
|
@@ -267,18 +268,18 @@ export function isAuthHandoffError(error) {
|
|
|
267
268
|
return typeof code === 'string' && AUTH_HANDOFF_CODES.has(code);
|
|
268
269
|
}
|
|
269
270
|
/**
|
|
270
|
-
* True when the server's `formatErrorResponse` owns this error's tool result:
|
|
271
|
-
* auth hand-off code (the `auth_required` reconnect,
|
|
272
|
-
* `CREDENTIAL_FILE_REFUSED`) or any `AccountError`, whose
|
|
273
|
-
* text. A handler's
|
|
271
|
+
* True when the server's `formatErrorResponse` owns this error's tool result: a
|
|
272
|
+
* wrapped Google auth failure, an auth hand-off code (the `auth_required` reconnect,
|
|
273
|
+
* or a pass-through such as `CREDENTIAL_FILE_REFUSED`) or any `AccountError`, whose
|
|
274
|
+
* `resolution` is connector text. A handler's result builder rethrows these instead of summarising them,
|
|
274
275
|
* so the resolution and the hand-off are written in one place.
|
|
275
276
|
*/
|
|
276
277
|
export function isServerFormattedError(error) {
|
|
277
|
-
return isAuthHandoffError(error) || error instanceof AccountError;
|
|
278
|
+
return isAuthHandoffError(error) || error instanceof AccountError || isGoogleAuthFailure(error);
|
|
278
279
|
}
|
|
279
280
|
/** Keep structured recovery decisions out of lossy string-only error results. */
|
|
280
281
|
export function isPassthroughGoogleError(error) {
|
|
281
|
-
return isAuthHandoffError(error) || isGoogleRecoveryExhausted(error) || extractHttpStatus(error) === 429 || isShortWindowRateLimit(error);
|
|
282
|
+
return isAuthHandoffError(error) || isGoogleAuthFailure(error) || isGoogleRecoveryExhausted(error) || extractHttpStatus(error) === 429 || isShortWindowRateLimit(error);
|
|
282
283
|
}
|
|
283
284
|
/**
|
|
284
285
|
* Normalizes any thrown value into an error suitable for a tool handler's outer
|
|
@@ -8,6 +8,12 @@ export declare function rateLimitReason(error: unknown, status: number | undefin
|
|
|
8
8
|
export declare function isShortWindowRateLimit(error: unknown): boolean;
|
|
9
9
|
/** Only this request policy can declare a terminal retry decision. */
|
|
10
10
|
export declare function isGoogleRecoveryExhausted(error: unknown): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Marks a failure from an operation that may already have changed the user's data,
|
|
13
|
+
* so a token renewal never re-runs it (the retry would repeat those writes).
|
|
14
|
+
*/
|
|
15
|
+
export declare function markMayHaveWritten<T extends object>(target: T): T;
|
|
16
|
+
export declare function mayHaveWritten(error: unknown): boolean;
|
|
11
17
|
export declare function preserveGoogleRetryDecision<T extends object>(target: T, cause: unknown): T;
|
|
12
18
|
export declare function createGoogleRetryConfig({ sleep, random, }?: {
|
|
13
19
|
sleep?: (ms: number) => Promise<void>;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { extractHttpStatus } from './apiError.js';
|
|
2
2
|
const TERMINAL_RETRY_DECISION = Symbol.for('google-workspace.terminal-retry-decision');
|
|
3
|
+
const MAY_HAVE_WRITTEN = Symbol.for('google-workspace.may-have-written');
|
|
3
4
|
const RETRY_METHODS = new Set(['GET', 'HEAD', 'PUT', 'OPTIONS', 'DELETE']);
|
|
4
5
|
const EXTENDED_RATE_LIMIT_METHODS = new Set(['GET', 'HEAD', 'OPTIONS']);
|
|
5
6
|
// Google's documented exponential backoff caps at 32–64 seconds. Our 32-second
|
|
@@ -44,18 +45,34 @@ export function isShortWindowRateLimit(error) {
|
|
|
44
45
|
}
|
|
45
46
|
/** Only this request policy can declare a terminal retry decision. */
|
|
46
47
|
export function isGoogleRecoveryExhausted(error) {
|
|
48
|
+
return hasMark(error, TERMINAL_RETRY_DECISION);
|
|
49
|
+
}
|
|
50
|
+
function hasMark(error, mark) {
|
|
47
51
|
const seen = new Set();
|
|
48
52
|
while (typeof error === 'object' && error !== null && !seen.has(error)) {
|
|
49
|
-
if (error[
|
|
53
|
+
if (error[mark] === true)
|
|
50
54
|
return true;
|
|
51
55
|
seen.add(error);
|
|
52
56
|
error = error.cause;
|
|
53
57
|
}
|
|
54
58
|
return false;
|
|
55
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Marks a failure from an operation that may already have changed the user's data,
|
|
62
|
+
* so a token renewal never re-runs it (the retry would repeat those writes).
|
|
63
|
+
*/
|
|
64
|
+
export function markMayHaveWritten(target) {
|
|
65
|
+
Object.defineProperty(target, MAY_HAVE_WRITTEN, { value: true });
|
|
66
|
+
return target;
|
|
67
|
+
}
|
|
68
|
+
export function mayHaveWritten(error) {
|
|
69
|
+
return hasMark(error, MAY_HAVE_WRITTEN);
|
|
70
|
+
}
|
|
56
71
|
export function preserveGoogleRetryDecision(target, cause) {
|
|
57
72
|
if (isGoogleRecoveryExhausted(cause))
|
|
58
73
|
Object.defineProperty(target, TERMINAL_RETRY_DECISION, { value: true });
|
|
74
|
+
if (mayHaveWritten(cause))
|
|
75
|
+
markMayHaveWritten(target);
|
|
59
76
|
return target;
|
|
60
77
|
}
|
|
61
78
|
function retryAfterMs(error) {
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
# cross-spawn
|
|
2
2
|
|
|
3
|
-
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][ci-image]][ci-url]
|
|
3
|
+
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][ci-image]][ci-url]
|
|
4
4
|
|
|
5
5
|
[npm-url]:https://npmjs.org/package/cross-spawn
|
|
6
6
|
[downloads-image]:https://img.shields.io/npm/dm/cross-spawn.svg
|
|
7
7
|
[npm-image]:https://img.shields.io/npm/v/cross-spawn.svg
|
|
8
8
|
[ci-url]:https://github.com/moxystudio/node-cross-spawn/actions/workflows/ci.yaml
|
|
9
9
|
[ci-image]:https://github.com/moxystudio/node-cross-spawn/actions/workflows/ci.yaml/badge.svg
|
|
10
|
-
[appveyor-url]:https://ci.appveyor.com/project/satazor/node-cross-spawn
|
|
11
|
-
[appveyor-image]:https://img.shields.io/appveyor/ci/satazor/node-cross-spawn/master.svg
|
|
12
10
|
|
|
13
11
|
A cross platform solution to node's spawn and spawnSync.
|
|
14
12
|
|
|
@@ -70,6 +68,24 @@ While `cross-spawn` adds support for `options.shell` in node `<v4.8`, all of its
|
|
|
70
68
|
|
|
71
69
|
This mimics the Node.js behavior. More specifically, the command and its arguments will not be automatically escaped nor shebang support will be offered. This is by design because if you are using `options.shell` you are probably targeting a specific platform anyway and you don't want things to get into your way.
|
|
72
70
|
|
|
71
|
+
### Windows argument escaping
|
|
72
|
+
|
|
73
|
+
On Unix systems arguments are passed to a new process as an array of strings, but on Windows they are passed as a single command line string and it is up to the child process to parse it back into an array. Therefore `cross-spawn` and the child process must agree on how the command line is encoded.
|
|
74
|
+
|
|
75
|
+
`cross-spawn` escapes arguments according to the conventions of the Microsoft C runtime, which most programs use, including Node.js, Python, Java and .NET. However, some programs have their own way of parsing the command line, in which case they may see a different array of arguments than the one you passed.
|
|
76
|
+
|
|
77
|
+
Commands that are not executables, such as batch files (`.bat` and `.cmd`), are run through `cmd.exe`, which uses non-standard parsing and is especially vulnerable to malicious input, as it may be used to run arbitrary commands. `cross-spawn` escapes the command and its arguments according to `cmd.exe` rules, similarly to other runtimes such as [Rust](https://doc.rust-lang.org/std/process/index.html#windows-argument-splitting). Still, due to the complexity of `cmd.exe`, there are limitations that no escaping can overcome:
|
|
78
|
+
|
|
79
|
+
- Some characters can't be escaped at all. Line breaks (`\r` and `\n`) can't be escaped and passing them results in an error being thrown.
|
|
80
|
+
- The command must be the name or path of a single program, with its arguments passed in `args`. The command is quoted, so `spawn('npm install')` fails with `ENOENT`, like it does on other platforms, and double quotes in it are ignored, since Windows file names can't contain them.
|
|
81
|
+
- Shell built-ins that run other commands, such as `call` and `start`, pass their arguments to those commands unescaped. Call the program directly instead, e.g. `spawn('tool.bat', args)` instead of `spawn('call', ['tool.bat', ...args])`.
|
|
82
|
+
- Batch files are assumed to forward arguments as received, e.g. with `%*`, `%1` or `"%~1"`. Batch files that use `%~1` unquoted, `call` or enable delayed expansion re-parse their arguments and may be vulnerable to malicious input.
|
|
83
|
+
- Programs called by batch files must follow the Microsoft C runtime conventions. Programs that don't, such as those written in Go or that use `CommandLineToArgvW`, may split arguments containing double quotes differently.
|
|
84
|
+
|
|
85
|
+
Additionally, programs that read their arguments with the legacy ANSI code page instead of Unicode may convert certain Unicode characters into others, such as `"` (U+FF02) into `"`, which changes how arguments are split. This is known as [WorstFit](https://worst.fit) and can't be mitigated by `cross-spawn`.
|
|
86
|
+
|
|
87
|
+
When passing untrusted input on Windows, validate it so that only a safe subset is allowed, and prefer calling executables directly rather than through batch files.
|
|
88
|
+
|
|
73
89
|
### Shebangs support
|
|
74
90
|
|
|
75
91
|
While `cross-spawn` handles shebangs on Windows, its support is limited. More specifically, it just supports `#!/usr/bin/env <program>` where `<program>` must not contain any arguments.
|
|
@@ -7,7 +7,11 @@ const readShebang = require('./util/readShebang');
|
|
|
7
7
|
|
|
8
8
|
const isWin = process.platform === 'win32';
|
|
9
9
|
const isExecutableRegExp = /\.(?:com|exe)$/i;
|
|
10
|
-
const
|
|
10
|
+
const isBatchFileRegExp = /\.(?:bat|cmd)$/i;
|
|
11
|
+
// Windows strips trailing dots and spaces from file names, so `cmd.exe` runs `foo.bat ` as `foo.bat`
|
|
12
|
+
// Other trailing whitespace, such as tabs, is treated the same way to be safe
|
|
13
|
+
// See CVE-2024-36138 for the equivalent Node.js vulnerability
|
|
14
|
+
const trailingDotsAndSpacesRegExp = /[.\s]+$/;
|
|
11
15
|
|
|
12
16
|
function detectShebang(parsed) {
|
|
13
17
|
parsed.file = resolveCommand(parsed);
|
|
@@ -24,11 +28,29 @@ function detectShebang(parsed) {
|
|
|
24
28
|
return parsed.file;
|
|
25
29
|
}
|
|
26
30
|
|
|
31
|
+
function isBatchFile(parsed, commandFile) {
|
|
32
|
+
if (commandFile) {
|
|
33
|
+
return isBatchFileRegExp.test(commandFile.replace(trailingDotsAndSpacesRegExp, ''));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// The command couldn't be resolved, but cmd.exe might still find a batch file after stripping
|
|
37
|
+
// trailing dots and spaces, e.g.: `foo ` might run `foo.bat`
|
|
38
|
+
const command = parsed.command.replace(trailingDotsAndSpacesRegExp, '');
|
|
39
|
+
const file = command !== parsed.command ? resolveCommand(Object.assign({}, parsed, { command })) : null;
|
|
40
|
+
|
|
41
|
+
return isBatchFileRegExp.test(file || command);
|
|
42
|
+
}
|
|
43
|
+
|
|
27
44
|
function parseNonShell(parsed) {
|
|
28
45
|
if (!isWin) {
|
|
29
46
|
return parsed;
|
|
30
47
|
}
|
|
31
48
|
|
|
49
|
+
// Windows file names can't contain double quotes, and cmd.exe ignores them in command names, so remove them
|
|
50
|
+
// Otherwise a quoted command, e.g. `"foo.bat"`, wouldn't be found and could be run as a batch file without
|
|
51
|
+
// being detected as one
|
|
52
|
+
parsed.command = parsed.command.replace(/"/g, '');
|
|
53
|
+
|
|
32
54
|
// Detect & add support for shebangs
|
|
33
55
|
const commandFile = detectShebang(parsed);
|
|
34
56
|
|
|
@@ -38,23 +60,29 @@ function parseNonShell(parsed) {
|
|
|
38
60
|
// If a shell is required, use cmd.exe and take care of escaping everything correctly
|
|
39
61
|
// Note that `forceShell` is an hidden option used only in tests
|
|
40
62
|
if (parsed.options.forceShell || needsShell) {
|
|
41
|
-
// Need to double escape meta chars if the command is a cmd-shim located in `node_modules/.bin/`
|
|
42
|
-
// The cmd-shim simply calls execute the package bin file with NodeJS, proxying any argument
|
|
43
|
-
// Because the escape of metachars with ^ gets interpreted when the cmd.exe is first called,
|
|
44
|
-
// we need to double escape them
|
|
45
|
-
const needsDoubleEscapeMetaChars = isCmdShimRegExp.test(commandFile);
|
|
46
|
-
|
|
47
63
|
// Normalize posix paths into OS compatible paths (e.g.: foo/bar -> foo\bar)
|
|
48
64
|
// This is necessary otherwise it will always fail with ENOENT in those cases
|
|
49
65
|
parsed.command = path.normalize(parsed.command);
|
|
50
66
|
|
|
51
67
|
// Escape command & arguments
|
|
52
|
-
|
|
53
|
-
|
|
68
|
+
// Batch files need a different escaping strategy, see `lib/util/escape.js` for more information
|
|
69
|
+
if (isBatchFile(parsed, commandFile)) {
|
|
70
|
+
// Run the batch file by its resolved path, if it was found, otherwise as written
|
|
71
|
+
// When a batch file is run through a quoted name found in PATH, e.g. `"gradle"`, cmd.exe sets `%~dp0` to
|
|
72
|
+
// the current directory instead of the batch file's directory, which breaks batch files that use it to
|
|
73
|
+
// find their own files, such as Gradle's
|
|
74
|
+
parsed.command = escape.batchCommand(commandFile ? path.normalize(commandFile) : parsed.command);
|
|
75
|
+
parsed.args = parsed.args.map((arg) => escape.batchArgument(arg));
|
|
76
|
+
} else {
|
|
77
|
+
parsed.command = escape.command(parsed.command);
|
|
78
|
+
parsed.args = parsed.args.map((arg) => escape.argument(arg));
|
|
79
|
+
}
|
|
54
80
|
|
|
55
81
|
const shellCommand = [parsed.command].concat(parsed.args).join(' ');
|
|
56
82
|
|
|
57
|
-
|
|
83
|
+
// Enable command extensions with /e:on, needed by the %cd:~,% trick, as they might be disabled in the registry
|
|
84
|
+
// Disable delayed expansion with /v:off, otherwise !VAR! would be expanded if enabled in the registry
|
|
85
|
+
parsed.args = ['/d', '/e:on', '/v:off', '/s', '/c', `"${shellCommand}"`];
|
|
58
86
|
parsed.command = process.env.comspec || 'cmd.exe';
|
|
59
87
|
parsed.options.windowsVerbatimArguments = true; // Tell node's spawn that the arguments are already escaped
|
|
60
88
|
}
|
|
@@ -1,47 +1,106 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// See http://www.robvanderwoude.com/escapechars.php
|
|
4
|
-
|
|
4
|
+
// Note that % is not included because escaping it with ^ doesn't prevent %VAR% expansion if
|
|
5
|
+
// a variable named `VAR^` exists, see `escapePercent()` below
|
|
6
|
+
const metaCharsRegExp = /([()\][!^"`<>&|;, *?])/g;
|
|
7
|
+
|
|
8
|
+
// cmd.exe treats CR and LF as command separators and there's no way to escape them
|
|
9
|
+
// Allowing them would enable command injection, so we reject them instead
|
|
10
|
+
const lineBreakRegExp = /[\r\n]/;
|
|
11
|
+
|
|
12
|
+
function assertNoLineBreaks(arg) {
|
|
13
|
+
if (lineBreakRegExp.test(arg)) {
|
|
14
|
+
throw new TypeError('The command and its arguments cannot contain line breaks when executed through cmd.exe');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// %VAR% is expanded even inside double quotes or when escaped with ^, so we replace % with %%cd:~,%
|
|
19
|
+
// The latter is an empty substring of the %cd% dynamic variable, which interrupts the expansion
|
|
20
|
+
// This must be done after escaping meta chars, as the replacement must not be escaped itself
|
|
21
|
+
function escapePercent(arg) {
|
|
22
|
+
return arg.replace(/%/g, '%%cd:~,%');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Quotes an argument so that it's parsed back verbatim by programs following the Microsoft C runtime rules,
|
|
26
|
+
// where backslashes are only special when they precede a double quote
|
|
27
|
+
// Algorithm below is based on https://qntm.org/cmd, but implemented with a loop instead of regular expressions,
|
|
28
|
+
// which runs in linear time and correctly handles any number of backslashes
|
|
29
|
+
function quoteArgument(arg, escapedQuote) {
|
|
30
|
+
let quoted = '"';
|
|
31
|
+
let backslashes = 0;
|
|
32
|
+
|
|
33
|
+
for (const char of arg) {
|
|
34
|
+
if (char === '\\') {
|
|
35
|
+
backslashes += 1;
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (char === '"') {
|
|
40
|
+
// Sequence of backslashes followed by a double quote:
|
|
41
|
+
// double up all the backslashes and escape the double quote
|
|
42
|
+
quoted += '\\'.repeat(backslashes * 2) + escapedQuote;
|
|
43
|
+
} else {
|
|
44
|
+
// All other backslashes occur literally
|
|
45
|
+
quoted += '\\'.repeat(backslashes) + char;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
backslashes = 0;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Sequence of backslashes followed by the end of the string (which will become a double quote):
|
|
52
|
+
// double up all the backslashes
|
|
53
|
+
return `${quoted}${'\\'.repeat(backslashes * 2)}"`;
|
|
54
|
+
}
|
|
5
55
|
|
|
6
56
|
function escapeCommand(arg) {
|
|
7
|
-
|
|
8
|
-
arg = arg.replace(metaCharsRegExp, '^$1');
|
|
57
|
+
assertNoLineBreaks(arg);
|
|
9
58
|
|
|
10
|
-
|
|
59
|
+
// Quote the command, so that cmd.exe treats it as a single name
|
|
60
|
+
// Otherwise cmd.exe would end the name at the first space, tab, `,`, `;` or `=`, even if escaped with ^,
|
|
61
|
+
// and run whatever it finds with that name, e.g. a batch file, with the rest as extra arguments
|
|
62
|
+
// Command names can't contain double quotes, see `parse.js`, and meta chars are literal inside quotes
|
|
63
|
+
return `"${escapePercent(arg)}"`;
|
|
11
64
|
}
|
|
12
65
|
|
|
13
|
-
function escapeArgument(arg
|
|
66
|
+
function escapeArgument(arg) {
|
|
14
67
|
// Convert to string
|
|
15
68
|
arg = `${arg}`;
|
|
16
69
|
|
|
17
|
-
|
|
18
|
-
// It's slightly altered to disable JS backtracking to avoid hanging on specially crafted input
|
|
19
|
-
// Please see https://github.com/moxystudio/node-cross-spawn/pull/160 for more information
|
|
70
|
+
assertNoLineBreaks(arg);
|
|
20
71
|
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
arg = arg.replace(/(?=(\\+?)?)\1"/g, '$1$1\\"');
|
|
72
|
+
// Quote the whole thing, escaping double quotes with a backslash
|
|
73
|
+
arg = quoteArgument(arg, '\\"');
|
|
24
74
|
|
|
25
|
-
//
|
|
26
|
-
|
|
27
|
-
// double up all the backslashes
|
|
28
|
-
arg = arg.replace(/(?=(\\+?)?)\1$/, '$1$1');
|
|
75
|
+
// Escape meta chars and neutralize %VAR% expansion
|
|
76
|
+
arg = escapePercent(arg.replace(metaCharsRegExp, '^$1'));
|
|
29
77
|
|
|
30
|
-
|
|
78
|
+
return arg;
|
|
79
|
+
}
|
|
31
80
|
|
|
32
|
-
|
|
33
|
-
|
|
81
|
+
// Batch files (.bat and .cmd) usually proxy their arguments with %* or read them with "%~1",
|
|
82
|
+
// which makes cmd.exe parse them a second time, after ^ escapes have already been consumed
|
|
83
|
+
// Instead of escaping meta chars with ^, we rely on double quotes, which keep meta chars literal in both parses
|
|
84
|
+
// This is the same approach taken by Rust after BatBadBut (CVE-2024-24576)
|
|
34
85
|
|
|
35
|
-
|
|
36
|
-
arg
|
|
86
|
+
function escapeBatchCommand(arg) {
|
|
87
|
+
assertNoLineBreaks(arg);
|
|
37
88
|
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
89
|
+
// Command names can't contain double quotes, so we just need to quote it
|
|
90
|
+
return `"${escapePercent(arg)}"`;
|
|
91
|
+
}
|
|
42
92
|
|
|
43
|
-
|
|
93
|
+
function escapeBatchArgument(arg) {
|
|
94
|
+
// Convert to string
|
|
95
|
+
arg = `${arg}`;
|
|
96
|
+
|
|
97
|
+
assertNoLineBreaks(arg);
|
|
98
|
+
|
|
99
|
+
// Quote the whole thing, escaping double quotes by doubling them, which keeps cmd.exe's quoting balanced
|
|
100
|
+
return escapePercent(quoteArgument(arg, '""'));
|
|
44
101
|
}
|
|
45
102
|
|
|
46
103
|
module.exports.command = escapeCommand;
|
|
47
104
|
module.exports.argument = escapeArgument;
|
|
105
|
+
module.exports.batchCommand = escapeBatchCommand;
|
|
106
|
+
module.exports.batchArgument = escapeBatchArgument;
|
|
@@ -13,8 +13,14 @@ function readShebang(command) {
|
|
|
13
13
|
try {
|
|
14
14
|
fd = fs.openSync(command, 'r');
|
|
15
15
|
fs.readSync(fd, buffer, 0, size, 0);
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
} catch (e) { /* Empty */ } finally {
|
|
17
|
+
// Always close the file descriptor, even if reading failed
|
|
18
|
+
if (fd !== undefined) {
|
|
19
|
+
try {
|
|
20
|
+
fs.closeSync(fd);
|
|
21
|
+
} catch (e) { /* Empty */ }
|
|
22
|
+
}
|
|
23
|
+
}
|
|
18
24
|
|
|
19
25
|
// Attempt to extract shebang (null is returned if not a shebang)
|
|
20
26
|
return shebangCommand(buffer.toString());
|
|
@@ -4,6 +4,13 @@ const path = require('path');
|
|
|
4
4
|
const which = require('which');
|
|
5
5
|
const getPathKey = require('path-key');
|
|
6
6
|
|
|
7
|
+
// Returns the child's PATHEXT, which cmd.exe will use to find the command
|
|
8
|
+
function getPathExt(env) {
|
|
9
|
+
const key = Object.keys(env).find((name) => name.toUpperCase() === 'PATHEXT');
|
|
10
|
+
|
|
11
|
+
return key ? env[key] : undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
7
14
|
function resolveCommandAttempt(parsed, withoutPathExt) {
|
|
8
15
|
const env = parsed.options.env || process.env;
|
|
9
16
|
const cwd = process.cwd();
|
|
@@ -26,7 +33,7 @@ function resolveCommandAttempt(parsed, withoutPathExt) {
|
|
|
26
33
|
try {
|
|
27
34
|
resolved = which.sync(parsed.command, {
|
|
28
35
|
path: env[getPathKey({ env })],
|
|
29
|
-
pathExt: withoutPathExt ? path.delimiter :
|
|
36
|
+
pathExt: withoutPathExt ? path.delimiter : getPathExt(env),
|
|
30
37
|
});
|
|
31
38
|
} catch (e) {
|
|
32
39
|
/* Empty */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cross-spawn",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.2",
|
|
4
4
|
"description": "Cross platform child_process#spawn and child_process#spawnSync",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"spawn",
|
|
@@ -21,26 +21,26 @@
|
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"main": "index.js",
|
|
23
23
|
"files": [
|
|
24
|
-
"lib"
|
|
24
|
+
"lib",
|
|
25
|
+
"!lib/**/*.test.js"
|
|
25
26
|
],
|
|
26
27
|
"scripts": {
|
|
27
28
|
"lint": "eslint .",
|
|
28
29
|
"test": "jest --env node --coverage",
|
|
29
30
|
"prerelease": "npm t && npm run lint",
|
|
30
31
|
"release": "standard-version",
|
|
31
|
-
"postrelease": "git push --follow-tags origin HEAD && npm publish"
|
|
32
|
+
"postrelease": "git push --follow-tags origin HEAD && npm publish",
|
|
33
|
+
"prepare": "husky"
|
|
32
34
|
},
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
|
|
35
|
+
"jest": {
|
|
36
|
+
"collectCoverageFrom": [
|
|
37
|
+
"index.js",
|
|
38
|
+
"lib/**/*.js",
|
|
39
|
+
"!lib/**/*.test.js"
|
|
40
|
+
]
|
|
38
41
|
},
|
|
39
42
|
"lint-staged": {
|
|
40
|
-
"*.js":
|
|
41
|
-
"eslint --fix",
|
|
42
|
-
"git add"
|
|
43
|
-
]
|
|
43
|
+
"*.js": "eslint --fix"
|
|
44
44
|
},
|
|
45
45
|
"commitlint": {
|
|
46
46
|
"extends": [
|
|
@@ -53,18 +53,15 @@
|
|
|
53
53
|
"which": "^2.0.1"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@commitlint/cli": "^
|
|
57
|
-
"@commitlint/config-conventional": "^
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"lint-staged": "^9.2.5",
|
|
66
|
-
"mkdirp": "^0.5.1",
|
|
67
|
-
"rimraf": "^3.0.0",
|
|
56
|
+
"@commitlint/cli": "^20.5.3",
|
|
57
|
+
"@commitlint/config-conventional": "^20.5.3",
|
|
58
|
+
"@eslint/js": "^9.39.5",
|
|
59
|
+
"eslint": "^9.39.5",
|
|
60
|
+
"eslint-plugin-jest": "^28.14.0",
|
|
61
|
+
"globals": "^16.5.0",
|
|
62
|
+
"husky": "^9.1.7",
|
|
63
|
+
"jest": "^30.5.2",
|
|
64
|
+
"lint-staged": "^15.5.2",
|
|
68
65
|
"standard-version": "^9.5.0"
|
|
69
66
|
},
|
|
70
67
|
"engines": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mindstone/mcp-server-google-workspace",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.13",
|
|
4
4
|
"mcpName": "io.github.mindstone/mcp-server-google-workspace",
|
|
5
5
|
"description": "Google Workspace MCP server for Gmail, Calendar, Drive, Docs, Sheets, Slides, Contacts, Tasks, and Forms",
|
|
6
6
|
"license": "FSL-1.1-MIT",
|