@codacy/codacy-mcp 0.6.19 → 0.6.21

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.
@@ -1,5 +1,6 @@
1
1
  export const CODACY_FOLDER_NAME = '.codacy';
2
2
  import { exec } from 'child_process';
3
+ import * as path from 'path';
3
4
  // Set a larger buffer size (10MB)
4
5
  const MAX_BUFFER_SIZE = 1024 * 1024 * 10;
5
6
  export class CodacyCli {
@@ -22,16 +23,50 @@ export class CodacyCli {
22
23
  setCliCommand(command) {
23
24
  this._cliCommand = command;
24
25
  }
26
+ isPathSafe(filePath) {
27
+ // Reject null bytes (always a security risk)
28
+ if (filePath.includes('\0')) {
29
+ return false;
30
+ }
31
+ // Reject all control characters (including newline, tab, carriage return)
32
+ // as they are very unusual for file names
33
+ // eslint-disable-next-line no-control-regex -- Intentionally checking for control chars to reject them for security
34
+ const hasUnsafeControlChars = /[\x00-\x1F\x7F]/.test(filePath);
35
+ if (hasUnsafeControlChars) {
36
+ return false;
37
+ }
38
+ // Resolve the path to check for path traversal attempts
39
+ const resolvedPath = path.resolve(this.rootPath, filePath);
40
+ const normalizedRoot = path.normalize(this.rootPath);
41
+ // Check if the resolved path is within the workspace
42
+ if (!resolvedPath.startsWith(normalizedRoot)) {
43
+ return false;
44
+ }
45
+ return true;
46
+ }
25
47
  preparePathForExec(path) {
26
- return path;
48
+ // Validate path security before escaping
49
+ if (!this.isPathSafe(path)) {
50
+ throw new Error(`Unsafe file path rejected: ${path}`);
51
+ }
52
+ // Escape special characters for shell execution
53
+ return path.replace(/([\s'"\\;&|`$()[\]{}*?~<>])/g, '\\$1');
27
54
  }
28
55
  execAsync(command, args) {
29
56
  // stringyfy the args
30
57
  const argsString = Object.entries(args || {})
31
- .map(([key, value]) => `--${key} ${value}`)
58
+ .map(([key, value]) => {
59
+ // Validate argument key (should be alphanumeric and hyphens only)
60
+ if (!/^[a-zA-Z0-9-]+$/.test(key)) {
61
+ throw new Error(`Invalid argument key: ${key}`);
62
+ }
63
+ // Escape the value to prevent injection
64
+ const escapedValue = value.replace(/([\s'"\\;&|`$()[\]{}*?~<>])/g, '\\$1');
65
+ return `--${key} ${escapedValue}`;
66
+ })
32
67
  .join(' ');
33
- // Add the args to the command and remove any shell metacharacters
34
- const cmd = `${command} ${argsString}`.trim().replace(/[;&|`$]/g, '');
68
+ // Build the command - no need to strip characters since we've already escaped them properly
69
+ const cmd = `${command} ${argsString}`.trim();
35
70
  return new Promise((resolve, reject) => {
36
71
  exec(cmd, {
37
72
  cwd: this.rootPath,
@@ -7,23 +7,37 @@ export class WinWSLCodacyCli extends MacCodacyCli {
7
7
  super(winRootPath, provider, organization, repository);
8
8
  }
9
9
  static toWSLPath(path) {
10
- // Convert Windows path to WSL path
11
- // Example: C:\Users\user\project -> /mnt/c/Users/user/project
12
- const wslPath = path.replace(/\\/g, '/').replace(/^([a-zA-Z]):/, '/mnt/$1');
10
+ // First, remove outer quotes if present
11
+ const cleanPath = path.replace(/^["']|["']$/g, '');
12
+ // Convert backslashes to slashes and handle drive letter
13
+ const wslPath = cleanPath
14
+ .replace(/\\/g, '/')
15
+ .replace(/^([a-zA-Z]):/, (match, letter) => `/mnt/${letter.toLowerCase()}`);
13
16
  return wslPath;
14
17
  }
15
18
  static fromWSLPath(path) {
16
19
  // Convert WSL path to Windows path
17
20
  // Example: /mnt/c/Users/user/project -> C:\Users\user\project
18
- const windowsPath = path.replace(/^\/mnt\/([a-zA-Z])/, '$1:').replace(/\//g, '\\');
21
+ const windowsPath = path
22
+ .replace(/^'\/mnt\/([a-zA-Z])/, (match, letter) => `'${letter.toUpperCase()}:`)
23
+ .replace(/^\/mnt\/([a-zA-Z])/, (match, letter) => `${letter.toUpperCase()}:`)
24
+ .replace(/\//g, '\\');
19
25
  return windowsPath;
20
26
  }
21
27
  preparePathForExec(path) {
22
- // Convert the path to WSL format
23
- return WinWSLCodacyCli.toWSLPath(path);
28
+ // Convert WSL path to Windows format for validation
29
+ const winFilePath = path.startsWith('/mnt/') ? WinWSLCodacyCli.fromWSLPath(path) : path;
30
+ // Validate path security before escaping
31
+ if (!this.isPathSafe(winFilePath)) {
32
+ throw new Error(`Unsafe file path rejected: ${winFilePath}`);
33
+ }
34
+ // Convert to WSL format and escape special characters
35
+ const wslPath = WinWSLCodacyCli.toWSLPath(winFilePath);
36
+ const escapedPath = wslPath.replace(/([\s'"\\;&|`$()[\]{}*?~<>])/g, '\\$1');
37
+ return `'${escapedPath}'`;
24
38
  }
25
39
  async execAsync(command, args) {
26
- return await super.execAsync(`wsl ${command}`, args);
40
+ return await super.execAsync(`wsl bash -c "${command}"`, args);
27
41
  }
28
42
  getCliCommand() {
29
43
  return WinWSLCodacyCli.toWSLPath(super.getCliCommand());
@@ -3,7 +3,7 @@ export const cliAnalyzeHandler = async (args) => {
3
3
  const cli = await Cli.get(args);
4
4
  try {
5
5
  const results = await cli.analyze({
6
- file: args.file,
6
+ file: `'${args.file}'`,
7
7
  tool: args.tool,
8
8
  });
9
9
  // clean up the results to remove superfluous information
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codacy/codacy-mcp",
3
- "version": "0.6.19",
3
+ "version": "0.6.21",
4
4
  "mcpName": "io.github.codacy/codacy-mcp-server",
5
5
  "description": "Codacy MCP server",
6
6
  "license": "MIT",