@strixgov/mcp-proxy 0.2.5 → 0.3.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/CHANGELOG.md +76 -8
- package/README.md +209 -20
- package/bin/strix-mcp-proxy.mjs +100 -5
- package/package.json +4 -4
- package/src/config.mjs +152 -2
- package/src/content-guard.mjs +432 -0
- package/src/http-upstream.mjs +431 -0
- package/src/index.mjs +11 -2
- package/src/proxy.mjs +241 -33
- package/src/tool-guard.mjs +183 -0
- package/src/types.d.ts +83 -1
- package/test/content-guard-hardening.test.mjs +159 -0
- package/test/content-guard.test.mjs +387 -0
- package/test/http-upstream-config.test.mjs +115 -0
- package/test/http-upstream-e2e.test.mjs +279 -0
- package/test/http-upstream.test.mjs +304 -0
- package/test/tool-guard.test.mjs +243 -0
- package/test/validate-cli.test.mjs +171 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `strix-mcp-proxy --validate` and the config checks behind it.
|
|
3
|
+
* The validator reads a config and prints what it would do; it connects to
|
|
4
|
+
* nothing and never prints a credential.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import test from "node:test";
|
|
8
|
+
import assert from "node:assert/strict";
|
|
9
|
+
import { spawnSync } from "node:child_process";
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import os from "node:os";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
import url from "node:url";
|
|
14
|
+
|
|
15
|
+
import { describeConfig, unknownConfigKeys, loadConfig } from "../src/config.mjs";
|
|
16
|
+
|
|
17
|
+
const BIN = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), "..", "bin", "strix-mcp-proxy.mjs");
|
|
18
|
+
|
|
19
|
+
function withConfig(cfg, fn) {
|
|
20
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "strix-validate-"));
|
|
21
|
+
const file = path.join(dir, "proxy.json");
|
|
22
|
+
fs.writeFileSync(file, typeof cfg === "string" ? cfg : JSON.stringify(cfg));
|
|
23
|
+
try {
|
|
24
|
+
return fn(file);
|
|
25
|
+
} finally {
|
|
26
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const run = (args) => spawnSync(process.execPath, [BIN, ...args], { encoding: "utf8", timeout: 15_000 });
|
|
31
|
+
|
|
32
|
+
const GOOD = {
|
|
33
|
+
serverId: "demo",
|
|
34
|
+
upstream: { command: "node", args: ["server.mjs", "--token", "do-not-print-this"] },
|
|
35
|
+
capabilities: [{ id: "mcp.demo.read", name: "read", risk: "LOW", mode: "READ" }],
|
|
36
|
+
policy: { default: "DENY" },
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
test("validate: a good config exits 0 and prints a report with no upstream arguments", () => {
|
|
40
|
+
withConfig(GOOD, (file) => {
|
|
41
|
+
const out = run(["--validate", "--config", file]);
|
|
42
|
+
assert.equal(out.status, 0, out.stderr);
|
|
43
|
+
const report = JSON.parse(out.stdout);
|
|
44
|
+
assert.equal(report.valid, true);
|
|
45
|
+
assert.equal(report.serverId, "demo");
|
|
46
|
+
assert.equal(report.capabilityCount, 1);
|
|
47
|
+
assert.equal(report.upstream.transport, "stdio");
|
|
48
|
+
assert.equal(report.upstream.argCount, 3);
|
|
49
|
+
assert.ok(!out.stdout.includes("do-not-print-this"), "upstream arguments can carry a secret and are never printed");
|
|
50
|
+
assert.deepEqual(report.secretScanning, { arguments: "block", results: "warn" });
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("validate: an HTTP upstream prints the origin and header NAMES, never the environment variable or value", () => {
|
|
55
|
+
withConfig(
|
|
56
|
+
{
|
|
57
|
+
upstream: { url: "https://mcp.example.com/mcp?x=1", headers: { Authorization: { fromEnv: "SUPER_SECRET_VAR", prefix: "Bearer " } } },
|
|
58
|
+
capabilities: [],
|
|
59
|
+
},
|
|
60
|
+
(file) => {
|
|
61
|
+
const out = run(["--validate", "--config", file]);
|
|
62
|
+
assert.equal(out.status, 0, out.stdout + out.stderr);
|
|
63
|
+
const report = JSON.parse(out.stdout);
|
|
64
|
+
assert.deepEqual(report.upstream, { transport: "http", origin: "https://mcp.example.com", headerNames: ["Authorization"], allowPrivateNetwork: false });
|
|
65
|
+
assert.ok(!out.stdout.includes("SUPER_SECRET_VAR"));
|
|
66
|
+
assert.ok(!out.stdout.includes("x=1"), "the query string can carry a token and is not printed");
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("validate: an invalid config exits 1 with the reason", () => {
|
|
72
|
+
withConfig({ upstream: {} }, (file) => {
|
|
73
|
+
const out = run(["--validate", "--config", file]);
|
|
74
|
+
assert.equal(out.status, 1);
|
|
75
|
+
const report = JSON.parse(out.stdout);
|
|
76
|
+
assert.equal(report.valid, false);
|
|
77
|
+
assert.match(report.error, /exactly one of 'upstream.command'/);
|
|
78
|
+
});
|
|
79
|
+
withConfig("{not json", (file) => {
|
|
80
|
+
const out = run(["--validate", "--config", file]);
|
|
81
|
+
assert.equal(out.status, 1);
|
|
82
|
+
assert.match(JSON.parse(out.stdout).error, /not valid JSON/);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("validate: needs --config, and a missing file is invalid", () => {
|
|
87
|
+
assert.equal(run(["--validate"]).status, 1);
|
|
88
|
+
assert.equal(run(["--validate", "--config", "/nonexistent/proxy.json"]).status, 1);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("validate: bad secretScanning and argumentRules are caught at load", () => {
|
|
92
|
+
const bad = [
|
|
93
|
+
[{ ...GOOD, secretScanning: { arguments: "strict" } }, /secretScanning.arguments/],
|
|
94
|
+
[{ ...GOOD, secretScanning: { results: "block" } }, /secretScanning.results/],
|
|
95
|
+
[{ ...GOOD, secretScanning: { argument: "block" } }, /not a known setting/],
|
|
96
|
+
[{ ...GOOD, secretScanning: "yes" }, /false or an object/],
|
|
97
|
+
[{ ...GOOD, argumentRules: { t: [{ path: "a" }] } }, /exactly one of/],
|
|
98
|
+
[{ ...GOOD, argumentRules: { t: [{ path: "a", pattern: "(" }] } }, /not a valid regular expression/],
|
|
99
|
+
];
|
|
100
|
+
for (const [cfg, message] of bad) {
|
|
101
|
+
withConfig(cfg, (file) => {
|
|
102
|
+
const out = run(["--validate", "--config", file]);
|
|
103
|
+
assert.equal(out.status, 1, JSON.stringify(cfg));
|
|
104
|
+
assert.match(JSON.parse(out.stdout).error, message);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("validate: warnings for risky settings and for a typo that would be silently ignored", () => {
|
|
110
|
+
withConfig(
|
|
111
|
+
{
|
|
112
|
+
...GOOD,
|
|
113
|
+
policy: { default: "ALLOW" },
|
|
114
|
+
toolPinning: "off",
|
|
115
|
+
secretScanning: { arguments: "off" },
|
|
116
|
+
approval: { enabled: true, type: "auto" },
|
|
117
|
+
toolpinning: "block", // a typo: the real key is toolPinning
|
|
118
|
+
},
|
|
119
|
+
(file) => {
|
|
120
|
+
const out = run(["--validate", "--config", file]);
|
|
121
|
+
assert.equal(out.status, 0);
|
|
122
|
+
const { warnings } = JSON.parse(out.stdout);
|
|
123
|
+
const text = warnings.join("\n");
|
|
124
|
+
assert.match(text, /policy.default is ALLOW/);
|
|
125
|
+
assert.match(text, /approval is auto/);
|
|
126
|
+
assert.match(text, /toolPinning is off/);
|
|
127
|
+
assert.match(text, /secretScanning.arguments is off/);
|
|
128
|
+
assert.match(text, /unknown config key 'toolpinning'/);
|
|
129
|
+
},
|
|
130
|
+
);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("describeConfig and unknownConfigKeys are pure and agree on a clean config", () => {
|
|
134
|
+
assert.deepEqual(unknownConfigKeys(GOOD), []);
|
|
135
|
+
const report = describeConfig(GOOD, GOOD.capabilities);
|
|
136
|
+
assert.deepEqual(report.warnings, []);
|
|
137
|
+
assert.equal(report.approval, "disabled (every approval-required call is denied)");
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("loadConfig accepts secretScanning:false and a valid argumentRules block", async () => {
|
|
141
|
+
await withConfig({ ...GOOD, secretScanning: false, argumentRules: { read: [{ path: "id", max: 10 }] } }, async (file) => {
|
|
142
|
+
const cfg = await loadConfig(file);
|
|
143
|
+
assert.equal(cfg.secretScanning, false);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test("validate: a top-level key starting with _ is a comment, other unknown keys still warn", () => {
|
|
148
|
+
assert.deepEqual(unknownConfigKeys({ _comment: "x", _note: "y", toolPining: "off" }), ["toolPining"]);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("validate: warns, without naming it, when a header's environment variable is not set", () => {
|
|
152
|
+
const cfg = {
|
|
153
|
+
serverId: "x",
|
|
154
|
+
upstream: { url: "https://example.com/mcp", headers: { Authorization: { fromEnv: "STRIX_TEST_UNSET_VAR_42", prefix: "Bearer " } } },
|
|
155
|
+
};
|
|
156
|
+
const env = { ...process.env };
|
|
157
|
+
delete env.STRIX_TEST_UNSET_VAR_42;
|
|
158
|
+
const prev = process.env.STRIX_TEST_UNSET_VAR_42;
|
|
159
|
+
delete process.env.STRIX_TEST_UNSET_VAR_42;
|
|
160
|
+
try {
|
|
161
|
+
const out = describeConfig(cfg, []);
|
|
162
|
+
const w = out.warnings.find((x) => /read from the environment and not set/.test(x));
|
|
163
|
+
assert.ok(w, "expected an unset-environment warning");
|
|
164
|
+
assert.ok(!w.includes("STRIX_TEST_UNSET_VAR_42"), "the variable name must not be printed");
|
|
165
|
+
process.env.STRIX_TEST_UNSET_VAR_42 = "present";
|
|
166
|
+
assert.ok(!describeConfig(cfg, []).warnings.some((x) => /not set in this shell/.test(x)));
|
|
167
|
+
} finally {
|
|
168
|
+
if (prev === undefined) delete process.env.STRIX_TEST_UNSET_VAR_42;
|
|
169
|
+
else process.env.STRIX_TEST_UNSET_VAR_42 = prev;
|
|
170
|
+
}
|
|
171
|
+
});
|