@craswitch/mcp-server 0.0.0-stage → 2.0.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.
@@ -0,0 +1,336 @@
1
+
2
+ import { SCAN_LIMITS } from './core/types.js';
3
+ import { sha256Hex } from './core/index.js';
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
+ const SAFE_SEGMENT_RE = /^[A-Za-z0-9._-]+$/;
63
+ export const DEFAULT_HTTP_TIMEOUT_MS = 15_000;
64
+
65
+ export function validateRepoSegment(value , fieldName ) {
66
+ if (typeof value !== 'string') {
67
+ throw new Error(`Invalid "${fieldName}": expected a non-empty string.`);
68
+ }
69
+ const trimmed = value.trim();
70
+ if (!trimmed || trimmed === '.' || trimmed === '..' || !SAFE_SEGMENT_RE.test(trimmed)) {
71
+ throw new Error(
72
+ `Invalid "${fieldName}": must match ${SAFE_SEGMENT_RE.source} and not contain path traversal.`,
73
+ );
74
+ }
75
+ return trimmed;
76
+ }
77
+
78
+ export function normalizeBaseUrl(rawUrl ) {
79
+ let parsed ;
80
+ try {
81
+ parsed = new URL(rawUrl);
82
+ } catch {
83
+ throw new Error(`Invalid baseUrl: "${rawUrl}" is not a valid URL.`);
84
+ }
85
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
86
+ throw new Error(`Invalid baseUrl protocol "${parsed.protocol}": only http and https are allowed.`);
87
+ }
88
+ return parsed.toString().replace(/\/+$/, '');
89
+ }
90
+
91
+ function asRecord(value ) {
92
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
93
+ return value ;
94
+ }
95
+ return undefined;
96
+ }
97
+
98
+ function pickString(sources , keys ) {
99
+ for (const src of sources) {
100
+ if (!src) continue;
101
+ for (const key of keys) {
102
+ const val = src[key];
103
+ if (typeof val === 'string' && val.length > 0) return val;
104
+ if (typeof val === 'number' && Number.isFinite(val)) return String(val);
105
+ }
106
+ }
107
+ return '';
108
+ }
109
+
110
+ async function readJsonOrText(res ) {
111
+ const text = await res.text();
112
+ if (!text) return undefined;
113
+ try {
114
+ return JSON.parse(text);
115
+ } catch {
116
+ return text;
117
+ }
118
+ }
119
+
120
+ export async function checkVendorCraStatus(
121
+ input ,
122
+ defaultBaseUrl ,
123
+ fetchImpl = fetch,
124
+ timeoutMs = DEFAULT_HTTP_TIMEOUT_MS,
125
+ ) {
126
+ const org = validateRepoSegment(input.org, 'org');
127
+ const repo = validateRepoSegment(input.repo, 'repo');
128
+ const baseUrl = normalizeBaseUrl(input.baseUrl || defaultBaseUrl);
129
+ const endpoint = `${baseUrl}/api/v1/x402/attest/${encodeURIComponent(org)}/${encodeURIComponent(repo)}`;
130
+
131
+ const headers = {
132
+ Accept: 'application/json',
133
+ 'User-Agent': 'cra-switch-mcp/1.0.0',
134
+ };
135
+ if (input.paymentProof && input.paymentProof.trim().length > 0) {
136
+ headers['X-Payment-Proof'] = input.paymentProof.trim();
137
+ }
138
+
139
+ const res = await fetchImpl(endpoint, {
140
+ method: 'GET',
141
+ headers,
142
+ signal: AbortSignal.timeout(timeoutMs),
143
+ });
144
+ const body = await readJsonOrText(res);
145
+
146
+ if (res.status === 402) {
147
+ const root = asRecord(body);
148
+ const challengeObj = asRecord(root?.challenge);
149
+ const accepts = Array.isArray(root?.accepts) ? asRecord(root.accepts[0]) : undefined;
150
+ const extraObj = asRecord(accepts?.extra) ?? asRecord(challengeObj?.extra) ?? asRecord(root?.extra);
151
+
152
+ const headerMap = {};
153
+ res.headers.forEach((v, k) => {
154
+ headerMap[k.toLowerCase()] = v;
155
+ });
156
+
157
+ const sources = [root, challengeObj, accepts, extraObj];
158
+ const amount =
159
+ pickString(sources, ['amount', 'maxAmountRequired', 'priceAtomic', 'price']) ||
160
+ headerMap['x-402-amount'] ||
161
+ headerMap['x-payment-amount'] ||
162
+ '';
163
+ const asset =
164
+ pickString(sources, ['asset', 'currency', 'token']) ||
165
+ headerMap['x-402-asset'] ||
166
+ headerMap['x-payment-asset'] ||
167
+ '';
168
+ const network =
169
+ pickString(sources, ['network', 'chain']) ||
170
+ headerMap['x-402-network'] ||
171
+ headerMap['x-payment-network'] ||
172
+ '';
173
+ const payTo =
174
+ pickString(sources, ['payTo', 'recipient', 'address']) ||
175
+ headerMap['x-402-payto'] ||
176
+ headerMap['x-402-pay-to'] ||
177
+ headerMap['x-payment-payto'] ||
178
+ '';
179
+ const scheme = pickString(sources, ['scheme']) || headerMap['x-payment-scheme'] || undefined;
180
+ const resource = pickString(sources, ['resource']) || endpoint;
181
+ const description = pickString(sources, ['description']) || undefined;
182
+ const rawNonce =
183
+ pickString(sources, ['nonce', 'challengeNonce', 'paymentNonce', 'id']) ||
184
+ headerMap['x-402-nonce'] ||
185
+ headerMap['x-x402-nonce'] ||
186
+ headerMap['x-payment-nonce'] ||
187
+ '';
188
+ const nonce =
189
+ rawNonce ||
190
+ sha256Hex(`${network}:${asset}:${payTo}:${amount}:${resource}`).slice(0, 24);
191
+
192
+ return {
193
+ status: 'payment_required',
194
+ httpStatus: 402,
195
+ org,
196
+ repo,
197
+ endpoint,
198
+ amount,
199
+ asset,
200
+ network,
201
+ payTo,
202
+ nonce,
203
+ scheme,
204
+ resource,
205
+ description,
206
+ rawChallenge: body,
207
+ };
208
+ }
209
+
210
+ if (res.status === 200) {
211
+ return {
212
+ status: 'ok',
213
+ httpStatus: 200,
214
+ org,
215
+ repo,
216
+ endpoint,
217
+ attestation: body,
218
+ };
219
+ }
220
+
221
+ const errRec = asRecord(body);
222
+ const errorMsg =
223
+ (typeof errRec?.error === 'string' && errRec.error) ||
224
+ (typeof errRec?.message === 'string' && errRec.message) ||
225
+ `HTTP ${res.status} ${res.statusText}`.trim();
226
+
227
+ return {
228
+ status: 'error',
229
+ httpStatus: res.status,
230
+ org,
231
+ repo,
232
+ endpoint,
233
+ error: errorMsg,
234
+ body,
235
+ };
236
+ }
237
+
238
+ export async function fetchVexDocument(
239
+ input ,
240
+ defaultBaseUrl ,
241
+ fetchImpl = fetch,
242
+ timeoutMs = DEFAULT_HTTP_TIMEOUT_MS,
243
+ ) {
244
+ const org = validateRepoSegment(input.org, 'org');
245
+ const repo = validateRepoSegment(input.repo, 'repo');
246
+ const baseUrl = normalizeBaseUrl(input.baseUrl || defaultBaseUrl);
247
+ const url = `${baseUrl}/.well-known/vex.json?repo=${encodeURIComponent(`${org}/${repo}`)}`;
248
+
249
+ const res = await fetchImpl(url, {
250
+ method: 'GET',
251
+ headers: {
252
+ Accept: 'application/json',
253
+ 'User-Agent': 'cra-switch-mcp/1.0.0',
254
+ },
255
+ signal: AbortSignal.timeout(timeoutMs),
256
+ });
257
+
258
+ const body = await readJsonOrText(res);
259
+ if (!res.ok) {
260
+ const errRec = asRecord(body);
261
+ const detail =
262
+ (typeof errRec?.error === 'string' && errRec.error) ||
263
+ `HTTP ${res.status} fetching ${url}`;
264
+ throw new Error(detail);
265
+ }
266
+
267
+ const doc = asRecord(body);
268
+ if (!doc || !Array.isArray(doc.statements)) {
269
+ throw new Error(`Invalid OpenVEX payload returned by ${url}: missing statements array.`);
270
+ }
271
+
272
+ const publicKeyHeader =
273
+ res.headers.get('x-ed25519-public-key') ??
274
+ res.headers.get('x-cra-switch-public-key') ??
275
+ res.headers.get('x-cra-public-key') ??
276
+ res.headers.get('x-public-key') ??
277
+ undefined;
278
+
279
+ return {
280
+ url,
281
+ httpStatus: res.status,
282
+ document: body ,
283
+ publicKeyHeader: publicKeyHeader || undefined,
284
+ };
285
+ }
286
+
287
+ export async function scanLockfileRemote(
288
+ input ,
289
+ fetchImpl = fetch,
290
+ timeoutMs = DEFAULT_HTTP_TIMEOUT_MS,
291
+ ) {
292
+ if (typeof input.filename !== 'string' || input.filename.trim().length === 0) {
293
+ throw new Error('Invalid "filename": expected a non-empty lockfile path or basename.');
294
+ }
295
+ if (typeof input.content !== 'string') {
296
+ throw new Error('Invalid "content": expected lockfile text content.');
297
+ }
298
+ const byteLength = new TextEncoder().encode(input.content).byteLength;
299
+ if (byteLength > SCAN_LIMITS.maxLockfileBytes) {
300
+ throw new Error(
301
+ `Lockfile exceeds maximum allowed size (${byteLength} > ${SCAN_LIMITS.maxLockfileBytes} bytes).`,
302
+ );
303
+ }
304
+
305
+ const baseUrl = normalizeBaseUrl(input.baseUrl);
306
+ const endpoint = `${baseUrl}/api/v1/scan`;
307
+
308
+ const res = await fetchImpl(endpoint, {
309
+ method: 'POST',
310
+ headers: {
311
+ 'Content-Type': 'application/json',
312
+ Accept: 'application/json',
313
+ 'User-Agent': 'cra-switch-mcp/1.0.0',
314
+ },
315
+ body: JSON.stringify({
316
+ filename: input.filename,
317
+ content: input.content,
318
+ }),
319
+ signal: AbortSignal.timeout(timeoutMs),
320
+ });
321
+
322
+ const body = await readJsonOrText(res);
323
+ if (!res.ok) {
324
+ const errRec = asRecord(body);
325
+ const detail =
326
+ (typeof errRec?.error === 'string' && errRec.error) ||
327
+ `Remote scan failed with HTTP ${res.status}`;
328
+ throw new Error(detail);
329
+ }
330
+
331
+ return {
332
+ endpoint,
333
+ httpStatus: res.status,
334
+ result: body,
335
+ };
336
+ }
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ export {
2
+ MCP_PROTOCOL_VERSION,
3
+ MCP_SERVER_INFO,
4
+ MCP_TOOLS,
5
+ handleMcpRequest,
6
+ startStdioServer,
7
+ verifyEd25519VexDocument,
8
+ } from './server.js';
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
@@ -0,0 +1,186 @@
1
+
2
+
3
+ /**
4
+ * Bundled curated KEV/EUVD baseline feed for deterministic offline lockfile scans
5
+ * when no remote CRA-Switch API URL is supplied.
6
+ */
7
+ export const BUNDLED_KEV_FEED = [
8
+ {
9
+ id: 'CVE-2025-55182',
10
+ cveId: 'CVE-2025-55182',
11
+ euvdId: 'EUVD-2025-55182',
12
+ source: 'cisa_kev',
13
+ purls: [
14
+ 'pkg:npm/react-server-dom-webpack@19.0.0',
15
+ 'pkg:npm/react-server-dom-webpack@19.1.0',
16
+ 'pkg:npm/react-server-dom-webpack@19.2.0',
17
+ ],
18
+ affectedRanges: [
19
+ {
20
+ ecosystem: 'npm',
21
+ name: 'react-server-dom-webpack',
22
+ introduced: '19.0.0',
23
+ fixed: '19.2.1',
24
+ exactVersions: ['19.0.0', '19.1.0', '19.1.1', '19.2.0'],
25
+ },
26
+ ],
27
+ activelyExploited: true,
28
+ dateAdded: '2025-12-04T00:00:00Z',
29
+ dueDate: '2025-12-25T00:00:00Z',
30
+ summary: 'React Server Components Flight protocol remote code execution (actively exploited in the wild).',
31
+ references: [
32
+ 'https://nvd.nist.gov/vuln/detail/CVE-2025-55182',
33
+ 'https://www.cisa.gov/known-exploited-vulnerabilities-catalog',
34
+ ],
35
+ rawHash: 'kev-baseline-cve-2025-55182',
36
+ },
37
+ {
38
+ id: 'CVE-2021-44228',
39
+ cveId: 'CVE-2021-44228',
40
+ euvdId: 'EUVD-2021-31234',
41
+ source: 'cisa_kev',
42
+ purls: [
43
+ 'pkg:maven/org.apache.logging.log4j/log4j-core@2.14.1',
44
+ 'pkg:maven/org.apache.logging.log4j/log4j-core@2.13.3',
45
+ ],
46
+ affectedRanges: [
47
+ {
48
+ ecosystem: 'maven',
49
+ namespace: 'org.apache.logging.log4j',
50
+ name: 'log4j-core',
51
+ introduced: '2.0.0',
52
+ fixed: '2.15.0',
53
+ exactVersions: ['2.14.1', '2.14.0', '2.13.3'],
54
+ },
55
+ ],
56
+ activelyExploited: true,
57
+ dateAdded: '2021-12-10T00:00:00Z',
58
+ dueDate: '2021-12-24T00:00:00Z',
59
+ summary: 'Apache Log4j2 JNDI lookup remote code execution (Log4Shell).',
60
+ references: ['https://nvd.nist.gov/vuln/detail/CVE-2021-44228'],
61
+ rawHash: 'kev-baseline-cve-2021-44228',
62
+ },
63
+ {
64
+ id: 'CVE-2023-44487',
65
+ cveId: 'CVE-2023-44487',
66
+ euvdId: 'EUVD-2023-48957',
67
+ source: 'cisa_kev',
68
+ purls: [
69
+ 'pkg:golang/golang.org/x/net@v0.16.0',
70
+ 'pkg:golang/golang.org/x/net@0.16.0',
71
+ 'pkg:cargo/h2@0.3.20',
72
+ ],
73
+ affectedRanges: [
74
+ {
75
+ ecosystem: 'golang',
76
+ namespace: 'golang.org/x',
77
+ name: 'net',
78
+ introduced: '0.0.0',
79
+ fixed: '0.17.0',
80
+ exactVersions: ['0.16.0', 'v0.16.0', '0.15.0', 'v0.15.0'],
81
+ },
82
+ {
83
+ ecosystem: 'cargo',
84
+ name: 'h2',
85
+ introduced: '0.3.0',
86
+ fixed: '0.3.21',
87
+ exactVersions: ['0.3.20', '0.3.19'],
88
+ },
89
+ ],
90
+ activelyExploited: true,
91
+ dateAdded: '2023-10-10T00:00:00Z',
92
+ dueDate: '2023-10-31T00:00:00Z',
93
+ summary: 'HTTP/2 Rapid Reset stream cancellation denial of service.',
94
+ references: ['https://nvd.nist.gov/vuln/detail/CVE-2023-44487'],
95
+ rawHash: 'kev-baseline-cve-2023-44487',
96
+ },
97
+ {
98
+ id: 'CVE-2021-23337',
99
+ cveId: 'CVE-2021-23337',
100
+ euvdId: 'EUVD-2021-23337',
101
+ source: 'enisa_euvd',
102
+ purls: ['pkg:npm/lodash@4.17.20', 'pkg:npm/lodash@4.17.19'],
103
+ affectedRanges: [
104
+ {
105
+ ecosystem: 'npm',
106
+ name: 'lodash',
107
+ introduced: '0.0.0',
108
+ fixed: '4.17.21',
109
+ exactVersions: ['4.17.20', '4.17.19', '4.17.15'],
110
+ },
111
+ ],
112
+ activelyExploited: true,
113
+ dateAdded: '2021-02-15T00:00:00Z',
114
+ summary: 'Lodash command injection via template function.',
115
+ references: ['https://nvd.nist.gov/vuln/detail/CVE-2021-23337'],
116
+ rawHash: 'kev-baseline-cve-2021-23337',
117
+ },
118
+ {
119
+ id: 'CVE-2023-45857',
120
+ cveId: 'CVE-2023-45857',
121
+ euvdId: 'EUVD-2023-45857',
122
+ source: 'enisa_euvd',
123
+ purls: ['pkg:npm/axios@1.5.1', 'pkg:npm/axios@1.5.0', 'pkg:npm/axios@0.21.1'],
124
+ affectedRanges: [
125
+ {
126
+ ecosystem: 'npm',
127
+ name: 'axios',
128
+ introduced: '0.8.1',
129
+ fixed: '1.6.0',
130
+ exactVersions: ['1.5.1', '1.5.0', '0.21.1'],
131
+ },
132
+ ],
133
+ activelyExploited: true,
134
+ dateAdded: '2023-11-08T00:00:00Z',
135
+ summary: 'Axios XSRF-TOKEN cookie exposure to third-party hosts.',
136
+ references: ['https://nvd.nist.gov/vuln/detail/CVE-2023-45857'],
137
+ rawHash: 'kev-baseline-cve-2023-45857',
138
+ },
139
+ {
140
+ id: 'CVE-2023-32681',
141
+ cveId: 'CVE-2023-32681',
142
+ euvdId: 'EUVD-2023-32681',
143
+ source: 'cisa_kev',
144
+ purls: ['pkg:pypi/requests@2.30.0', 'pkg:pypi/requests@2.28.2'],
145
+ affectedRanges: [
146
+ {
147
+ ecosystem: 'pypi',
148
+ name: 'requests',
149
+ introduced: '2.3.0',
150
+ fixed: '2.31.0',
151
+ exactVersions: ['2.30.0', '2.28.2', '2.28.1'],
152
+ },
153
+ ],
154
+ activelyExploited: true,
155
+ dateAdded: '2023-05-26T00:00:00Z',
156
+ summary: 'Python requests unintended leak of Proxy-Authorization header on redirect.',
157
+ references: ['https://nvd.nist.gov/vuln/detail/CVE-2023-32681'],
158
+ rawHash: 'kev-baseline-cve-2023-32681',
159
+ },
160
+ {
161
+ id: 'CVE-2024-21626',
162
+ cveId: 'CVE-2024-21626',
163
+ euvdId: 'EUVD-2024-21626',
164
+ source: 'cisa_kev',
165
+ purls: [
166
+ 'pkg:golang/github.com/opencontainers/runc@v1.1.11',
167
+ 'pkg:golang/github.com/opencontainers/runc@1.1.11',
168
+ ],
169
+ affectedRanges: [
170
+ {
171
+ ecosystem: 'golang',
172
+ namespace: 'github.com/opencontainers',
173
+ name: 'runc',
174
+ introduced: '1.0.0',
175
+ fixed: '1.1.12',
176
+ exactVersions: ['1.1.11', 'v1.1.11', '1.1.10', 'v1.1.10'],
177
+ },
178
+ ],
179
+ activelyExploited: true,
180
+ dateAdded: '2024-02-01T00:00:00Z',
181
+ dueDate: '2024-02-22T00:00:00Z',
182
+ summary: 'runc container breakout via leaked internal file descriptor.',
183
+ references: ['https://nvd.nist.gov/vuln/detail/CVE-2024-21626'],
184
+ rawHash: 'kev-baseline-cve-2024-21626',
185
+ },
186
+ ];