@mindstone/mcp-server-google-workspace 0.5.12 → 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.
Files changed (35) hide show
  1. package/README.md +28 -3
  2. package/dist/connectorRenewalClient.d.ts +162 -0
  3. package/dist/connectorRenewalClient.js +212 -0
  4. package/dist/modules/accounts/manager.js +24 -4
  5. package/dist/modules/accounts/oauth.js +5 -2
  6. package/dist/modules/accounts/token.d.ts +1 -0
  7. package/dist/modules/accounts/token.js +18 -8
  8. package/dist/modules/accounts/unauthorized.d.ts +4 -0
  9. package/dist/modules/accounts/unauthorized.js +46 -0
  10. package/dist/modules/calendar/service.js +2 -2
  11. package/dist/modules/docs/service.d.ts +1 -1
  12. package/dist/modules/docs/service.js +16 -8
  13. package/dist/modules/docs/types.d.ts +0 -1
  14. package/dist/oauth/coordinatedClient.d.ts +14 -0
  15. package/dist/oauth/coordinatedClient.js +103 -0
  16. package/dist/tools/definitions/chat.js +1 -2
  17. package/dist/tools/definitions/docs.js +4 -8
  18. package/dist/tools/definitions/drive.js +0 -1
  19. package/dist/tools/docs-handlers.d.ts +0 -2
  20. package/dist/tools/docs-handlers.js +0 -2
  21. package/dist/tools/param-normalizer.js +0 -1
  22. package/dist/tools/server.js +8 -23
  23. package/dist/tools/type-guards.d.ts +0 -1
  24. package/dist/tools/type-guards.js +1 -3
  25. package/dist/utils/apiError.d.ts +4 -4
  26. package/dist/utils/apiError.js +7 -6
  27. package/dist/utils/google-retry.d.ts +6 -0
  28. package/dist/utils/google-retry.js +18 -1
  29. package/node_modules/cross-spawn/README.md +19 -3
  30. package/node_modules/cross-spawn/lib/parse.js +38 -10
  31. package/node_modules/cross-spawn/lib/util/escape.js +84 -25
  32. package/node_modules/cross-spawn/lib/util/readShebang.js +8 -2
  33. package/node_modules/cross-spawn/lib/util/resolveCommand.js +8 -1
  34. package/node_modules/cross-spawn/package.json +21 -24
  35. package/package.json +1 -1
@@ -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 isCmdShimRegExp = /node_modules[\\/].bin[\\/][^\\/]+\.cmd$/i;
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
- parsed.command = escape.command(parsed.command);
53
- parsed.args = parsed.args.map((arg) => escape.argument(arg, needsDoubleEscapeMetaChars));
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
- parsed.args = ['/d', '/s', '/c', `"${shellCommand}"`];
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
- const metaCharsRegExp = /([()\][%!^"`<>&|;, *?])/g;
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
- // Escape meta chars
8
- arg = arg.replace(metaCharsRegExp, '^$1');
57
+ assertNoLineBreaks(arg);
9
58
 
10
- return arg;
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, doubleEscapeMetaChars) {
66
+ function escapeArgument(arg) {
14
67
  // Convert to string
15
68
  arg = `${arg}`;
16
69
 
17
- // Algorithm below is based on https://qntm.org/cmd
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
- // Sequence of backslashes followed by a double quote:
22
- // double up all the backslashes and escape the double quote
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
- // Sequence of backslashes followed by the end of the string
26
- // (which will become a double quote later):
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
- // All other backslashes occur literally
78
+ return arg;
79
+ }
31
80
 
32
- // Quote the whole thing:
33
- arg = `"${arg}"`;
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
- // Escape meta chars
36
- arg = arg.replace(metaCharsRegExp, '^$1');
86
+ function escapeBatchCommand(arg) {
87
+ assertNoLineBreaks(arg);
37
88
 
38
- // Double escape meta chars if necessary
39
- if (doubleEscapeMetaChars) {
40
- arg = arg.replace(metaCharsRegExp, '^$1');
41
- }
89
+ // Command names can't contain double quotes, so we just need to quote it
90
+ return `"${escapePercent(arg)}"`;
91
+ }
42
92
 
43
- return arg;
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
- fs.closeSync(fd);
17
- } catch (e) { /* Empty */ }
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 : undefined,
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.0.6",
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
- "husky": {
34
- "hooks": {
35
- "commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
36
- "pre-commit": "lint-staged"
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": "^8.1.0",
57
- "@commitlint/config-conventional": "^8.1.0",
58
- "babel-core": "^6.26.3",
59
- "babel-jest": "^24.9.0",
60
- "babel-preset-moxy": "^3.1.0",
61
- "eslint": "^5.16.0",
62
- "eslint-config-moxy": "^7.1.0",
63
- "husky": "^3.0.5",
64
- "jest": "^24.9.0",
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.12",
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",