@socketsecurity/mcp 0.0.13 → 0.0.15
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/index.js +44 -31
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -210,37 +210,6 @@ if (useHttp) {
|
|
|
210
210
|
logger.info(`Starting HTTP server on port ${port}`);
|
|
211
211
|
let httpTransport = null;
|
|
212
212
|
const httpServer = createServer(async (req, res) => {
|
|
213
|
-
const origin = req.headers.origin;
|
|
214
|
-
const allowedOrigins = [
|
|
215
|
-
'http://localhost:3000',
|
|
216
|
-
'http://127.0.0.1:3000',
|
|
217
|
-
'https://mcp.socket.dev',
|
|
218
|
-
'https://mcp.socket-staging.dev'
|
|
219
|
-
];
|
|
220
|
-
const isValidOrigin = origin && allowedOrigins.includes(origin);
|
|
221
|
-
if (!isValidOrigin) {
|
|
222
|
-
logger.warn(`Rejected request from invalid origin: ${origin}`);
|
|
223
|
-
res.writeHead(403, { 'Content-Type': 'application/json' });
|
|
224
|
-
res.end(JSON.stringify({
|
|
225
|
-
jsonrpc: '2.0',
|
|
226
|
-
error: { code: -32000, message: 'Forbidden: Invalid origin' },
|
|
227
|
-
id: null
|
|
228
|
-
}));
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
if (origin && isValidOrigin) {
|
|
232
|
-
res.setHeader('Access-Control-Allow-Origin', origin);
|
|
233
|
-
}
|
|
234
|
-
else {
|
|
235
|
-
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
|
|
236
|
-
}
|
|
237
|
-
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
|
|
238
|
-
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Accept');
|
|
239
|
-
if (req.method === 'OPTIONS') {
|
|
240
|
-
res.writeHead(200);
|
|
241
|
-
res.end();
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
213
|
let url;
|
|
245
214
|
try {
|
|
246
215
|
url = new URL(req.url, `http://localhost:${port}`);
|
|
@@ -265,6 +234,48 @@ if (useHttp) {
|
|
|
265
234
|
}));
|
|
266
235
|
return;
|
|
267
236
|
}
|
|
237
|
+
const origin = req.headers.origin;
|
|
238
|
+
const isLocalhostOrigin = (originUrl) => {
|
|
239
|
+
try {
|
|
240
|
+
const url = new URL(originUrl);
|
|
241
|
+
return url.hostname === 'localhost' || url.hostname === '127.0.0.1';
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
const allowedOrigins = [
|
|
248
|
+
'https://mcp.socket.dev',
|
|
249
|
+
'https://mcp.socket-staging.dev'
|
|
250
|
+
];
|
|
251
|
+
const host = req.headers.host || '';
|
|
252
|
+
const isLocalhostHost = host === `localhost:${port}` ||
|
|
253
|
+
host === `127.0.0.1:${port}` ||
|
|
254
|
+
host === 'localhost' ||
|
|
255
|
+
host === '127.0.0.1';
|
|
256
|
+
const isValidOrigin = origin
|
|
257
|
+
? (isLocalhostOrigin(origin) || allowedOrigins.includes(origin))
|
|
258
|
+
: isLocalhostHost;
|
|
259
|
+
if (!isValidOrigin) {
|
|
260
|
+
logger.warn(`Rejected request from invalid origin: ${origin || 'missing'} (host: ${host})`);
|
|
261
|
+
res.writeHead(403, { 'Content-Type': 'application/json' });
|
|
262
|
+
res.end(JSON.stringify({
|
|
263
|
+
jsonrpc: '2.0',
|
|
264
|
+
error: { code: -32000, message: 'Forbidden: Invalid origin' },
|
|
265
|
+
id: null
|
|
266
|
+
}));
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (origin) {
|
|
270
|
+
res.setHeader('Access-Control-Allow-Origin', origin);
|
|
271
|
+
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
|
|
272
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Accept');
|
|
273
|
+
}
|
|
274
|
+
if (req.method === 'OPTIONS') {
|
|
275
|
+
res.writeHead(200);
|
|
276
|
+
res.end();
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
268
279
|
if (url.pathname === '/') {
|
|
269
280
|
if (req.method === 'POST') {
|
|
270
281
|
let body = '';
|
|
@@ -273,6 +284,8 @@ if (useHttp) {
|
|
|
273
284
|
try {
|
|
274
285
|
const jsonData = JSON.parse(body);
|
|
275
286
|
if (jsonData && jsonData.method === 'initialize') {
|
|
287
|
+
const clientInfo = jsonData.params?.clientInfo;
|
|
288
|
+
logger.info(`Client connected: ${clientInfo?.name || 'unknown'} v${clientInfo?.version || 'unknown'} from ${origin || host}`);
|
|
276
289
|
if (httpTransport) {
|
|
277
290
|
try {
|
|
278
291
|
httpTransport.close();
|