[Bug] Config sync can create a self-referential Remote magpie forwarding loop
中文说明
摘要
配置同步可能把一个 Remote magpie Provider 同步到它所指向的 Magpie 实例本身。Provider 在目标机器上仍保持启用时,请求会被转发回同一个网关,形成无界的自调用回环。
这次事故使服务器网关和 Web UI 逐渐变慢并最终超时。仅重启只能短暂恢复;将该 Provider 设为关闭后重启,服务立即恢复。
环境
- Magpie 服务端:v0.1.1591
- 客户端:macOS 桌面版 Magpie
- 配置同步:已启用,手动同步模式(
autoEvery=-1) - Provider 类型:
Remote magpie - Provider id:
remote-magpie - endpoint 为服务器自身的 Magpie API 地址(此处脱敏)
- 未附带凭据或私有配置
事故经过
- Mac 上的
Remote magpie指向常驻的 OPS Magpie 服务器。 - Mac 配置同步到 OPS 服务器。
- OPS 接收后将该 Provider 作为普通启用 Provider 使用,没有识别 endpoint 指向自身。
- 请求进入该 Provider 后又回到 OPS 网关,网关再次选择相同 Provider。
- 网关累计了大量嵌套 passthrough 请求和 keepalive 流;CPU/iowait、内存和 swap 持续升高,Web UI/API 开始超时。
- 只重启服务只能暂时恢复,配置未改变时问题会再次出现。
- 将
remote-magpie.off=true,保留其他 Provider 字段和凭据,然后重启服务。之后观测到:/v1/models:HTTP 200- 模型数:248
remote-magpie模型数:0- 响应延迟:约 44 ms
- 服务内存:约 0.9 GiB
- swap:0
- 1 分钟 CPU busy:约 24%
最小复现
机器 A 配置:
Machine A Magpie -> Remote magpie -> Machine B Magpie
将 Provider 配置同步到机器 B。机器 B 现在会变成:
Machine B Magpie -> Remote magpie -> Machine B Magpie -> ...
任何经该 Provider 路由的请求都会重复进入同一个网关,直到资源和请求超时耗尽。
当前行为
- 同步把 Provider 作为目标机器上的普通启用 Provider 应用。
- 模型发现和请求路由都可能使用自指向 Provider。
- 没有明显的同步前检查、失败关闭或回环提示。
- 一个配置错误被放大成请求递归和资源耗尽。
期望行为
至少应提供以下一项保护:
- 同步/导入时识别 Remote magpie endpoint 指向当前实例,并在该机器上保持 Provider 关闭,同时展示可操作的原因。
- Provider 定义可以保留,但本机激活前必须阻止或要求明确确认。
- 运行时检测重复的 Magpie 实例并在递归前返回有界的
508 Loop Detected(或等价错误)。
本机的安全关闭状态应能跨后续同步保留;同步来的 off=false 不应覆盖当前实例的自指向保护。
设计建议
1. 按机器隔离 Provider 激活状态
Provider 的 endpoint、模型设置和加密凭据可以共享,但启用状态需要按机器评估。最小可行方案:
- 自指向检查通过后才允许本机启用。
- 自指向时保留本地
off覆盖。 - Providers 页面显示“已在本机关闭:endpoint 指向当前 Magpie 实例”。
- endpoint 或远端实例身份变化后重新评估。
这与 #2438 的设备级配置同步、#3032 的按机器 Library 项目互补。
2. 实例身份和 hop 防护
仅比较 URL 不可靠,因为同一实例可能通过公网域名、内网地址、代理或负载均衡访问。建议:
- 提供稳定的 Magpie instance id;
- 透传
X-Magpie-Instance-ID; - 透传并限制
X-Magpie-Hop; - 当前实例 id 已在链路中出现,或 hop 超限时,直接返回明确错误。
该防护应覆盖模型发现、chat/responses、流式请求和 Remote magpie 使用的 usage/health 路径。
3. 同步前验证和安全恢复
启用同步来的 Remote Provider 前:
- 规范化并探测 endpoint;
- 比较远端返回的 instance id 与当前实例;
- 保存旧本地配置以便恢复;
- 探测不确定或确认自指向时失败关闭并显示诊断。
建议只禁止激活,不自动删除 Provider 或凭据。
验收建议
使用两个隔离的本地 Magpie 实例验证:
- A -> B 正常工作;
- B -> B 在模型发现/请求前被拒绝或自动关闭;
- A -> B -> A 被 hop 防护限制;
- 公网域名、loopback、内网 IP、代理别名能识别为同一实例;
- 真正的远端 Provider 仍能启用;
- 本机自指向关闭状态不会被后续同步重新打开;
- 诊断日志不暴露凭据。
相关议题
- #2438:设备级配置同步与独立的每机选择
- #3032:按机器启用 Library 项目
- #3356:重新启用同步部分可能覆盖更新的服务端内容
- #2345 / #2821:直连远端 Magpie、避免不必要的本地转发
本 issue 聚焦的是安全缺口:配置同步能够创建自指向 Remote magpie Provider,而运行时没有回环保护。
English version
Summary
Configuration sync can import a Remote magpie provider onto the Magpie instance that the provider points to. If the provider remains enabled on that destination machine, requests are forwarded back to the same gateway and recurse indefinitely.
This incident made the server gateway and web UI progressively slow and eventually time out. Restarting alone only recovered the service temporarily. Setting the provider to off=true and restarting immediately restored normal behavior.
Environment
- Magpie server: v0.1.1591
- Client: macOS desktop Magpie
- Configuration sync: enabled, manual mode (
autoEvery=-1) - Provider type/id:
Remote magpie/remote-magpie - The endpoint was the server's own Magpie API URL (redacted here)
- No credentials or private configuration are attached
Incident timeline
- The Mac configured
Remote magpieto point to the always-on OPS Magpie server. - The Mac configuration was synced to the OPS server.
- The server accepted the provider as an enabled provider without recognizing that the endpoint resolved to itself.
- Requests entered the provider and returned to the same gateway, which selected the same provider again.
- Nested passthrough requests and keepalive streams accumulated; CPU/iowait, memory and swap increased, and the UI/API timed out.
- Restarting without changing the configuration only provided temporary recovery.
- Setting
remote-magpie.off=true, preserving all other provider fields and credentials, then restarting stopped the loop. The follow-up checks showed HTTP 200 for/v1/models, 248 models, zero models fromremote-magpie, about 44 ms latency, about 0.9 GiB service memory, zero swap, and about 24% one-minute CPU busy.
Minimal reproduction
Configure Machine A as:
Machine A Magpie -> Remote magpie -> Machine B Magpie
Sync the provider configuration to Machine B. Machine B then becomes:
Machine B Magpie -> Remote magpie -> Machine B Magpie -> ...
A request routed through that provider repeatedly re-enters the same gateway until resources and request timeouts are exhausted.
Actual behavior
- Sync applies the provider as a normal enabled provider on the destination machine.
- Model discovery and request routing can use the self-referential provider.
- There is no visible preflight warning, fail-closed state, or loop diagnostic.
- A configuration mistake becomes request amplification and resource exhaustion.
Expected behavior
At least one of these safeguards should apply:
- Sync/import detects that the Remote magpie endpoint is the current instance and keeps the provider disabled on that machine with an actionable explanation.
- The provider definition may remain portable, but local activation is blocked until the endpoint is changed or explicitly confirmed.
- Runtime forwarding detects a repeated Magpie instance and returns a bounded
508 Loop Detected(or equivalent) before recursion.
A local safety override should survive later syncs; a synced off=false must not silently override self-reference protection.
Suggested design
1. Machine-scoped provider activation
The endpoint, model settings and encrypted credential may be shared, but activation should be evaluated per machine. A minimal first step is to preserve a destination-local off override when the endpoint resolves to the current instance, show the reason in the Providers UI, and re-evaluate when the endpoint or advertised instance identity changes.
This is complementary to #2438 (device-scoped configuration sync) and #3032 (per-machine Library items).
2. Instance identity and hop guard
URL comparison alone is fragile because one instance may be reached through a public hostname, private address, proxy, or load balancer. Consider a stable Magpie instance id plus:
X-Magpie-Instance-ID- bounded
X-Magpie-Hop
Reject a request when the current instance id is already present in the chain or the hop limit is exceeded. Cover model discovery, chat/responses, streaming, and Remote magpie usage/health paths.
3. Sync validation and safe recovery
Before enabling a synced Remote provider:
- canonicalize and probe the endpoint;
- compare its instance id with the destination instance;
- preserve the previous local configuration as a recoverable backup;
- fail closed with a visible diagnostic if the endpoint is self-referential or inconclusive.
This should block activation without deleting the user's provider or credential.
Acceptance tests
Use two isolated Magpie instances:
- A -> B works normally;
- B -> B is rejected or disabled before model discovery/request forwarding;
- A -> B -> A is bounded by the hop guard;
- public-hostname, loopback, private-IP and proxy aliases of the same instance are detected;
- a genuinely remote provider remains enabled;
- a local self-reference override remains disabled after subsequent sync;
- diagnostics never expose credential values.
Related issues
- #2438 — device-scoped configuration sync and independent per-machine choices.
- #3032 — per-machine Library items.
- #3356 — re-enabling a sync part can overwrite newer server content.
- #2345 / #2821 — direct remote Magpie usage and avoiding an unnecessary local forwarding hop.
This issue focuses on the safety gap: configuration sync can create a self-referential Remote magpie provider, and runtime forwarding has no loop guard.
根因在 internal/gateway/remote_magpie.go:passOnCaller 会把 agent 和来源机器告诉远端 magpie,但没有任何地方记录一个请求已经过多少个 magpie。所以当同步把一个指向本机的 Remote magpie provider 带到目标机器时,转发链没有终点:请求回到同一个网关,网关又选中同一个 provider。
fix/3834-config-sync-can-create-a 给请求加了一个跳数。每次转发到 Remote magpie 都把 X-Magpie-Hop 在收到的值上加一(passOnCaller),收到的请求和其余 X-Magpie-* 头一起被摘掉(withCaller)。已经过 4 个 magpie 的请求在再次转发之前直接返回 508,并在消息里说明是某个 Remote magpie provider 指进了自己的链路、该关掉或指向它真正指向的机器。五处转发到 Remote magpie 的路径(会话请求、decide、draw、embeddings、video)都经过 passOnCaller,因此一并覆盖;厂商收不到这个头;旧版 magpie 不发也不认,混用不会被误拒。
这条链路我按 issue 里的第三个方案做运行时兜底,没有做「同步时按机器保持 provider 关闭」那一步——那需要产品决定(本机覆盖状态、Providers 页的提示),没有在这个分支里替你们决定。
fix/3834-config-sync-can-create-a, commit 142350b342ad82f3dbd464281f480eacd3adf413
验证:新增 TestRemoteMagpieLoopIsRefused(internal/gateway/remote_magpie_loop_test.go)。把产品代码还原后它失败:the request went on with X-Magpie-Hop "", want "1" 和 a request through 8 magpies: 200 {...}, want 508;改动后通过(含 -race -count=2)。gofmt -l 无输出,go build ./... 和 go vet ./internal/gateway/ 通过。完整的 go test ./internal/gateway/ -count=1 没有跑完(中途停掉,结果未知;此前一次运行退出码为 1 但输出没抓到),合并前请先跑一遍整包测试。
这是 fork 上的分支,供 review 用,不主张该 issue 的归属。
补充一个 macOS 上的现场:客户端和共享端均为 magpie 0.1.2119,使用同一个配置同步目标。共享端也有一个启用中的 Remote magpie,其终结点与客户端连接共享端网关时使用的地址相同;同步页面显示,共享端的供应商配置曾被另一台电脑的较新版本替换。
客户端的远程模型缓存两次检查相隔约 11 分钟:图像模型记录从 478 条增至 479 条,模型 ID 中连续的 remote-magpie/ 前缀最深从 477 层增至 478 层;缓存约 11 MB。供应商编辑界面的图像模型名称严重重叠,难以使用。
这些观察与同步后的自引用相吻合,但尚未独立核实全部网络地址与转发关系,也没有在隔离环境中复现。实际对话和生图转发未测试。
希望同时考虑对模型发现、缓存增长和超长名称显示增加防护;请求转发的 hop 防护是否也能阻止模型目录膨胀,需要单独验证。
本报告未包含网络地址、账号、密钥、截图或原始日志。
lostornot 的现场报告(图像模型记录 11 分钟 478→479 条、remote-magpie/ 前缀 477→478 层、缓存约 11 MB)对上了这个分支补的第二处漏洞:上一条评论的请求跳数防护拦不住模型发现。跳数计在转发的请求上(X-Magpie-Hop),而模型发现不是转发——每台 magpie 各自去对方的 /v1/models 取列表,单独验证的结论是:它确实拦不住目录膨胀。所以分支上现在有第二道防护。
机制:一台 magpie 对外提供目录时,每个模型 id 都带着它所属 provider 的 id(internal/provider/models.go 的 entryFor:p.ID + "/" + m.ID;图像、视频列表在 gateway.go 的 drawerObjects/videomakerObjects 同样拼一层);取回的一端把这些 id 当作模型自己的 id 原样保存,自己再对外提供时又叠一层自己的。Remote magpie provider 的默认 id 就是 remote-magpie,配置同步把同一个 provider 带到两端、其中一端的地址又指回链路里之后,每取一次列表就给同一个模型多叠一层 remote-magpie/——正好是现场看到的增长方式。
修法与跳数防护同一个思路,只拒绝一层:模型 id 一旦已经带 remote-magpie/ 限定(provider.RemoteNested),就既不从另一台 magpie 的列表里保留(fetchOne 包住了所有取列表的路径——普通、多 key、添加表单的 List、探测、预填),也不再出现在给另一台 magpie 的对话/图像/视频列表里(gateway.go 的 models/drawerObjects/videomakerObjects,只对带 X-Magpie-Drawers 的另一台 magpie 生效)。两边任一处单独就能止住叠加;两边都做,是让新旧版本混用时也止住:旧版污染不了已更新的,已更新的也不把旧版会叠的 id 发出去。本机自己的 agent 仍能看到这种模型;成环的请求仍由跳数防护以 508 结束;已被污染的缓存不需要手动清理——下一次取列表整表换掉,里面不再有嵌套 id 可保留。
不在这次范围内的两件事,如报告所提:已经长出来的缓存(重启,或删掉重加该 provider,即可清掉;更新后第一次刷新列表也会把它换掉)和超长名称在 UI 里的显示(供应商编辑界面图像模型名重叠是 UI 自身的问题,应另行处理)。
fix/3834-config-sync-can-create-a,head b0263ae742d4afe160083b7008ae6ebd2f192588(跳数防护那条提交已 rebase 到最新 main 为 c291f2bcc,本提交在其上)。
验证:新增 TestRemoteMagpieNestingDoesNotCompound、TestRemoteMagpieNestedNotServed(internal/gateway/remote_magpie_nested_test.go)。把产品代码还原后它们失败,失败的样子就是现场的增长:round 2: remote-magpie/relay-a/claude-sonnet-5 kept、round 3: remote-magpie/remote-magpie/relay-a/claude-sonnet-5 kept(每轮多一层,图像模型同样),嵌套 id 也会出现在发给另一台 magpie 的列表里;改动后通过(含 -race -count=2;跳数防护的 TestRemoteMagpieLoopIsRefused 保持通过)。gofmt -l 无输出,go build ./... 与 go vet ./internal/gateway/ ./internal/provider/ 通过,go test ./internal/provider/ ./internal/catalog/ -count=1 通过。完整的 go test ./internal/gateway/ -count=1 有 6 个失败(TestClaudeClientCacheAbsentUnchanged、TestCodexPoolSaysTheClientsVersion、TestMarshalRawIsEncodingJSON、TestMarshalSizedIsJSONMarshal、FuzzMarshalRaw、TestServeWSLListensBesideLoopback),每个都在未改动的同一基线上复现、都不在 remote-magpie 路径上(fastjson 的 \u2028 转义、本机没有 WSL、codex 版本测试只与其他测试同跑时失败),属本机环境问题,与本次改动无关;合并前请在它们能通过的环境跑一遍整包。
这是 fork 上的分支,供 review 用,不主张该 issue 的归属。