@heroku/mcp-server 1.2.5 → 1.2.7
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/index.js +2 -2
- package/dist/repl/heroku-cli-repl.spec.js +31 -0
- package/dist/resources/dev-center-resource.spec.js +1 -1
- package/dist/tools/apps.spec.js +51 -0
- package/dist/tools/data.spec.js +50 -0
- package/dist/utils/command-builder.js +31 -3
- package/dist/utils/command-builder.spec.js +34 -0
- package/dist/utils/mcp-tool-mocks.spechelper.js +1 -1
- package/dist/utils/tarball.js +44 -8
- package/dist/utils/tarball.spec.js +52 -0
- package/package.json +1 -2
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp
|
|
3
|
-
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio
|
|
2
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
|
|
3
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio';
|
|
4
4
|
import * as pjson from '../package.json' with { type: 'json' };
|
|
5
5
|
import * as apps from './tools/apps.js';
|
|
6
6
|
import * as spaces from './tools/spaces.js';
|
|
@@ -76,6 +76,37 @@ describe('HerokuREPL', () => {
|
|
|
76
76
|
expect(result).to.include('The command failed to complete in 500ms');
|
|
77
77
|
});
|
|
78
78
|
});
|
|
79
|
+
describe('stdin protocol', () => {
|
|
80
|
+
// This test locks in the exact bytes written to the CLI process stdin.
|
|
81
|
+
// Commands are newline-delimited on stdin, so the REPL appends a single
|
|
82
|
+
// trailing '\n' to signal "end of command". This is why a command string
|
|
83
|
+
// must itself be a single line: the CLI treats everything after an embedded
|
|
84
|
+
// newline as a separate command. The CommandBuilder CR/LF validation
|
|
85
|
+
// upstream enforces that; this test documents the exact-bytes behavior it
|
|
86
|
+
// relies on.
|
|
87
|
+
it('should write exactly command + a single trailing newline to stdin', async () => {
|
|
88
|
+
const command = 'apps';
|
|
89
|
+
// Deterministically resolve the moment the async iterator writes to
|
|
90
|
+
// stdin, rather than waiting a fixed duration.
|
|
91
|
+
let onWrite;
|
|
92
|
+
const written = new Promise((resolve) => {
|
|
93
|
+
onWrite = resolve;
|
|
94
|
+
});
|
|
95
|
+
mockProcess.stdin.write.callsFake(() => {
|
|
96
|
+
onWrite();
|
|
97
|
+
return true;
|
|
98
|
+
});
|
|
99
|
+
const commandPromise = repl.executeCommand(command);
|
|
100
|
+
// Proceed as soon as the write actually happens.
|
|
101
|
+
await written;
|
|
102
|
+
expect(mockProcess.stdin.write.calledOnce).to.be.true;
|
|
103
|
+
const writtenValue = mockProcess.stdin.write.firstCall.args[0];
|
|
104
|
+
expect(writtenValue).to.equal(command + '\n');
|
|
105
|
+
// Clean up the outstanding command promise.
|
|
106
|
+
setTimeout(() => mockProcess.stdout.emit('data', 'Command output\n<<<END RESULTS>>>'));
|
|
107
|
+
await commandPromise;
|
|
108
|
+
});
|
|
109
|
+
});
|
|
79
110
|
describe('process management', () => {
|
|
80
111
|
it('should restart process on unexpected close', () => {
|
|
81
112
|
mockProcess.emit('close', 1);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expect } from 'chai';
|
|
2
2
|
import sinon from 'sinon';
|
|
3
|
-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp
|
|
3
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
|
|
4
4
|
import { registerDevCenterResource } from './dev-center-resource.js';
|
|
5
5
|
describe('registerDevCenterResource', () => {
|
|
6
6
|
let server;
|
package/dist/tools/apps.spec.js
CHANGED
|
@@ -255,4 +255,55 @@ describe('apps topic tools', () => {
|
|
|
255
255
|
}
|
|
256
256
|
});
|
|
257
257
|
});
|
|
258
|
+
// Input validation: a multi-line value routed through a real tool handler must be
|
|
259
|
+
// rejected by CommandBuilder before it can reach the Heroku REPL.
|
|
260
|
+
describe('input validation', () => {
|
|
261
|
+
let mocks;
|
|
262
|
+
beforeEach(() => {
|
|
263
|
+
mocks = setupMcpToolMocks();
|
|
264
|
+
});
|
|
265
|
+
afterEach(() => {
|
|
266
|
+
sinon.restore();
|
|
267
|
+
});
|
|
268
|
+
it('rejects a multi-line team flag on list_apps without executing it', async () => {
|
|
269
|
+
const multiLineTeam = 'my-team\nsecond-line';
|
|
270
|
+
registerListAppsTool(mocks.server, mocks.herokuRepl);
|
|
271
|
+
const toolCallback = mocks.getToolCallback();
|
|
272
|
+
let error;
|
|
273
|
+
try {
|
|
274
|
+
await toolCallback({ team: multiLineTeam }, {});
|
|
275
|
+
}
|
|
276
|
+
catch (caught) {
|
|
277
|
+
error = caught;
|
|
278
|
+
}
|
|
279
|
+
// The invalid value must be surfaced as an error...
|
|
280
|
+
expect(error).to.be.an('Error');
|
|
281
|
+
// ...and must never reach the REPL.
|
|
282
|
+
expect(mocks.herokuRepl.executeCommand.called).to.be.false;
|
|
283
|
+
const executedWithLineBreak = mocks.herokuRepl.executeCommand
|
|
284
|
+
.getCalls()
|
|
285
|
+
.some((call) => typeof call.args[0] === 'string' && call.args[0].includes('\n'));
|
|
286
|
+
expect(executedWithLineBreak).to.be.false;
|
|
287
|
+
});
|
|
288
|
+
it('rejects a multi-line newName argument on rename_app without executing it', async () => {
|
|
289
|
+
const multiLineNewName = 'renamed-app\nsecond-line';
|
|
290
|
+
registerRenameAppTool(mocks.server, mocks.herokuRepl);
|
|
291
|
+
const toolCallback = mocks.getToolCallback();
|
|
292
|
+
let error;
|
|
293
|
+
try {
|
|
294
|
+
await toolCallback({ app: 'test-app', newName: multiLineNewName }, {});
|
|
295
|
+
}
|
|
296
|
+
catch (caught) {
|
|
297
|
+
error = caught;
|
|
298
|
+
}
|
|
299
|
+
// The invalid value must be surfaced as an error...
|
|
300
|
+
expect(error).to.be.an('Error');
|
|
301
|
+
// ...and must never reach the REPL.
|
|
302
|
+
expect(mocks.herokuRepl.executeCommand.called).to.be.false;
|
|
303
|
+
const executedWithLineBreak = mocks.herokuRepl.executeCommand
|
|
304
|
+
.getCalls()
|
|
305
|
+
.some((call) => typeof call.args[0] === 'string' && call.args[0].includes('\n'));
|
|
306
|
+
expect(executedWithLineBreak).to.be.false;
|
|
307
|
+
});
|
|
308
|
+
});
|
|
258
309
|
});
|
package/dist/tools/data.spec.js
CHANGED
|
@@ -55,6 +55,56 @@ describe('PostgreSQL Tools', () => {
|
|
|
55
55
|
isError: true
|
|
56
56
|
});
|
|
57
57
|
});
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
// CHARACTERIZATION TESTS: newline handling in the `command` value.
|
|
60
|
+
//
|
|
61
|
+
// These tests document the CURRENT (and intentionally inconsistent) behavior
|
|
62
|
+
// of pg:psql. Unlike every other tool -- which relies on CommandBuilder's
|
|
63
|
+
// CR/LF validation to REJECT any value containing a line break -- pg:psql
|
|
64
|
+
// pre-processes its `command` value with `replaceAll('\n', ' ')` in data.ts
|
|
65
|
+
// BEFORE it reaches CommandBuilder (see the `command:` flag in
|
|
66
|
+
// registerPgPsqlTool). As a result:
|
|
67
|
+
// * a line feed (\n) is SILENTLY REWRITTEN to a space (strip, not reject),
|
|
68
|
+
// * but a carriage return (\r) is NOT touched by that replace and still
|
|
69
|
+
// reaches CommandBuilder's validation.
|
|
70
|
+
//
|
|
71
|
+
// If pg:psql is ever unified with the reject-everywhere validation (i.e. the
|
|
72
|
+
// `replaceAll('\n', ' ')` is removed so LF is rejected like everywhere else),
|
|
73
|
+
// these tests will intentionally break -- signalling that the documented
|
|
74
|
+
// behavior changed on purpose.
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
it('CHARACTERIZATION: strips (does not reject) a LF in command, replacing it with a space', async () => {
|
|
77
|
+
mocks.herokuRepl.executeCommand.resolves('Query executed successfully\n');
|
|
78
|
+
// Multi-line SQL: the LF between the two statements should be converted to a space.
|
|
79
|
+
await toolCallback({ app: 'myapp', command: 'SELECT 1;\nSELECT 2;' });
|
|
80
|
+
expect(mocks.herokuRepl.executeCommand.calledOnce).to.be.true;
|
|
81
|
+
const builtCommand = mocks.herokuRepl.executeCommand.firstCall.args[0];
|
|
82
|
+
// The newline was replaced by a space BEFORE reaching CommandBuilder, so the
|
|
83
|
+
// command is NOT rejected and the built command contains the space-joined SQL.
|
|
84
|
+
expect(builtCommand).to.equal(`${TOOL_COMMAND_MAP.PG_PSQL} --app=myapp --command="SELECT 1; SELECT 2;"`);
|
|
85
|
+
// Confirm no raw LF survived in the command sent to the CLI.
|
|
86
|
+
expect(builtCommand).to.not.include('\n');
|
|
87
|
+
});
|
|
88
|
+
// OBSERVED BEHAVIOR (verified by running this spec): a carriage return (\r) in
|
|
89
|
+
// `command` is NOT stripped by `replaceAll('\n', ' ')`, so it reaches
|
|
90
|
+
// CommandBuilder's CR/LF validation and is REJECTED -- the tool callback REJECTS
|
|
91
|
+
// (throws) and executeCommand is never called. This differs from the LF case
|
|
92
|
+
// above, which is silently stripped.
|
|
93
|
+
it('CHARACTERIZATION: rejects a CR in command (not stripped) via CommandBuilder validation', async () => {
|
|
94
|
+
mocks.herokuRepl.executeCommand.resolves('Query executed successfully\n');
|
|
95
|
+
let thrownError;
|
|
96
|
+
try {
|
|
97
|
+
await toolCallback({ app: 'myapp', command: 'SELECT 1;\rSELECT 2;' });
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
thrownError = error;
|
|
101
|
+
}
|
|
102
|
+
// The \r is not touched by replaceAll('\n', ' '), so CommandBuilder rejects it.
|
|
103
|
+
expect(thrownError, 'expected the tool callback to throw for a CR in command').to.be.an('error');
|
|
104
|
+
expect(thrownError?.message).to.include('line breaks (CR/LF) are not allowed');
|
|
105
|
+
// Because building the command threw, the command was never executed.
|
|
106
|
+
expect(mocks.herokuRepl.executeCommand.called).to.be.false;
|
|
107
|
+
});
|
|
58
108
|
});
|
|
59
109
|
describe('pg:info', () => {
|
|
60
110
|
let mocks;
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Characters that are not permitted in a flag value or positional argument.
|
|
3
|
+
*
|
|
4
|
+
* Each built command is delivered to the Heroku CLI as a single line — the REPL
|
|
5
|
+
* writes `command + '\n'` to the CLI process's stdin (see `HerokuREPL`). Because
|
|
6
|
+
* commands are line-delimited, a value that spans multiple lines would not be
|
|
7
|
+
* handled as a single, self-contained value. There is no legitimate reason for a
|
|
8
|
+
* flag value or positional argument to contain a line break, so we apply strict
|
|
9
|
+
* input validation and reject them.
|
|
10
|
+
*/
|
|
11
|
+
const LINE_BREAK_PATTERN = /[\r\n]/;
|
|
1
12
|
/**
|
|
2
13
|
* A builder class for constructing Heroku CLI commands with flags and positional arguments.
|
|
3
14
|
* This class provides a fluent interface for building command-line arguments in a structured way.
|
|
@@ -14,6 +25,19 @@ export class CommandBuilder {
|
|
|
14
25
|
constructor(baseCommand) {
|
|
15
26
|
this.baseCommand = baseCommand;
|
|
16
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Validates that a value is a single line, as required for a well-formed command.
|
|
30
|
+
*
|
|
31
|
+
* @param kind - Whether the value is a flag value or a positional argument (for the error message).
|
|
32
|
+
* @param name - The flag/argument name the value is associated with (for the error message).
|
|
33
|
+
* @param value - The value to validate.
|
|
34
|
+
* @throws {Error} If the value contains a carriage return or line feed.
|
|
35
|
+
*/
|
|
36
|
+
static assertNoLineBreaks(kind, name, value) {
|
|
37
|
+
if (LINE_BREAK_PATTERN.test(value)) {
|
|
38
|
+
throw new Error(`Invalid ${kind} value for "${name}": line breaks (CR/LF) are not allowed. Commands are sent one per line to the Heroku CLI, so a value must be contained on a single line.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
17
41
|
/**
|
|
18
42
|
* Adds command-line flags to the command.
|
|
19
43
|
*
|
|
@@ -26,8 +50,10 @@ export class CommandBuilder {
|
|
|
26
50
|
if (value) {
|
|
27
51
|
if (typeof value === 'boolean')
|
|
28
52
|
this.flags.push(`--${flag}`);
|
|
29
|
-
else
|
|
53
|
+
else {
|
|
54
|
+
CommandBuilder.assertNoLineBreaks('flag', flag, value);
|
|
30
55
|
this.flags.push(`--${flag}=${value}`);
|
|
56
|
+
}
|
|
31
57
|
}
|
|
32
58
|
}
|
|
33
59
|
return this;
|
|
@@ -39,9 +65,11 @@ export class CommandBuilder {
|
|
|
39
65
|
* @returns The builder instance for method chaining
|
|
40
66
|
*/
|
|
41
67
|
addPositionalArguments(args) {
|
|
42
|
-
for (const [, value] of Object.entries(args)) {
|
|
43
|
-
if (value)
|
|
68
|
+
for (const [name, value] of Object.entries(args)) {
|
|
69
|
+
if (value) {
|
|
70
|
+
CommandBuilder.assertNoLineBreaks('argument', name, value);
|
|
44
71
|
this.args.push(value);
|
|
72
|
+
}
|
|
45
73
|
}
|
|
46
74
|
return this;
|
|
47
75
|
}
|
|
@@ -43,6 +43,19 @@ describe('CommandBuilder', () => {
|
|
|
43
43
|
const result = builder.addFlags({ all: true });
|
|
44
44
|
expect(result).to.equal(builder);
|
|
45
45
|
});
|
|
46
|
+
it('rejects flag values containing line breaks', () => {
|
|
47
|
+
const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS);
|
|
48
|
+
expect(() => builder.addFlags({ team: 'my-team\nsecond-line' })).to.throw(/line breaks \(CR\/LF\) are not allowed/);
|
|
49
|
+
expect(() => builder.addFlags({ team: 'my-team\rother' })).to.throw(/line breaks/);
|
|
50
|
+
});
|
|
51
|
+
it('still allows flag values that contain spaces (only line breaks are rejected)', () => {
|
|
52
|
+
const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS);
|
|
53
|
+
expect(() => builder.addFlags({ team: 'my team' })).to.not.throw();
|
|
54
|
+
});
|
|
55
|
+
it('names the offending flag and the "flag" kind in the error message', () => {
|
|
56
|
+
const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS);
|
|
57
|
+
expect(() => builder.addFlags({ team: 'my-team\nsecond-line' })).to.throw(/Invalid flag value for "team"/);
|
|
58
|
+
});
|
|
46
59
|
});
|
|
47
60
|
describe('addPositionalArguments', () => {
|
|
48
61
|
it('adds positional arguments correctly', () => {
|
|
@@ -65,6 +78,14 @@ describe('CommandBuilder', () => {
|
|
|
65
78
|
const result = builder.addPositionalArguments({ new_name: 'new-app-name' });
|
|
66
79
|
expect(result).to.equal(builder);
|
|
67
80
|
});
|
|
81
|
+
it('rejects positional argument values containing line breaks', () => {
|
|
82
|
+
const builder = new CommandBuilder(TOOL_COMMAND_MAP.RENAME_APP);
|
|
83
|
+
expect(() => builder.addPositionalArguments({ new_name: 'ok\nsecond-line' })).to.throw(/line breaks \(CR\/LF\) are not allowed/);
|
|
84
|
+
});
|
|
85
|
+
it('names the offending argument and the "argument" kind in the error message', () => {
|
|
86
|
+
const builder = new CommandBuilder(TOOL_COMMAND_MAP.RENAME_APP);
|
|
87
|
+
expect(() => builder.addPositionalArguments({ new_name: 'ok\nsecond-line' })).to.throw(/Invalid argument value for "new_name"/);
|
|
88
|
+
});
|
|
68
89
|
});
|
|
69
90
|
describe('build', () => {
|
|
70
91
|
it('combines flags and positional arguments correctly', () => {
|
|
@@ -91,5 +112,18 @@ describe('CommandBuilder', () => {
|
|
|
91
112
|
builder.addFlags({ region: 'eu' }).addPositionalArguments({ app: 'my-app' }).addFlags({ team: 'my-team' });
|
|
92
113
|
expect(builder.build()).to.equal('apps:create --region=eu --team=my-team -- my-app');
|
|
93
114
|
});
|
|
115
|
+
it('can never emit a command containing carriage returns or line feeds', () => {
|
|
116
|
+
// Validation rejects CR/LF at add-time, so a value with a line break is never
|
|
117
|
+
// stored and the build output is always free of them. This test depends on that
|
|
118
|
+
// validation: it asserts the invalid add throws AND that the command built from
|
|
119
|
+
// the remaining valid values contains no CR/LF.
|
|
120
|
+
const builder = new CommandBuilder(TOOL_COMMAND_MAP.CREATE_APP);
|
|
121
|
+
builder.addFlags({ region: 'eu' });
|
|
122
|
+
expect(() => builder.addFlags({ team: 'my-team\nsecond-line' })).to.throw(/line breaks \(CR\/LF\) are not allowed/);
|
|
123
|
+
expect(() => builder.addPositionalArguments({ app: 'my-app\rsecond-line' })).to.throw(/line breaks \(CR\/LF\) are not allowed/);
|
|
124
|
+
const command = builder.build();
|
|
125
|
+
expect(command).to.not.match(/[\r\n]/);
|
|
126
|
+
expect(command).to.equal('apps:create --region=eu');
|
|
127
|
+
});
|
|
94
128
|
});
|
|
95
129
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import sinon from 'sinon';
|
|
2
|
-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp
|
|
2
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
|
|
3
3
|
import { HerokuREPL } from '../repl/heroku-cli-repl.js';
|
|
4
4
|
/**
|
|
5
5
|
* Helper function to set up common MCP tool mocks for testing
|
package/dist/utils/tarball.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import zlib from 'node:zlib';
|
|
2
|
-
import
|
|
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
|
|
74
|
-
|
|
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.
|
|
4
|
+
"version": "1.2.7",
|
|
5
5
|
"author": "Heroku",
|
|
6
6
|
"bugs": "https://github.com/heroku/heroku-mcp-server/issues",
|
|
7
7
|
"bin": {
|
|
@@ -78,7 +78,6 @@
|
|
|
78
78
|
"start": "node bin/heroku-mcp-server.mjs",
|
|
79
79
|
"format": "prettier . --write --config ./.prettier.mjs",
|
|
80
80
|
"lint": "cd src && eslint ./",
|
|
81
|
-
"posttest": "npm run lint",
|
|
82
81
|
"prepare": "husky",
|
|
83
82
|
"pretest": "npm run build",
|
|
84
83
|
"type-check": "tsc --noEmit",
|