mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-25 18:11:50 +08:00
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:
1 parent
8cf118e34d
commit
0a0158a4ab
9 files changed
+523
-19
No files matched your search
@@ -30,6 +30,7 @@ import {
|
||||
import { Field, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { localizedError } from "@/i18n/errors";
|
||||
import { useDateLocale } from "@/i18n/use-date-locale";
|
||||
import { useLiveTranslation } from "@/i18n/use-live-translation";
|
||||
@@ -98,6 +99,9 @@ export function McpServerCard() {
|
||||
const [tokens, setTokens] = useState<McpServerToken[]>([]);
|
||||
const [audit, setAudit] = useState<McpServerAuditEntry[]>([]);
|
||||
const [companies, setCompanies] = useState<Company[]>([]);
|
||||
const [allowedIps, setAllowedIps] = useState("");
|
||||
const [trustedProxies, setTrustedProxies] = useState("");
|
||||
const [savingAccess, setSavingAccess] = useState(false);
|
||||
const [error, setError] = useState<unknown>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [createOpen, setCreateOpen] = useState(false);
|
||||
@@ -116,6 +120,8 @@ export function McpServerCard() {
|
||||
api.mcpServerAudit(),
|
||||
]);
|
||||
setSettings(nextSettings);
|
||||
setAllowedIps(nextSettings.allowed_ips.join("\n"));
|
||||
setTrustedProxies(nextSettings.trusted_proxies.join("\n"));
|
||||
setTokens(nextTokens);
|
||||
setAudit(nextAudit);
|
||||
} catch (e) {
|
||||
@@ -146,6 +152,26 @@ export function McpServerCard() {
|
||||
}
|
||||
};
|
||||
|
||||
const saveAccess = async (event: React.FormEvent) => {
|
||||
event.preventDefault();
|
||||
setSavingAccess(true);
|
||||
try {
|
||||
const next = await api.setMcpServerAccess({
|
||||
allowed_ips: allowedIps.split("\n"),
|
||||
trusted_proxies: trustedProxies.split("\n"),
|
||||
});
|
||||
setSettings(next);
|
||||
// Show the canonical form the server stored (trimmed, deduplicated, masked).
|
||||
setAllowedIps(next.allowed_ips.join("\n"));
|
||||
setTrustedProxies(next.trusted_proxies.join("\n"));
|
||||
toast.success(t("settings.mcpServer.allowlistSaved"));
|
||||
} catch (e) {
|
||||
toast.error(t("settings.saveFailed", { message: localizedError(e, t) }));
|
||||
} finally {
|
||||
setSavingAccess(false);
|
||||
}
|
||||
};
|
||||
|
||||
const openCreate = () => {
|
||||
setForm(emptyForm);
|
||||
setCreateError(null);
|
||||
@@ -238,6 +264,43 @@ export function McpServerCard() {
|
||||
) : null}
|
||||
</Field>
|
||||
|
||||
<form onSubmit={saveAccess} className="flex flex-col gap-4">
|
||||
<Field>
|
||||
<FieldLabel htmlFor="mcp-server-allowlist">{t("settings.mcpServer.allowlist")}</FieldLabel>
|
||||
<Textarea
|
||||
id="mcp-server-allowlist"
|
||||
rows={3}
|
||||
className="font-mono text-xs"
|
||||
value={allowedIps}
|
||||
disabled={!settings || savingAccess}
|
||||
placeholder={t("settings.mcpServer.allowlistPlaceholder")}
|
||||
onChange={(e) => setAllowedIps(e.target.value)}
|
||||
/>
|
||||
<FieldDescription>{t("settings.mcpServer.allowlistHelp")}</FieldDescription>
|
||||
{settings?.client_ip ? (
|
||||
<FieldDescription>{t("settings.mcpServer.clientIp", { ip: settings.client_ip })}</FieldDescription>
|
||||
) : null}
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="mcp-server-proxies">{t("settings.mcpServer.trustedProxies")}</FieldLabel>
|
||||
<Textarea
|
||||
id="mcp-server-proxies"
|
||||
rows={2}
|
||||
className="font-mono text-xs"
|
||||
value={trustedProxies}
|
||||
disabled={!settings || savingAccess}
|
||||
placeholder={t("settings.mcpServer.trustedProxiesPlaceholder")}
|
||||
onChange={(e) => setTrustedProxies(e.target.value)}
|
||||
/>
|
||||
<FieldDescription>{t("settings.mcpServer.trustedProxiesHelp")}</FieldDescription>
|
||||
</Field>
|
||||
<div>
|
||||
<Button type="submit" variant="outline" disabled={!settings || savingAccess}>
|
||||
{savingAccess ? t("settings.saving") : t("settings.mcpServer.saveAllowlist")}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{error ? (
|
||||
<div className="flex items-center gap-2">
|
||||
<p role="alert" className="text-destructive text-sm">
|
||||
|
||||
@@ -115,8 +115,8 @@ export const settingsMessages = {
|
||||
"basic auth authentication sign-in login username password",
|
||||
],
|
||||
"settings.keywords.mcpServer": [
|
||||
"MCP 服务端 访问令牌 token 智能体 编排 外部 bot 审计",
|
||||
"mcp server access token agent orchestration external bot audit",
|
||||
"MCP 服务端 访问令牌 token 智能体 编排 外部 bot 审计 白名单 来源 IP",
|
||||
"mcp server access token agent orchestration external bot audit allowlist whitelist source ip",
|
||||
],
|
||||
"settings.trafficCapture": ["流量捕获", "Traffic capture"],
|
||||
"settings.trafficCaptureHelp": [
|
||||
@@ -506,6 +506,24 @@ export const settingsMessages = {
|
||||
"当前已启用 HTTP Basic Auth,/mcp 仍可直接凭访问令牌访问。",
|
||||
"HTTP Basic Auth is currently enabled; /mcp remains reachable with an access token alone.",
|
||||
],
|
||||
"settings.mcpServer.allowlist": ["来源白名单", "Source allowlist"],
|
||||
"settings.mcpServer.allowlistHelp": [
|
||||
"只有这些来源地址能调用 /mcp,每行一个 IP 或 CIDR(如 203.0.113.7 或 10.0.0.0/8)。留空 = 不限制来源。白名单在令牌之前检查:不在名单内的来源即使持有有效令牌也会被拒绝(403)。",
|
||||
"Only these source addresses may call /mcp. One IP or CIDR per line (for example 203.0.113.7 or 10.0.0.0/8). Leave empty for no source restriction. The allowlist is checked before the token: a source that is not listed is rejected (403) even with a valid token.",
|
||||
],
|
||||
"settings.mcpServer.allowlistPlaceholder": ["留空 = 不限制来源", "Empty = no source restriction"],
|
||||
"settings.mcpServer.trustedProxies": ["可信代理", "Trusted proxies"],
|
||||
"settings.mcpServer.trustedProxiesHelp": [
|
||||
"ARTEX 部署在反向代理(nginx 等)之后时,填写代理的地址,每行一个 IP 或 CIDR;来自这些地址的请求改用 X-Forwarded-For 里的地址作为来源。不填则一律以直连地址为准——此时经代理进来的请求来源都是代理本身。不要填不受你控制的地址,否则对方可以伪造来源。",
|
||||
"If ARTEX runs behind a reverse proxy (nginx and the like), list the proxy addresses here, one IP or CIDR per line; requests from them use the address in X-Forwarded-For as the source. Left empty, the direct connection address is always used, so every request arriving through a proxy appears to come from the proxy itself. Never list addresses you do not control, or their owner can forge the source.",
|
||||
],
|
||||
"settings.mcpServer.trustedProxiesPlaceholder": ["没有反向代理则留空", "Leave empty without a reverse proxy"],
|
||||
"settings.mcpServer.clientIp": [
|
||||
"服务器看到的你当前的来源地址:{{ip}}",
|
||||
"Your current source address as the server sees it: {{ip}}",
|
||||
],
|
||||
"settings.mcpServer.saveAllowlist": ["保存来源白名单", "Save source allowlist"],
|
||||
"settings.mcpServer.allowlistSaved": ["来源白名单已保存", "Source allowlist saved"],
|
||||
"settings.mcpServer.copy": ["复制", "Copy"],
|
||||
"settings.mcpServer.copied": ["已复制", "Copied"],
|
||||
"settings.mcpServer.tokens": ["访问令牌", "Access tokens"],
|
||||
|
||||
@@ -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) =>
|
||||
|
||||
Reference in new issue
Block a user