eslint-plugin-mcp-sdk-security 0.1.1 → 0.2.0
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/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-mcp-sdk-security",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "ESLint plugin for Model Context Protocol (MCP) SDK security — catches tools registered without an input schema,
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "ESLint plugin for Model Context Protocol (MCP) SDK security — catches tools registered without an input schema, handlers reading arguments the schema never declared, model-visible descriptions built from dynamic text, and tool arguments reaching a shell.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
|
7
7
|
"types": "./src/index.d.ts",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
}
|
|
75
75
|
},
|
|
76
76
|
"dependencies": {
|
|
77
|
-
"@interlace/eslint-devkit": "^1.
|
|
77
|
+
"@interlace/eslint-devkit": "^1.9.0"
|
|
78
78
|
},
|
|
79
79
|
"funding": {
|
|
80
80
|
"type": "github",
|
package/src/index.js
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.configs = exports.plugin = exports.rules = exports.requireToolInputSchema = void 0;
|
|
3
|
+
exports.configs = exports.plugin = exports.rules = exports.requireToolInputSchema = exports.noUnvalidatedToolArgs = exports.noToolDescriptionInjection = exports.noCommandInjectionInTool = void 0;
|
|
4
|
+
var no_command_injection_in_tool_2 = require("./rules/no-command-injection-in-tool");
|
|
5
|
+
Object.defineProperty(exports, "noCommandInjectionInTool", { enumerable: true, get: function () { return no_command_injection_in_tool_2.noCommandInjectionInTool; } });
|
|
6
|
+
var no_tool_description_injection_2 = require("./rules/no-tool-description-injection");
|
|
7
|
+
Object.defineProperty(exports, "noToolDescriptionInjection", { enumerable: true, get: function () { return no_tool_description_injection_2.noToolDescriptionInjection; } });
|
|
8
|
+
var no_unvalidated_tool_args_2 = require("./rules/no-unvalidated-tool-args");
|
|
9
|
+
Object.defineProperty(exports, "noUnvalidatedToolArgs", { enumerable: true, get: function () { return no_unvalidated_tool_args_2.noUnvalidatedToolArgs; } });
|
|
4
10
|
var require_tool_input_schema_2 = require("./rules/require-tool-input-schema");
|
|
5
11
|
Object.defineProperty(exports, "requireToolInputSchema", { enumerable: true, get: function () { return require_tool_input_schema_2.requireToolInputSchema; } });
|
|
6
12
|
exports.rules = {
|
|
13
|
+
get 'no-command-injection-in-tool'() { return require("./rules/no-command-injection-in-tool").noCommandInjectionInTool; },
|
|
14
|
+
get 'no-tool-description-injection'() { return require("./rules/no-tool-description-injection").noToolDescriptionInjection; },
|
|
15
|
+
get 'no-unvalidated-tool-args'() { return require("./rules/no-unvalidated-tool-args").noUnvalidatedToolArgs; },
|
|
7
16
|
get 'require-tool-input-schema'() { return require("./rules/require-tool-input-schema").requireToolInputSchema; },
|
|
8
17
|
|
|
9
18
|
};
|
|
10
19
|
exports.plugin = {
|
|
11
20
|
meta: {
|
|
12
21
|
name: 'eslint-plugin-mcp-sdk-security',
|
|
13
|
-
version: '0.
|
|
22
|
+
version: '0.2.0',
|
|
14
23
|
},
|
|
15
24
|
rules: exports.rules,
|
|
16
25
|
};
|
|
@@ -34,9 +43,7 @@ const strictConfig = {
|
|
|
34
43
|
plugins: {
|
|
35
44
|
'mcp-sdk-security': exports.plugin,
|
|
36
45
|
},
|
|
37
|
-
rules: {
|
|
38
|
-
'mcp-sdk-security/require-tool-input-schema': 'error',
|
|
39
|
-
},
|
|
46
|
+
rules: Object.fromEntries(Object.keys(exports.rules).map((ruleName) => [`mcp-sdk-security/${ruleName}`, 'error'])),
|
|
40
47
|
};
|
|
41
48
|
exports.configs = {
|
|
42
49
|
minimal: minimalConfig,
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noCommandInjectionInTool = void 0;
|
|
4
|
+
exports.isBuiltString = isBuiltString;
|
|
5
|
+
exports.handlerArgNames = handlerArgNames;
|
|
6
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
7
|
+
const MCP_MODULE_PREFIX = '@modelcontextprotocol/sdk';
|
|
8
|
+
const REGISTER_TOOL = 'registerTool';
|
|
9
|
+
const LEGACY_TOOL = 'tool';
|
|
10
|
+
const PROCESS_SINKS = new Set([
|
|
11
|
+
'exec',
|
|
12
|
+
'execSync',
|
|
13
|
+
'execFile',
|
|
14
|
+
'execFileSync',
|
|
15
|
+
'spawn',
|
|
16
|
+
'spawnSync',
|
|
17
|
+
'fork',
|
|
18
|
+
]);
|
|
19
|
+
function isBuiltString(node) {
|
|
20
|
+
if (node.type === 'TemplateLiteral')
|
|
21
|
+
return node.expressions.length > 0;
|
|
22
|
+
if (node.type === 'BinaryExpression' && node.operator === '+')
|
|
23
|
+
return true;
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
function handlerArgNames(handler) {
|
|
27
|
+
const direct = new Set();
|
|
28
|
+
const objects = new Set();
|
|
29
|
+
if (handler.type !== 'ArrowFunctionExpression' &&
|
|
30
|
+
handler.type !== 'FunctionExpression' &&
|
|
31
|
+
handler.type !== 'FunctionDeclaration') {
|
|
32
|
+
return { direct, objects };
|
|
33
|
+
}
|
|
34
|
+
const first = handler.params[0];
|
|
35
|
+
if (first === undefined)
|
|
36
|
+
return { direct, objects };
|
|
37
|
+
if (first.type === 'Identifier') {
|
|
38
|
+
objects.add(first.name);
|
|
39
|
+
return { direct, objects };
|
|
40
|
+
}
|
|
41
|
+
if (first.type === 'ObjectPattern') {
|
|
42
|
+
collectPatternNames(first, direct, objects);
|
|
43
|
+
}
|
|
44
|
+
return { direct, objects };
|
|
45
|
+
}
|
|
46
|
+
function collectPatternNames(pattern, into, objects) {
|
|
47
|
+
for (const prop of pattern.properties) {
|
|
48
|
+
if (prop.type === 'RestElement') {
|
|
49
|
+
objects.add(prop.argument.name);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
let value = prop.value;
|
|
53
|
+
if (value.type === 'AssignmentPattern')
|
|
54
|
+
value = value.left;
|
|
55
|
+
if (value.type === 'Identifier')
|
|
56
|
+
into.add(value.name);
|
|
57
|
+
else if (value.type === 'ObjectPattern')
|
|
58
|
+
collectPatternNames(value, into, objects);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.noCommandInjectionInTool = (0, eslint_devkit_1.createRule)({
|
|
62
|
+
name: 'no-command-injection-in-tool',
|
|
63
|
+
meta: {
|
|
64
|
+
type: 'problem',
|
|
65
|
+
docs: {
|
|
66
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-mcp-sdk-security/docs/rules/no-command-injection-in-tool.md',
|
|
67
|
+
description: 'Disallow an MCP tool argument being used directly as the command in a child_process call',
|
|
68
|
+
cwe: 'CWE-78',
|
|
69
|
+
cvss: 9.8,
|
|
70
|
+
},
|
|
71
|
+
messages: {
|
|
72
|
+
toolArgToShell: (0, eslint_devkit_1.formatLLMMessage)({
|
|
73
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
74
|
+
issueName: 'MCP Tool Argument Reaches a Process Sink',
|
|
75
|
+
cwe: 'CWE-78',
|
|
76
|
+
owasp: 'A03:2021',
|
|
77
|
+
cvss: 9.8,
|
|
78
|
+
description: 'Tool argument `{{arg}}` is passed straight to `{{sink}}()`, so whatever steers the model chooses what runs on this host',
|
|
79
|
+
severity: 'CRITICAL',
|
|
80
|
+
compliance: ['SOC2', 'NIST-CSF'],
|
|
81
|
+
fix: 'Do not let the argument name the command. Map it through a fixed allowlist of permitted operations, and pass user data as an argv array element — `execFile(ALLOWED[op], [value])` — never as the executable.',
|
|
82
|
+
documentationLink: 'https://modelcontextprotocol.io/docs/concepts/tools',
|
|
83
|
+
}),
|
|
84
|
+
},
|
|
85
|
+
schema: [],
|
|
86
|
+
},
|
|
87
|
+
defaultOptions: [],
|
|
88
|
+
create(context) {
|
|
89
|
+
let importsMcpSdk = false;
|
|
90
|
+
const handlers = [];
|
|
91
|
+
const candidates = [];
|
|
92
|
+
function enclosingHandler(node) {
|
|
93
|
+
let innermost;
|
|
94
|
+
for (const h of handlers) {
|
|
95
|
+
if (node.range[0] >= h.range[0] && node.range[1] <= h.range[1])
|
|
96
|
+
innermost = h;
|
|
97
|
+
}
|
|
98
|
+
return innermost;
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
ImportDeclaration(node) {
|
|
102
|
+
if (node.source.value.startsWith(MCP_MODULE_PREFIX))
|
|
103
|
+
importsMcpSdk = true;
|
|
104
|
+
},
|
|
105
|
+
CallExpression(node) {
|
|
106
|
+
if (node.callee.type === 'Identifier' &&
|
|
107
|
+
node.callee.name === 'require' &&
|
|
108
|
+
node.arguments[0]?.type === 'Literal' &&
|
|
109
|
+
typeof node.arguments[0].value === 'string' &&
|
|
110
|
+
node.arguments[0].value.startsWith(MCP_MODULE_PREFIX)) {
|
|
111
|
+
importsMcpSdk = true;
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (node.callee.type === 'MemberExpression' &&
|
|
115
|
+
!node.callee.computed &&
|
|
116
|
+
node.callee.property.type === 'Identifier' &&
|
|
117
|
+
(node.callee.property.name === REGISTER_TOOL ||
|
|
118
|
+
node.callee.property.name === LEGACY_TOOL)) {
|
|
119
|
+
const handler = node.arguments[node.arguments.length - 1];
|
|
120
|
+
if (handler !== undefined) {
|
|
121
|
+
const { direct, objects } = handlerArgNames(handler);
|
|
122
|
+
if (direct.size > 0 || objects.size > 0) {
|
|
123
|
+
handlers.push({ range: handler.range, direct, objects });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const sinkName = node.callee.type === 'Identifier'
|
|
128
|
+
? node.callee.name
|
|
129
|
+
: node.callee.type === 'MemberExpression' &&
|
|
130
|
+
!node.callee.computed &&
|
|
131
|
+
node.callee.property.type === 'Identifier'
|
|
132
|
+
? node.callee.property.name
|
|
133
|
+
: undefined;
|
|
134
|
+
if (sinkName === undefined || !PROCESS_SINKS.has(sinkName))
|
|
135
|
+
return;
|
|
136
|
+
const commandArg = node.arguments[0];
|
|
137
|
+
if (commandArg === undefined)
|
|
138
|
+
return;
|
|
139
|
+
if (isBuiltString(commandArg))
|
|
140
|
+
return;
|
|
141
|
+
candidates.push({ node: commandArg, arg: '', sink: sinkName });
|
|
142
|
+
},
|
|
143
|
+
'Program:exit'() {
|
|
144
|
+
if (!importsMcpSdk)
|
|
145
|
+
return;
|
|
146
|
+
for (const candidate of candidates) {
|
|
147
|
+
const handler = enclosingHandler(candidate.node);
|
|
148
|
+
if (handler === undefined)
|
|
149
|
+
continue;
|
|
150
|
+
const command = candidate.node;
|
|
151
|
+
let argName;
|
|
152
|
+
if (command.type === 'Identifier' && handler.direct.has(command.name)) {
|
|
153
|
+
argName = command.name;
|
|
154
|
+
}
|
|
155
|
+
if (command.type === 'MemberExpression' &&
|
|
156
|
+
!command.computed &&
|
|
157
|
+
command.object.type === 'Identifier' &&
|
|
158
|
+
handler.objects.has(command.object.name) &&
|
|
159
|
+
command.property.type === 'Identifier') {
|
|
160
|
+
argName = `${command.object.name}.${command.property.name}`;
|
|
161
|
+
}
|
|
162
|
+
if (argName === undefined)
|
|
163
|
+
continue;
|
|
164
|
+
context.report({
|
|
165
|
+
node: command,
|
|
166
|
+
messageId: 'toolArgToShell',
|
|
167
|
+
data: { arg: argName, sink: candidate.sink },
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
},
|
|
173
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noToolDescriptionInjection = void 0;
|
|
4
|
+
exports.isStaticText = isStaticText;
|
|
5
|
+
exports.modelFacingProperties = modelFacingProperties;
|
|
6
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
7
|
+
const MCP_MODULE_PREFIX = '@modelcontextprotocol/sdk';
|
|
8
|
+
const REGISTER_TOOL = 'registerTool';
|
|
9
|
+
const LEGACY_TOOL = 'tool';
|
|
10
|
+
const MODEL_FACING_KEYS = ['description', 'title'];
|
|
11
|
+
function isStaticText(node) {
|
|
12
|
+
if (node.type === 'Literal')
|
|
13
|
+
return typeof node.value === 'string';
|
|
14
|
+
if (node.type === 'TemplateLiteral')
|
|
15
|
+
return node.expressions.length === 0;
|
|
16
|
+
if (node.type === 'BinaryExpression' && node.operator === '+') {
|
|
17
|
+
return isStaticText(node.left) && isStaticText(node.right);
|
|
18
|
+
}
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
function modelFacingProperties(config) {
|
|
22
|
+
const found = [];
|
|
23
|
+
for (const prop of config.properties) {
|
|
24
|
+
if (prop.type !== 'Property' || prop.computed)
|
|
25
|
+
continue;
|
|
26
|
+
const key = prop.key.type === 'Identifier'
|
|
27
|
+
? prop.key.name
|
|
28
|
+
: prop.key.type === 'Literal' && typeof prop.key.value === 'string'
|
|
29
|
+
? prop.key.value
|
|
30
|
+
: undefined;
|
|
31
|
+
if (key === undefined)
|
|
32
|
+
continue;
|
|
33
|
+
if (!MODEL_FACING_KEYS.includes(key))
|
|
34
|
+
continue;
|
|
35
|
+
if (isStaticText(prop.value))
|
|
36
|
+
continue;
|
|
37
|
+
found.push({ key, value: prop.value });
|
|
38
|
+
}
|
|
39
|
+
return found;
|
|
40
|
+
}
|
|
41
|
+
exports.noToolDescriptionInjection = (0, eslint_devkit_1.createRule)({
|
|
42
|
+
name: 'no-tool-description-injection',
|
|
43
|
+
meta: {
|
|
44
|
+
type: 'problem',
|
|
45
|
+
docs: {
|
|
46
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-mcp-sdk-security/docs/rules/no-tool-description-injection.md',
|
|
47
|
+
description: 'Require MCP tool descriptions and titles to be static text, since they reach the model as instructions',
|
|
48
|
+
cwe: 'CWE-1427',
|
|
49
|
+
cvss: 8.6,
|
|
50
|
+
},
|
|
51
|
+
messages: {
|
|
52
|
+
dynamicDescription: (0, eslint_devkit_1.formatLLMMessage)({
|
|
53
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
54
|
+
issueName: 'MCP Tool Description Built at Runtime',
|
|
55
|
+
cwe: 'CWE-1427',
|
|
56
|
+
owasp: 'A03:2021',
|
|
57
|
+
cvss: 8.6,
|
|
58
|
+
description: 'The `{{key}}` for tool "{{tool}}" is assembled at runtime, so it reaches the model as instruction text this file does not control',
|
|
59
|
+
severity: 'HIGH',
|
|
60
|
+
compliance: ['SOC2'],
|
|
61
|
+
fix: 'Write the {{key}} as a literal. If it genuinely varies, register a separate tool per variant rather than interpolating — the text lands in the model\'s instruction block, so whoever controls the value controls the instructions.',
|
|
62
|
+
documentationLink: 'https://modelcontextprotocol.io/docs/concepts/tools',
|
|
63
|
+
}),
|
|
64
|
+
},
|
|
65
|
+
schema: [],
|
|
66
|
+
},
|
|
67
|
+
defaultOptions: [],
|
|
68
|
+
create(context) {
|
|
69
|
+
let importsMcpSdk = false;
|
|
70
|
+
const candidates = [];
|
|
71
|
+
function toolNameOf(node) {
|
|
72
|
+
const first = node.arguments[0];
|
|
73
|
+
if (first?.type === 'Literal' && typeof first.value === 'string')
|
|
74
|
+
return first.value;
|
|
75
|
+
return 'unknown';
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
ImportDeclaration(node) {
|
|
79
|
+
if (node.source.value.startsWith(MCP_MODULE_PREFIX))
|
|
80
|
+
importsMcpSdk = true;
|
|
81
|
+
},
|
|
82
|
+
CallExpression(node) {
|
|
83
|
+
if (node.callee.type === 'Identifier' &&
|
|
84
|
+
node.callee.name === 'require' &&
|
|
85
|
+
node.arguments[0]?.type === 'Literal' &&
|
|
86
|
+
typeof node.arguments[0].value === 'string' &&
|
|
87
|
+
node.arguments[0].value.startsWith(MCP_MODULE_PREFIX)) {
|
|
88
|
+
importsMcpSdk = true;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (node.callee.type !== 'MemberExpression' || node.callee.computed)
|
|
92
|
+
return;
|
|
93
|
+
if (node.callee.property.type !== 'Identifier')
|
|
94
|
+
return;
|
|
95
|
+
const method = node.callee.property.name;
|
|
96
|
+
if (method !== REGISTER_TOOL && method !== LEGACY_TOOL)
|
|
97
|
+
return;
|
|
98
|
+
const config = node.arguments[1];
|
|
99
|
+
if (config?.type !== 'ObjectExpression')
|
|
100
|
+
return;
|
|
101
|
+
for (const finding of modelFacingProperties(config)) {
|
|
102
|
+
candidates.push({ node: finding.value, tool: toolNameOf(node), key: finding.key });
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
'Program:exit'() {
|
|
106
|
+
if (!importsMcpSdk)
|
|
107
|
+
return;
|
|
108
|
+
for (const { node, tool, key } of candidates) {
|
|
109
|
+
context.report({ node, messageId: 'dynamicDescription', data: { tool, key } });
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noUnvalidatedToolArgs = void 0;
|
|
4
|
+
exports.propertyKey = propertyKey;
|
|
5
|
+
exports.declaredSchemaKeys = declaredSchemaKeys;
|
|
6
|
+
exports.destructuredArgNames = destructuredArgNames;
|
|
7
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
8
|
+
const MCP_MODULE_PREFIX = '@modelcontextprotocol/sdk';
|
|
9
|
+
const REGISTER_TOOL = 'registerTool';
|
|
10
|
+
const LEGACY_TOOL = 'tool';
|
|
11
|
+
function propertyKey(prop) {
|
|
12
|
+
if (prop.type !== 'Property' || prop.computed)
|
|
13
|
+
return undefined;
|
|
14
|
+
if (prop.key.type === 'Identifier')
|
|
15
|
+
return prop.key.name;
|
|
16
|
+
if (prop.key.type === 'Literal' && typeof prop.key.value === 'string')
|
|
17
|
+
return prop.key.value;
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
function declaredSchemaKeys(config) {
|
|
21
|
+
const index = config.properties.findIndex((prop) => propertyKey(prop) === 'inputSchema');
|
|
22
|
+
if (index === -1)
|
|
23
|
+
return undefined;
|
|
24
|
+
for (let i = index + 1; i < config.properties.length; i++) {
|
|
25
|
+
if (config.properties[i].type === 'SpreadElement')
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
const value = config.properties[index].value;
|
|
29
|
+
if (value.type !== 'ObjectExpression')
|
|
30
|
+
return undefined;
|
|
31
|
+
const keys = new Set();
|
|
32
|
+
for (const entry of value.properties) {
|
|
33
|
+
if (entry.type === 'SpreadElement')
|
|
34
|
+
return undefined;
|
|
35
|
+
const key = propertyKey(entry);
|
|
36
|
+
if (key === undefined)
|
|
37
|
+
return undefined;
|
|
38
|
+
keys.add(key);
|
|
39
|
+
}
|
|
40
|
+
return keys;
|
|
41
|
+
}
|
|
42
|
+
function destructuredArgNames(handler) {
|
|
43
|
+
if (handler.type !== 'ArrowFunctionExpression' &&
|
|
44
|
+
handler.type !== 'FunctionExpression' &&
|
|
45
|
+
handler.type !== 'FunctionDeclaration') {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
const first = handler.params[0];
|
|
49
|
+
if (first === undefined || first.type !== 'ObjectPattern')
|
|
50
|
+
return [];
|
|
51
|
+
const found = [];
|
|
52
|
+
for (const prop of first.properties) {
|
|
53
|
+
if (prop.type === 'RestElement')
|
|
54
|
+
continue;
|
|
55
|
+
const key = propertyKey(prop);
|
|
56
|
+
if (key === undefined)
|
|
57
|
+
continue;
|
|
58
|
+
found.push({ name: key, node: prop });
|
|
59
|
+
}
|
|
60
|
+
return found;
|
|
61
|
+
}
|
|
62
|
+
exports.noUnvalidatedToolArgs = (0, eslint_devkit_1.createRule)({
|
|
63
|
+
name: 'no-unvalidated-tool-args',
|
|
64
|
+
meta: {
|
|
65
|
+
type: 'problem',
|
|
66
|
+
docs: {
|
|
67
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-mcp-sdk-security/docs/rules/no-unvalidated-tool-args.md',
|
|
68
|
+
description: 'Disallow a tool handler reading an argument its declared input schema does not include',
|
|
69
|
+
cwe: 'CWE-20',
|
|
70
|
+
cvss: 7.5,
|
|
71
|
+
},
|
|
72
|
+
messages: {
|
|
73
|
+
undeclaredArg: (0, eslint_devkit_1.formatLLMMessage)({
|
|
74
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
75
|
+
issueName: 'MCP Tool Argument Outside the Declared Schema',
|
|
76
|
+
cwe: 'CWE-20',
|
|
77
|
+
owasp: 'A03:2021',
|
|
78
|
+
cvss: 7.5,
|
|
79
|
+
description: 'Tool "{{tool}}" reads `{{arg}}`, which its inputSchema does not declare — so the value either arrives unvalidated or never arrives at all',
|
|
80
|
+
severity: 'HIGH',
|
|
81
|
+
compliance: ['SOC2'],
|
|
82
|
+
fix: 'Declare `{{arg}}` in the inputSchema with the type and constraints it needs, or stop reading it. A key the schema does not mention is one nothing validated.',
|
|
83
|
+
documentationLink: 'https://modelcontextprotocol.io/docs/concepts/tools',
|
|
84
|
+
}),
|
|
85
|
+
},
|
|
86
|
+
schema: [],
|
|
87
|
+
},
|
|
88
|
+
defaultOptions: [],
|
|
89
|
+
create(context) {
|
|
90
|
+
let importsMcpSdk = false;
|
|
91
|
+
const candidates = [];
|
|
92
|
+
function toolNameOf(node) {
|
|
93
|
+
const first = node.arguments[0];
|
|
94
|
+
if (first?.type === 'Literal' && typeof first.value === 'string')
|
|
95
|
+
return first.value;
|
|
96
|
+
return 'unknown';
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
ImportDeclaration(node) {
|
|
100
|
+
if (node.source.value.startsWith(MCP_MODULE_PREFIX))
|
|
101
|
+
importsMcpSdk = true;
|
|
102
|
+
},
|
|
103
|
+
CallExpression(node) {
|
|
104
|
+
if (node.callee.type === 'Identifier' &&
|
|
105
|
+
node.callee.name === 'require' &&
|
|
106
|
+
node.arguments[0]?.type === 'Literal' &&
|
|
107
|
+
typeof node.arguments[0].value === 'string' &&
|
|
108
|
+
node.arguments[0].value.startsWith(MCP_MODULE_PREFIX)) {
|
|
109
|
+
importsMcpSdk = true;
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (node.callee.type !== 'MemberExpression' || node.callee.computed)
|
|
113
|
+
return;
|
|
114
|
+
if (node.callee.property.type !== 'Identifier')
|
|
115
|
+
return;
|
|
116
|
+
const method = node.callee.property.name;
|
|
117
|
+
if (method !== REGISTER_TOOL && method !== LEGACY_TOOL)
|
|
118
|
+
return;
|
|
119
|
+
const config = node.arguments[1];
|
|
120
|
+
if (config?.type !== 'ObjectExpression')
|
|
121
|
+
return;
|
|
122
|
+
const declared = declaredSchemaKeys(config);
|
|
123
|
+
if (declared === undefined)
|
|
124
|
+
return;
|
|
125
|
+
const handler = node.arguments[node.arguments.length - 1];
|
|
126
|
+
for (const read of destructuredArgNames(handler)) {
|
|
127
|
+
if (declared.has(read.name))
|
|
128
|
+
continue;
|
|
129
|
+
candidates.push({ node: read.node, tool: toolNameOf(node), arg: read.name });
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
'Program:exit'() {
|
|
133
|
+
if (!importsMcpSdk)
|
|
134
|
+
return;
|
|
135
|
+
for (const { node, tool, arg } of candidates) {
|
|
136
|
+
context.report({ node, messageId: 'undeclaredArg', data: { tool, arg } });
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
},
|
|
141
|
+
});
|