Skip to content

Commit 35fd3bc

Browse files
authored
Merge pull request #127 from SocketDev/kapravel/improved-origin-checks
Fix origin validation for production domains
2 parents 073575b + 8f4d018 commit 35fd3bc

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,17 +301,22 @@ if (useHttp) {
301301
// Check if request is from localhost (for same-origin requests that don't send Origin header)
302302
// Use strict matching to prevent spoofing via subdomains like "malicious-localhost.evil.com"
303303
const host = req.headers.host || ''
304-
const isLocalhostHost = host === `localhost:${port}` ||
304+
305+
// Extract hostnames from allowedOrigins for Host header validation
306+
const allowedHosts = allowedOrigins.map(o => new URL(o).hostname)
307+
308+
const isAllowedHost = host === `localhost:${port}` ||
305309
host === `127.0.0.1:${port}` ||
306310
host === 'localhost' ||
307-
host === '127.0.0.1'
311+
host === '127.0.0.1' ||
312+
allowedHosts.includes(host)
308313

309314
// Allow requests:
310315
// 1. With Origin header from localhost (any port) or production domains
311-
// 2. Without Origin header if they're from localhost (same-origin requests)
316+
// 2. Without Origin header if they're from localhost or allowed domains (same-origin requests)
312317
const isValidOrigin = origin
313318
? (isLocalhostOrigin(origin) || allowedOrigins.includes(origin))
314-
: isLocalhostHost
319+
: isAllowedHost
315320

316321
if (!isValidOrigin) {
317322
logger.warn(`Rejected request from invalid origin: ${origin || 'missing'} (host: ${host})`)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@socketsecurity/mcp",
3-
"version": "0.0.15",
3+
"version": "0.0.16",
44
"type": "module",
55
"main": "./index.js",
66
"bin": {

0 commit comments

Comments
 (0)