@heroku/mcp-server 1.2.6 → 1.2.8

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,5 @@
1
1
  import zlib from 'node:zlib';
2
- import { readFile, stat, readdir } from 'node:fs/promises';
2
+ import fs from 'node:fs/promises';
3
3
  import path from 'node:path';
4
4
  import { execSync } from 'node:child_process';
5
5
  import * as tar from 'tar-stream';
@@ -33,7 +33,7 @@ export async function packSources(root, additionalContents = []) {
33
33
  pack.pipe(gzip);
34
34
  for (const file of files) {
35
35
  try {
36
- const content = await readFile(file);
36
+ const content = await fs.readFile(file);
37
37
  const relativePath = path.relative(root, file);
38
38
  pack.entry({ name: relativePath }, Buffer.from(content));
39
39
  }
@@ -66,14 +66,23 @@ export async function getSourceFilePaths(root) {
66
66
  result = './';
67
67
  }
68
68
  const paths = result.split('\n').filter((file) => Boolean(file));
69
+ const realRoot = await fs.realpath(root);
69
70
  const allFiles = [];
70
71
  for (const p of paths) {
71
72
  const fullPath = path.join(root, p);
72
73
  try {
73
- const stats = await stat(fullPath);
74
- if (stats.isDirectory()) {
74
+ const stats = await fs.lstat(fullPath);
75
+ // Never follow a symlink whose target escapes the workspace root: doing so
76
+ // would read files outside the deploy source and place them in the uploaded
77
+ // tarball (W-23510157, CWE-59).
78
+ if (stats.isSymbolicLink() && (await escapesRoot(realRoot, fullPath))) {
79
+ continue;
80
+ }
81
+ // Resolve the (possibly in-root symlinked) entry to decide file vs. directory.
82
+ const resolved = await fs.stat(fullPath);
83
+ if (resolved.isDirectory()) {
75
84
  // Recursively get all files in directory
76
- const files = await walkDirectory(fullPath);
85
+ const files = await walkDirectory(fullPath, realRoot);
77
86
  allFiles.push(...files);
78
87
  }
79
88
  else {
@@ -86,20 +95,47 @@ export async function getSourceFilePaths(root) {
86
95
  }
87
96
  return allFiles;
88
97
  }
98
+ /**
99
+ * Determines whether a path's real (symlink-resolved) location escapes the
100
+ * workspace root. Following a repository symlink whose target lives outside the
101
+ * selected root would let a malicious repository smuggle unrelated files from the
102
+ * developer's machine into the uploaded source archive (W-23510157, CWE-59).
103
+ *
104
+ * @param realRoot The canonical (realpath-resolved) workspace root
105
+ * @param candidate The path whose resolved target should be checked
106
+ * @returns true when the resolved target is outside realRoot or cannot be resolved
107
+ */
108
+ async function escapesRoot(realRoot, candidate) {
109
+ try {
110
+ const realCandidate = await fs.realpath(candidate);
111
+ const relative = path.relative(realRoot, realCandidate);
112
+ return relative.startsWith('..') || path.isAbsolute(relative);
113
+ }
114
+ catch {
115
+ // A dangling or otherwise unresolvable link is excluded rather than read.
116
+ return true;
117
+ }
118
+ }
89
119
  /**
90
120
  * Walks a directory and returns all file paths
91
121
  *
92
122
  * @param dir The directory to walk
123
+ * @param realRoot The canonical (realpath-resolved) workspace root used to reject
124
+ * symlinks that resolve outside of it (W-23510157, CWE-59)
93
125
  * @returns A promise that resolves to an array of file paths
94
126
  */
95
- async function walkDirectory(dir) {
127
+ async function walkDirectory(dir, realRoot) {
96
128
  const files = [];
97
129
  try {
98
- const entries = await readdir(dir, { withFileTypes: true });
130
+ const entries = await fs.readdir(dir, { withFileTypes: true });
99
131
  for (const entry of entries) {
100
132
  const fullPath = path.join(dir, entry.name);
133
+ // Never follow a symlink whose target escapes the workspace root.
134
+ if (entry.isSymbolicLink() && (await escapesRoot(realRoot, fullPath))) {
135
+ continue;
136
+ }
101
137
  if (entry.isDirectory()) {
102
- const subFiles = await walkDirectory(fullPath);
138
+ const subFiles = await walkDirectory(fullPath, realRoot);
103
139
  files.push(...subFiles);
104
140
  }
105
141
  else {
@@ -1,6 +1,7 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import path from 'node:path';
3
3
  import os from 'node:os';
4
+ import zlib from 'node:zlib';
4
5
  import { execSync } from 'node:child_process';
5
6
  import { expect } from 'chai';
6
7
  import { packSources, getSourceFilePaths } from './tarball.js';
@@ -95,4 +96,55 @@ describe('tarball utils', () => {
95
96
  expect(result).to.include(path.join(tempDir, 'file3.txt'));
96
97
  });
97
98
  });
99
+ // Regression tests for W-23510157 (CWE-59): a tracked/untracked symlink whose
100
+ // target lives outside the workspace root must never have its target bytes
101
+ // read into the uploaded source archive.
102
+ describe('symlink handling (W-23510157)', () => {
103
+ const SECRET = 'SUPER-SECRET-EXTERNAL-CANARY';
104
+ let outsideDir;
105
+ let secretPath;
106
+ beforeEach(async () => {
107
+ // A file that lives entirely outside the workspace root.
108
+ outsideDir = await fs.mkdtemp(path.join(os.tmpdir(), 'outside-workspace-'));
109
+ secretPath = path.join(outsideDir, 'secret.txt');
110
+ await fs.writeFile(secretPath, SECRET);
111
+ });
112
+ afterEach(async () => {
113
+ await fs.rm(outsideDir, { recursive: true, force: true });
114
+ });
115
+ it('excludes symlinks whose target is outside the workspace root (walk path)', async () => {
116
+ await createTempFile('legit.txt', 'legit content');
117
+ await fs.symlink(secretPath, path.join(tempDir, 'evil-link.txt'));
118
+ const result = await getSourceFilePaths(tempDir);
119
+ expect(result).to.include(path.join(tempDir, 'legit.txt'));
120
+ expect(result).to.not.include(path.join(tempDir, 'evil-link.txt'));
121
+ });
122
+ it('does not pack the contents of an out-of-root symlink target', async () => {
123
+ await createTempFile('legit.txt', 'legit content');
124
+ await fs.symlink(secretPath, path.join(tempDir, 'evil-link.txt'));
125
+ const tarball = await packSources(tempDir);
126
+ const unzipped = zlib.gunzipSync(tarball).toString('binary');
127
+ expect(unzipped).to.include('legit content');
128
+ expect(unzipped).to.not.include(SECRET);
129
+ });
130
+ it('excludes tracked symlinks that escape root via the git ls-files path', async () => {
131
+ execSync('git init -q', { cwd: tempDir });
132
+ await createTempFile('legit.txt', 'legit content');
133
+ // `git ls-files -o` lists untracked, non-ignored files, so no commit is required
134
+ // for the symlink path to reach getSourceFilePaths().
135
+ await fs.symlink(secretPath, path.join(tempDir, 'evil-link.txt'));
136
+ const result = await getSourceFilePaths(tempDir);
137
+ const tarball = await packSources(tempDir);
138
+ const unzipped = zlib.gunzipSync(tarball).toString('binary');
139
+ expect(result).to.not.include(path.join(tempDir, 'evil-link.txt'));
140
+ expect(unzipped).to.include('legit content');
141
+ expect(unzipped).to.not.include(SECRET);
142
+ });
143
+ it('still includes symlinks that resolve within the workspace root', async () => {
144
+ await createTempFile('real/target.txt', 'in-tree content');
145
+ await fs.symlink(path.join(tempDir, 'real/target.txt'), path.join(tempDir, 'inside-link.txt'));
146
+ const result = await getSourceFilePaths(tempDir);
147
+ expect(result).to.include(path.join(tempDir, 'inside-link.txt'));
148
+ });
149
+ });
98
150
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@heroku/mcp-server",
3
3
  "description": "Heroku Platform MCP Server",
4
- "version": "1.2.6",
4
+ "version": "1.2.8",
5
5
  "author": "Heroku",
6
6
  "bugs": "https://github.com/heroku/heroku-mcp-server/issues",
7
7
  "bin": {