mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-07 09:47:23 +00:00
插件出站白名单 = 已配置源的 host。这条规则对每个 adapter 都成立,直到
OpenDesign —— 它把推理和计费拆在两个域上:推理 amr-link,计费 amr-api。
adapter 从推理主机推导计费主机,于是它请求的 host 从来不是操作员配过的
那个,请求在离开进程前就被拒,报"不是允许的出站目标",读起来像插件 bug。
core.sourceHosts 现在同时加入派生主机,且仅当源本身就在 amr-link 上
(没有 OpenDesign 源的部署不会凭空获得这条授权)。
派生规则前后端各存一份,因为 Go 建白名单、Lua 建 URL,两边不一致的表现是
「请求被拒」而不是「命名不匹配的错误」。规则改成了完整主机名匹配:
`amr-link.` 前缀写法会把 amr-link.example.com 也改写成 OpenDesign 地址。
顺带修掉一个真缺陷:Lua 侧先 `gsub("/+$")` 再匹配主机名,而 base_url 是
`https://amr-link.open-design.ai/v1` —— 带路径时整串拿去匹配 `^...$`
永远不命中,改写静默失效,探测打到推理主机。改成先切路径。
这个缺陷是「行为断言」抓到的:上一版判据 grep 源码里的 gsub 模式,而
gsub 模式在改前改后都"看起来对"。8/8 变异全杀。
(cherry picked from commit 4a63b9dc0b)
109 lines
4.1 KiB
Go
109 lines
4.1 KiB
Go
package core
|
|
|
|
import (
|
|
"net/url"
|
|
"testing"
|
|
|
|
"llmsproxy/internal/config"
|
|
)
|
|
|
|
// The plugin outbound allowlist is built from the hosts of configured sources.
|
|
// That rule is correct for every adapter whose credential and endpoint live on
|
|
// the same host — which was all of them until OpenDesign.
|
|
//
|
|
// OpenDesign splits them: inference is amr-link.open-design.ai, billing is
|
|
// amr-api.open-design.ai. The quota plugin derives the billing host from the
|
|
// inference one, so the host it asks for is NOT the host the operator
|
|
// configured. The request is then refused by the allowlist with a message that
|
|
// reads like a plugin defect ("host ... is not an allowed outbound destination")
|
|
// rather than a missing entry.
|
|
//
|
|
// This file pins the bridge: when a source looks like OpenDesign, the derived
|
|
// billing host joins the allowlist.
|
|
|
|
func TestSourceHostsIncludesConfiguredHosts(t *testing.T) {
|
|
cfg := &config.Config{Sources: []config.Source{
|
|
{Name: "a", BaseURL: "https://api.deepseek.com"},
|
|
{Name: "b", BaseURL: "https://amr-link.open-design.ai/v1"},
|
|
{Name: "empty", BaseURL: ""},
|
|
{Name: "bad", BaseURL: "://nonsense"},
|
|
}}
|
|
got := map[string]bool{}
|
|
for _, h := range sourceHosts(cfg) {
|
|
got[h] = true
|
|
}
|
|
for _, want := range []string{"api.deepseek.com", "amr-link.open-design.ai"} {
|
|
if !got[want] {
|
|
t.Errorf("sourceHosts missing configured host %q (allowlist would refuse "+
|
|
"every plugin request to it): %v", want, got)
|
|
}
|
|
}
|
|
// An empty/unparseable base_url must not abort the whole list.
|
|
if !got["api.deepseek.com"] {
|
|
t.Fatal("an unparseable sibling source dropped a valid host")
|
|
}
|
|
}
|
|
|
|
// TestSourceHostsAddsDerivedBillingHost covers the OpenDesign case at the level
|
|
// that actually matters: the host the PLUGIN asks for must be in the allowlist.
|
|
func TestSourceHostsAddsDerivedBillingHost(t *testing.T) {
|
|
cfg := &config.Config{Sources: []config.Source{
|
|
{Name: "vela", BaseURL: "https://amr-link.open-design.ai/v1"},
|
|
}}
|
|
got := map[string]bool{}
|
|
for _, h := range sourceHosts(cfg) {
|
|
got[h] = true
|
|
}
|
|
if !got["amr-link.open-design.ai"] {
|
|
t.Fatalf("inference host missing: %v", got)
|
|
}
|
|
if !got["amr-api.open-design.ai"] {
|
|
t.Fatalf("the billing host the quota plugin derives is not in the allowlist, "+
|
|
"so every OpenDesign balance probe is refused before it leaves the "+
|
|
"process: %v", got)
|
|
}
|
|
}
|
|
|
|
// A deployment with no OpenDesign source must not silently gain its billing
|
|
// host: an allowlist entry is a grant, and widening it for everyone would let an
|
|
// unrelated plugin reach a host the operator never configured.
|
|
func TestSourceHostsDoesNotGrantUnconfiguredBillingHost(t *testing.T) {
|
|
cfg := &config.Config{Sources: []config.Source{
|
|
{Name: "ds", BaseURL: "https://api.deepseek.com"},
|
|
}}
|
|
for _, h := range sourceHosts(cfg) {
|
|
if h == "amr-api.open-design.ai" {
|
|
t.Fatal("the OpenDesign billing host was granted to a deployment with " +
|
|
"no OpenDesign source")
|
|
}
|
|
}
|
|
}
|
|
|
|
// The derivation has to agree with the Lua side. Keep the two spellings of the
|
|
// rule in one place so a change to one is visible when reading the other.
|
|
func TestSourceHostsDerivationMatchesPluginRule(t *testing.T) {
|
|
// what the Lua adapter does: origin:gsub("^amr%-link%.", "amr-api.")
|
|
const in = "amr-link.open-design.ai"
|
|
want := "amr-api.open-design.ai"
|
|
got := deriveBillingHost(in)
|
|
if got != want {
|
|
t.Fatalf("deriveBillingHost(%q) = %q, want %q — the Go allowlist and the "+
|
|
"Lua probe must agree on the host or the request is refused", in, got, want)
|
|
}
|
|
// A host that is not amr-link stays untouched, so this cannot accidentally
|
|
// rewrite an unrelated provider into an OpenDesign address.
|
|
for _, other := range []string{"api.deepseek.com", "amr-link.example.com", "xamr-link.a"} {
|
|
if deriveBillingHost(other) != other {
|
|
t.Errorf("deriveBillingHost(%q) = %q, want it unchanged", other, deriveBillingHost(other))
|
|
}
|
|
}
|
|
// url.Parse round-trip keeps the helper honest about what it receives.
|
|
u, err := url.Parse("https://amr-link.open-design.ai/v1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if deriveBillingHost(u.Hostname()) != "amr-api.open-design.ai" {
|
|
t.Fatal("derivation failed on a real base_url hostname")
|
|
}
|
|
}
|