mcp-schema-guard 0.0.0-stage → 1.4.2
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/LICENSE +21 -0
- package/README.md +34 -3
- package/index.d.ts +17 -0
- package/index.js +81 -0
- package/package.json +34 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 madebymedev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# mcp-schema-guard
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
Deterministic, zero-dependency JSON-RPC 2.0 and **Model Context Protocol (MCP)** stdio frame validator. Protects MCP servers, agent tool runners, and CLI transports against oversized frame allocations, prototype-pollution keys, and unbounded AST recursion prior to tool dispatch.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install mcp-schema-guard
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
const { validateJsonRpcFrame } = require('mcp-schema-guard');
|
|
19
|
+
|
|
20
|
+
const frame = validateJsonRpcFrame(rawStdioLine, {
|
|
21
|
+
maxFrameBytes: 524288,
|
|
22
|
+
maxDepth: 24,
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Security Guarantees
|
|
27
|
+
|
|
28
|
+
- **Byte-Length Guard:** Rejects oversized frames before `JSON.parse` allocation.
|
|
29
|
+
- **Prototype-Pollution Filtering:** Scans nested parameter objects for `__proto__`, `constructor`, and `prototype`.
|
|
30
|
+
- **Bounded Recursion:** Enforces strict nesting ceilings on untrusted tool arguments.
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
MIT
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface ValidationOptions {
|
|
2
|
+
maxFrameBytes?: number;
|
|
3
|
+
maxDepth?: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export class FrameValidationError extends Error {
|
|
7
|
+
code: number;
|
|
8
|
+
constructor(message: string, code?: number);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function validateJsonRpcFrame(
|
|
12
|
+
rawInput: string | Buffer | Record<string, unknown>,
|
|
13
|
+
options?: ValidationOptions
|
|
14
|
+
): Record<string, unknown>;
|
|
15
|
+
|
|
16
|
+
export const MAX_DEFAULT_DEPTH: number;
|
|
17
|
+
export const MAX_DEFAULT_FRAME_BYTES: number;
|
package/index.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const MAX_DEFAULT_DEPTH = 32;
|
|
4
|
+
const MAX_DEFAULT_FRAME_BYTES = 1048576;
|
|
5
|
+
const FORBIDDEN_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
6
|
+
|
|
7
|
+
class FrameValidationError extends Error {
|
|
8
|
+
constructor(message, code = -32600) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'FrameValidationError';
|
|
11
|
+
this.code = code;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function checkDepthAndKeys(node, maxDepth, currentDepth = 0) {
|
|
16
|
+
if (currentDepth > maxDepth) {
|
|
17
|
+
throw new FrameValidationError(`Payload exceeds maximum nesting depth of ${maxDepth}`, -32602);
|
|
18
|
+
}
|
|
19
|
+
if (node === null || typeof node !== 'object') {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (Array.isArray(node)) {
|
|
23
|
+
for (let i = 0; i < node.length; i++) {
|
|
24
|
+
checkDepthAndKeys(node[i], maxDepth, currentDepth + 1);
|
|
25
|
+
}
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const keys = Object.keys(node);
|
|
29
|
+
for (let i = 0; i < keys.length; i++) {
|
|
30
|
+
const k = keys[i];
|
|
31
|
+
if (FORBIDDEN_KEYS.has(k)) {
|
|
32
|
+
throw new FrameValidationError(`Forbidden prototype pollution key detected: ${k}`, -32602);
|
|
33
|
+
}
|
|
34
|
+
checkDepthAndKeys(node[k], maxDepth, currentDepth + 1);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function validateJsonRpcFrame(rawInput, options = {}) {
|
|
39
|
+
const maxBytes = options.maxFrameBytes || MAX_DEFAULT_FRAME_BYTES;
|
|
40
|
+
const maxDepth = options.maxDepth || MAX_DEFAULT_DEPTH;
|
|
41
|
+
|
|
42
|
+
const byteLen = Buffer.isBuffer(rawInput)
|
|
43
|
+
? rawInput.length
|
|
44
|
+
: Buffer.byteLength(String(rawInput), 'utf8');
|
|
45
|
+
|
|
46
|
+
if (byteLen === 0) {
|
|
47
|
+
throw new FrameValidationError('Empty stdio frame received', -32700);
|
|
48
|
+
}
|
|
49
|
+
if (byteLen > maxBytes) {
|
|
50
|
+
throw new FrameValidationError(`Frame size ${byteLen} exceeds limit ${maxBytes}`, -32600);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let parsed;
|
|
54
|
+
try {
|
|
55
|
+
parsed = typeof rawInput === 'string' || Buffer.isBuffer(rawInput)
|
|
56
|
+
? JSON.parse(rawInput.toString('utf8'))
|
|
57
|
+
: rawInput;
|
|
58
|
+
} catch (err) {
|
|
59
|
+
throw new FrameValidationError(`Malformed JSON frame: ${err.message}`, -32700);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
63
|
+
throw new FrameValidationError('JSON-RPC root must be an object', -32600);
|
|
64
|
+
}
|
|
65
|
+
if (parsed.jsonrpc !== '2.0') {
|
|
66
|
+
throw new FrameValidationError('Invalid or missing jsonrpc 2.0 version tag', -32600);
|
|
67
|
+
}
|
|
68
|
+
if (parsed.method !== undefined && typeof parsed.method !== 'string') {
|
|
69
|
+
throw new FrameValidationError('JSON-RPC method must be a string', -32600);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
checkDepthAndKeys(parsed, maxDepth, 0);
|
|
73
|
+
return parsed;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
module.exports = {
|
|
77
|
+
validateJsonRpcFrame,
|
|
78
|
+
FrameValidationError,
|
|
79
|
+
MAX_DEFAULT_DEPTH,
|
|
80
|
+
MAX_DEFAULT_FRAME_BYTES,
|
|
81
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "mcp-schema-guard",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "mcp-schema-guard",
|
|
3
|
+
"version": "1.4.2",
|
|
4
|
+
"description": "Zero-dependency JSON-RPC 2.0 and MCP stdio transport frame validator and schema sanitizer",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"index.d.ts",
|
|
10
|
+
"LICENSE",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "node test.js"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/madebymedev/mcp-schema-guard.git"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"mcp",
|
|
22
|
+
"model-context-protocol",
|
|
23
|
+
"json-rpc",
|
|
24
|
+
"stdio",
|
|
25
|
+
"validator",
|
|
26
|
+
"schema",
|
|
27
|
+
"security"
|
|
28
|
+
],
|
|
29
|
+
"author": "madebymedev",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=18.0.0"
|
|
33
|
+
}
|
|
34
|
+
}
|