feat(mcp): add a source allowlist for the MCP server endpoint

Restrict which source addresses may call /mcp with a list of IPs/CIDRs
(IPv4 and IPv6). The check runs before token authentication, so a source
that is not listed is refused with 403 even when it holds a valid token.
An empty list keeps the previous behaviour (any source).

Behind a reverse proxy the TCP peer is always the proxy, so proxies can be
registered as trusted: for requests from them the client is the rightmost
X-Forwarded-For hop that is not itself a trusted proxy. Without a trusted
proxy the header is ignored, so a caller cannot claim an allowed address.

- A stored configuration that cannot be read or parsed refuses requests
  instead of falling back to no restriction.
- The audit log records the resolved client address.
- The settings API reports the address the server sees for the caller.
- The allowlist applies to /mcp only; the web UI and admin API are
  unaffected.

Refs RuoJi6/ARTEX#144
This commit is contained in:
ji ruo committed 2026-11-14 14:32:39 +08:00
1 parent 8cf118e34d
commit 0a0158a4ab
9 files changed
+523 -19

No files matched your search

+6
View File
@@ -169,6 +169,10 @@ export interface McpServerSettings {
enabled: boolean;
path: string;
basic_auth_enabled: boolean;
allowed_ips: string[];
trusted_proxies: string[];
// 服务端按 /mcp 的同一套规则解析出的、当前管理员请求的来源地址。
client_ip?: string;
}
export interface McpServerToken {
@@ -1148,6 +1152,8 @@ export const api = {
// ---- MCP 服务端(ARTEX 对外暴露的 /mcp):开关、访问令牌、调用审计 ----
mcpServerSettings: () => get<McpServerSettings>("/mcp-server/settings"),
setMcpServerEnabled: (enabled: boolean) => put<McpServerSettings>("/mcp-server/settings", { enabled }),
setMcpServerAccess: (access: { allowed_ips: string[]; trusted_proxies: string[] }) =>
put<McpServerSettings>("/mcp-server/settings", access),
mcpServerTokens: () => get<{ tokens: McpServerToken[] }>("/mcp-server/tokens").then((r) => arr(r.tokens)),
// 明文令牌只在创建响应里返回这一次。
createMcpServerToken: (input: McpServerTokenInput) =>