serena-mcp-gateway 1.3.2 → 1.3.3
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/README.md +8 -0
- package/auth-proxy.mjs +23 -7
- package/package.json +1 -1
- package/serena-mcp.mjs +9 -1
package/README.md
CHANGED
|
@@ -89,3 +89,11 @@ serena-mcp --config ./projects.json
|
|
|
89
89
|
从源码全局安装时,可运行 `./install.sh --no-tunnel` 或 `./install.ps1 --no-tunnel`。安装脚本使用同一套依赖检查,接受 `--no-auto-install` 和 `--allow-remote-install`。
|
|
90
90
|
|
|
91
91
|
> **VPN / TUN 模式提示**:如果开启了 VPN 的 TUN 模式导致 `cloudflared` 报 `429` 或无法建立隧道,请在 VPN 规则中将 `cloudflared` 进程或 `*.trycloudflare.com`、`*.argotunnel.com` 设为 `DIRECT`(直连),也可使用 `--tunnel-direct`、切换代理节点、`CLOUDFLARE_TUNNEL_TOKEN`(自有 Named Tunnel)或 `serena-mcp --no-tunnel`。
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## 四、安全考量
|
|
96
|
+
|
|
97
|
+
- **Token 与授权有效期**:未指定时每次启动随机生成高强度 Bearer Token;OAuth 2.1 强制使用 PKCE(`S256`),Access Token 与动态注册客户端有效期均为 **8 小时**,过期后返回 `401` 触发重新授权。固定令牌请通过环境变量 `MCP_TOKEN`(≥32 字符)传入,Named Tunnel 请使用 `CLOUDFLARE_TUNNEL_TOKEN`,避免暴露在命令行历史或进程列表中。
|
|
98
|
+
- **网络与代理隔离**:Serena 与 OAuth 代理均仅监听 `127.0.0.1`;代理转发前会剥离 `authorization`、`cookie`、`cf-*`、`x-forwarded-*`、`forwarded` 等请求头;`/oauth/authorize` 启用 `HttpOnly` + `SameSite=Strict` + 参数绑定 CSRF 校验、CSP、`X-Frame-Options: DENY` 及失败认证限流。
|
|
99
|
+
- **最小权限与供应链校验**:开启公网隧道时建议传入具体项目目录(项目锁定模式),避免全局模式暴露本机其他目录;默认固定上游 Serena `v2.1.0` 的 40 位 commit SHA;自动安装 `cloudflared` 强制核对预置 SHA-256,远端 `uv` 安装脚本默认禁用(需显式 `--allow-remote-install`)。
|
package/auth-proxy.mjs
CHANGED
|
@@ -9,7 +9,8 @@ const MAX_ACCESS_TOKENS = 50;
|
|
|
9
9
|
const MAX_CSRF_TOKENS = 100;
|
|
10
10
|
const AUTH_CODE_TTL_MS = 5 * 60 * 1000;
|
|
11
11
|
const CSRF_TTL_MS = 10 * 60 * 1000;
|
|
12
|
-
const
|
|
12
|
+
const ACCESS_TOKEN_TTL_HOURS = 8;
|
|
13
|
+
const ACCESS_TOKEN_TTL_MS = ACCESS_TOKEN_TTL_HOURS * 60 * 60 * 1000;
|
|
13
14
|
const RATE_WINDOW_MS = 5 * 60 * 1000;
|
|
14
15
|
const MAX_FAILED_AUTH = 10;
|
|
15
16
|
|
|
@@ -41,10 +42,16 @@ function getClientKey(req, trustProxy) {
|
|
|
41
42
|
return (ip && isIP(ip) ? ip : req.socket.remoteAddress) || 'unknown';
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
function getOrigin(req, listenPort, trustProxy) {
|
|
45
|
-
const host = req.headers.host;
|
|
46
|
-
if (trustProxy
|
|
47
|
-
|
|
45
|
+
function getOrigin(req, listenPort, trustProxy, publicOrigin = '') {
|
|
46
|
+
const host = (req.headers.host || '').toLowerCase();
|
|
47
|
+
if (trustProxy) {
|
|
48
|
+
if (publicOrigin) {
|
|
49
|
+
const publicHost = new URL(publicOrigin).host.toLowerCase();
|
|
50
|
+
if (host === publicHost) return publicOrigin;
|
|
51
|
+
}
|
|
52
|
+
if (/^[a-z0-9-]+\.trycloudflare\.com$/i.test(host)) {
|
|
53
|
+
return `https://${host}`;
|
|
54
|
+
}
|
|
48
55
|
}
|
|
49
56
|
return `http://127.0.0.1:${listenPort}`;
|
|
50
57
|
}
|
|
@@ -148,6 +155,7 @@ export function startAuthProxy({
|
|
|
148
155
|
masterToken,
|
|
149
156
|
routes,
|
|
150
157
|
trustProxy = false,
|
|
158
|
+
publicOrigin = '',
|
|
151
159
|
}) {
|
|
152
160
|
if (!masterToken || masterToken.length < 32) {
|
|
153
161
|
throw new Error('MCP_TOKEN must be at least 32 characters.');
|
|
@@ -155,6 +163,14 @@ export function startAuthProxy({
|
|
|
155
163
|
if (!Array.isArray(routes) || routes.length === 0) {
|
|
156
164
|
throw new Error('At least one target route is required.');
|
|
157
165
|
}
|
|
166
|
+
let normalizedPublicOrigin = '';
|
|
167
|
+
if (publicOrigin) {
|
|
168
|
+
const parsed = new URL(publicOrigin);
|
|
169
|
+
if (parsed.protocol !== 'https:') {
|
|
170
|
+
throw new Error('publicOrigin must use https://');
|
|
171
|
+
}
|
|
172
|
+
normalizedPublicOrigin = parsed.origin;
|
|
173
|
+
}
|
|
158
174
|
|
|
159
175
|
const registeredClients = new Map();
|
|
160
176
|
const csrfTokens = new Map();
|
|
@@ -216,7 +232,7 @@ export function startAuthProxy({
|
|
|
216
232
|
|
|
217
233
|
const grantFields = ['client_id', 'redirect_uri', 'state', 'code_challenge', 'code_challenge_method'];
|
|
218
234
|
const server = http.createServer(async (req, res) => {
|
|
219
|
-
const origin = getOrigin(req, server.address().port, trustProxy);
|
|
235
|
+
const origin = getOrigin(req, server.address().port, trustProxy, normalizedPublicOrigin);
|
|
220
236
|
let url;
|
|
221
237
|
try {
|
|
222
238
|
url = new URL(req.url || '/', origin);
|
|
@@ -347,7 +363,7 @@ input,button{width:100%;padding:10px;margin-top:12px;box-sizing:border-box;borde
|
|
|
347
363
|
button{background:#1a73e8;color:#fff;border:none;font-weight:600;cursor:pointer}</style></head>
|
|
348
364
|
<body><form method="POST" action="/oauth/authorize">
|
|
349
365
|
<h3 style="margin:0 0 8px">授权访问本地 Serena MCP</h3>
|
|
350
|
-
<p style="margin:0;font-size:13px;color:#57606a">粘贴终端打印的本次会话 Token
|
|
366
|
+
<p style="margin:0;font-size:13px;color:#57606a">粘贴终端打印的本次会话 Token 完成验证(授权有效期 ${ACCESS_TOKEN_TTL_HOURS} 小时):</p>
|
|
351
367
|
<p style="overflow-wrap:anywhere">回调地址:${escapeHtml(q.get('redirect_uri'))}</p>
|
|
352
368
|
<input type="password" name="token" placeholder="粘贴 MCP Token" required autofocus />
|
|
353
369
|
<input type="hidden" name="csrf_token" value="${escapeHtml(csrf)}" />
|
package/package.json
CHANGED
package/serena-mcp.mjs
CHANGED
|
@@ -436,6 +436,9 @@ async function main() {
|
|
|
436
436
|
const routes = [];
|
|
437
437
|
|
|
438
438
|
if (!opts.token) console.log(`本次会话 Bearer Token: ${token}`);
|
|
439
|
+
console.log(`[Start] serena-mcp-gateway v${PKG_VERSION}`);
|
|
440
|
+
console.log('[Auth] OAuth 授权 Access Token 有效期为 8 小时,过期后重新授权即可。');
|
|
441
|
+
console.log('[Auth] OAuth 授权 Access Token 有效期为 8 小时,过期后重新授权即可。');
|
|
439
442
|
|
|
440
443
|
if (opts.projects.length === 0) {
|
|
441
444
|
console.log('[Mode] 全局多项目模式(可在对话中随时指定/切换项目路径)');
|
|
@@ -511,6 +514,7 @@ async function main() {
|
|
|
511
514
|
masterToken: token,
|
|
512
515
|
routes,
|
|
513
516
|
trustProxy: !opts.noTunnel,
|
|
517
|
+
publicOrigin: opts.tunnelUrl,
|
|
514
518
|
});
|
|
515
519
|
break;
|
|
516
520
|
} catch (error) {
|
|
@@ -544,6 +548,7 @@ async function main() {
|
|
|
544
548
|
|
|
545
549
|
const cfEnv = { ...childEnv };
|
|
546
550
|
delete cfEnv.CLOUDFLARE_TUNNEL_TOKEN;
|
|
551
|
+
delete cfEnv.TUNNEL_TOKEN;
|
|
547
552
|
if (opts.tunnelDirect) {
|
|
548
553
|
for (const key of ['HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'http_proxy', 'https_proxy', 'all_proxy']) {
|
|
549
554
|
delete cfEnv[key];
|
|
@@ -552,8 +557,11 @@ async function main() {
|
|
|
552
557
|
}
|
|
553
558
|
|
|
554
559
|
const useNamedTunnel = Boolean(opts.tunnelToken);
|
|
560
|
+
if (useNamedTunnel) {
|
|
561
|
+
cfEnv.TUNNEL_TOKEN = opts.tunnelToken;
|
|
562
|
+
}
|
|
555
563
|
const cfArgs = useNamedTunnel
|
|
556
|
-
? ['tunnel', '--protocol', 'http2', 'run'
|
|
564
|
+
? ['tunnel', '--protocol', 'http2', 'run']
|
|
557
565
|
: ['tunnel', '--protocol', 'http2', '--url', `http://127.0.0.1:${proxyPort}`];
|
|
558
566
|
|
|
559
567
|
console.log(
|