serena-mcp-gateway 1.3.1 → 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 CHANGED
@@ -78,6 +78,9 @@ serena-mcp --config ./projects.json
78
78
  | `--timeout <sec>` | 等待 Serena 就绪的超时秒数(默认 `300`) |
79
79
  | `--allow-remote-install` | 允许在需要时下载并执行 astral.sh 的 uv 安装脚本 |
80
80
  | `--no-tunnel` | 仅启动本地 Serena + OAuth 代理,不启动 `cloudflared` |
81
+ | `--tunnel-direct` | 启动 `cloudflared` 时清除代理环境变量(直连模式) |
82
+ | `--tunnel-token <token>` | 使用 Cloudflare Named Tunnel Token(建议通过 `CLOUDFLARE_TUNNEL_TOKEN` 环境变量传入,彻底避开临时隧道 429 限流) |
83
+ | `--tunnel-url <url>` | 配合 `--tunnel-token` 指定你的公网 `https://` 域名(也可用 `CLOUDFLARE_TUNNEL_URL`) |
81
84
  | `--setup` | 仅检查并自动安装缺失依赖(`uvx` / `cloudflared`),可结合 `--no-tunnel` 仅检查本地模式依赖 |
82
85
  | `--no-auto-install` | 缺依赖时仅提示手动命令,不自动安装 |
83
86
  | `-v, --version` | 查看版本 |
@@ -85,4 +88,12 @@ serena-mcp --config ./projects.json
85
88
 
86
89
  从源码全局安装时,可运行 `./install.sh --no-tunnel` 或 `./install.ps1 --no-tunnel`。安装脚本使用同一套依赖检查,接受 `--no-auto-install` 和 `--allow-remote-install`。
87
90
 
88
- > **VPN / TUN 模式提示**:如果开启了 VPN 的 TUN 模式导致 `cloudflared` 无法建立隧道,请在 VPN 规则中将 `cloudflared` 进程或 `*.argotunnel.com`、`*.trycloudflare.com` 设为直连,或改用 `serena-mcp --no-tunnel` 仅启动本地服务。
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 ACCESS_TOKEN_TTL_MS = 24 * 60 * 60 * 1000;
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 && /^[a-z0-9-]+\.trycloudflare\.com$/i.test(host || '')) {
47
- return `https://${host.toLowerCase()}`;
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 完成验证:</p>
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serena-mcp-gateway",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Cross-platform Serena MCP launcher with OAuth 2.1 proxy, Cloudflare Tunnel, and multi-project parallel routing",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/serena-mcp.mjs CHANGED
@@ -40,6 +40,9 @@ Serena MCP Gateway (跨平台单项目 / 多项目并行启动器)
40
40
  --allow-remote-install 允许执行 uv 官方远端安装脚本
41
41
  --context <name> 传给 Serena 的 --context(默认 claude-code)
42
42
  --no-tunnel 仅启动本地 Serena + OAuth 代理,不启动 cloudflared
43
+ --tunnel-direct 启动 cloudflared 时清除代理环境变量(直连模式)
44
+ --tunnel-token <t> 使用 Cloudflare Named Tunnel Token(建议用 CLOUDFLARE_TUNNEL_TOKEN 环境变量,避开 429)
45
+ --tunnel-url <url> 配合 --tunnel-token 指定公网 HTTPS 域名(如 https://mcp.example.com)
43
46
  --setup 仅检查并自动安装缺失依赖(uvx / cloudflared),不启动服务
44
47
  --no-auto-install 缺依赖时仅提示手动安装命令,不自动安装
45
48
  -v, --version 显示当前版本号
@@ -127,6 +130,9 @@ function parseArgs(argv) {
127
130
  allowRemoteInstall: false,
128
131
  context: 'claude-code',
129
132
  noTunnel: false,
133
+ tunnelDirect: false,
134
+ tunnelToken: process.env.CLOUDFLARE_TUNNEL_TOKEN || '',
135
+ tunnelUrl: process.env.CLOUDFLARE_TUNNEL_URL || '',
130
136
  setup: false,
131
137
  noAutoInstall: false,
132
138
  version: false,
@@ -147,6 +153,17 @@ function parseArgs(argv) {
147
153
  opts.version = true;
148
154
  } else if (arg === '--no-tunnel') {
149
155
  opts.noTunnel = true;
156
+ } else if (arg === '--tunnel-direct') {
157
+ opts.tunnelDirect = true;
158
+ } else if (arg === '--tunnel-token') {
159
+ opts.tunnelToken = readValue(arg, argv[++i]);
160
+ console.warn('[Security] 命令行 Tunnel Token 可能出现在历史记录中,建议使用 CLOUDFLARE_TUNNEL_TOKEN 环境变量。');
161
+ } else if (arg === '--tunnel-url') {
162
+ const rawUrl = readValue(arg, argv[++i]).replace(/\/+$/, '');
163
+ if (!/^https:\/\/[a-z0-9.-]+(?::\d+)?$/i.test(rawUrl)) {
164
+ throw new Error(`无效 --tunnel-url(需为 https:// 域名): ${rawUrl}`);
165
+ }
166
+ opts.tunnelUrl = rawUrl;
150
167
  } else if (arg === '--setup') {
151
168
  opts.setup = true;
152
169
  } else if (arg === '--no-auto-install') {
@@ -419,6 +436,9 @@ async function main() {
419
436
  const routes = [];
420
437
 
421
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 小时,过期后重新授权即可。');
422
442
 
423
443
  if (opts.projects.length === 0) {
424
444
  console.log('[Mode] 全局多项目模式(可在对话中随时指定/切换项目路径)');
@@ -494,6 +514,7 @@ async function main() {
494
514
  masterToken: token,
495
515
  routes,
496
516
  trustProxy: !opts.noTunnel,
517
+ publicOrigin: opts.tunnelUrl,
497
518
  });
498
519
  break;
499
520
  } catch (error) {
@@ -525,24 +546,49 @@ async function main() {
525
546
  return;
526
547
  }
527
548
 
528
- console.log(`[Start] 启动 Cloudflare Tunnel -> http://127.0.0.1:${proxyPort}`);
529
- const cf = spawn(
530
- 'cloudflared',
531
- ['tunnel', '--protocol', 'http2', '--url', `http://127.0.0.1:${proxyPort}`],
532
- {
533
- stdio: ['ignore', 'pipe', 'pipe'],
534
- shell: false,
535
- detached: !IS_WIN,
536
- env: childEnv,
549
+ const cfEnv = { ...childEnv };
550
+ delete cfEnv.CLOUDFLARE_TUNNEL_TOKEN;
551
+ delete cfEnv.TUNNEL_TOKEN;
552
+ if (opts.tunnelDirect) {
553
+ for (const key of ['HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'http_proxy', 'https_proxy', 'all_proxy']) {
554
+ delete cfEnv[key];
537
555
  }
556
+ console.log('[Tunnel] 已启用 --tunnel-direct:已清除 cloudflared 代理环境变量(若开 TUN 请同时在代理软件中放行 DIRECT)。');
557
+ }
558
+
559
+ const useNamedTunnel = Boolean(opts.tunnelToken);
560
+ if (useNamedTunnel) {
561
+ cfEnv.TUNNEL_TOKEN = opts.tunnelToken;
562
+ }
563
+ const cfArgs = useNamedTunnel
564
+ ? ['tunnel', '--protocol', 'http2', 'run']
565
+ : ['tunnel', '--protocol', 'http2', '--url', `http://127.0.0.1:${proxyPort}`];
566
+
567
+ console.log(
568
+ `[Start] 启动 Cloudflare ${useNamedTunnel ? 'Named ' : ''}Tunnel -> http://127.0.0.1:${proxyPort}`
538
569
  );
570
+ const cf = spawn('cloudflared', cfArgs, {
571
+ stdio: ['ignore', 'pipe', 'pipe'],
572
+ shell: false,
573
+ detached: !IS_WIN,
574
+ env: cfEnv,
575
+ });
539
576
  children.push(cf);
540
577
 
541
578
  let printedUrl = false;
579
+ let tunnelRateLimited = false;
580
+ if (useNamedTunnel && opts.tunnelUrl) {
581
+ printedUrl = true;
582
+ printEndpoints(opts.tunnelUrl);
583
+ }
584
+
542
585
  const watchTunnelUrl = (chunk) => {
543
586
  const text = chunk.toString();
544
587
  process.stderr.write(text);
545
- if (!printedUrl) {
588
+ if (/status 429|Tunnelstore request failed/i.test(text)) {
589
+ tunnelRateLimited = true;
590
+ }
591
+ if (!printedUrl && !useNamedTunnel) {
546
592
  const match = text.match(/https:\/\/[a-z0-9-]+\.trycloudflare\.com/i);
547
593
  if (match) {
548
594
  printedUrl = true;
@@ -560,9 +606,18 @@ async function main() {
560
606
  cf.on('exit', (code) => {
561
607
  if (!shuttingDown) {
562
608
  console.error(`[Error] cloudflared 已退出 (code=${code})`);
563
- if (!printedUrl) {
609
+ if (tunnelRateLimited) {
610
+ console.error(
611
+ '[Hint] Cloudflare 临时隧道触发 429 限流(通常是 VPN/TUN 节点共享出口 IP 请求过多所致)。\n' +
612
+ ' 可选解决方式(无需关闭 TUN):\n' +
613
+ ' 1) 在代理规则中将 cloudflared / *.trycloudflare.com / *.argotunnel.com 设为 DIRECT(直连);\n' +
614
+ ' 2) 切换冷门代理节点或稍等 1~5 分钟重试(若走环境变量代理可加 --tunnel-direct);\n' +
615
+ ' 3) 使用自有 Cloudflare Named Tunnel(设置 CLOUDFLARE_TUNNEL_TOKEN 或 --tunnel-token);\n' +
616
+ ' 4) 仅本机使用时加 --no-tunnel。'
617
+ );
618
+ } else if (!printedUrl) {
564
619
  console.error(
565
- '[Hint] Cloudflare Tunnel 未能建立连接。若开启了 VPN TUN 模式,请将 cloudflared 或 *.argotunnel.com 设为直连,或使用 --no-tunnel 仅启动本地服务。'
620
+ '[Hint] Cloudflare Tunnel 未能建立连接。若开启了 VPN TUN 模式,请在代理规则中将 cloudflared / *.trycloudflare.com / *.argotunnel.com 设为 DIRECT(直连),或尝试 --tunnel-direct / --no-tunnel。'
566
621
  );
567
622
  }
568
623
  shutdown(code || 1);