mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-28 15:32:13 +00:00
官方端点(api.x.ai / cli-chat-proxy.grok.com)偶发不可用,运营方需要在 端点间手动切换。旧语义把 OAuth 账号存储的官方 host 一律视同"未定制"并 回落默认 CLI 网关:填了官方地址保存成功却不生效、重新编辑开关回到关闭、 再次保存直接删值,形成"修改不生效"的静默循环。 后端: - GetGrokBaseURL OAuth 分支改为"存了什么用什么":官方 API / 区域 API / 第三方转发地址一律按填写值转发与探测,仅空值或无法解析的脏数据回落 默认 CLI 网关;删除官方变体运行时迁移逻辑 - *.api.x.ai 区域端点(us-east-1/us-west-2/eu-west-1 等)纳入可信 host, OAuth 使用时不受运营方 URL 白名单限制,官方 host 仍强制 /v1 path - 修复 SSO 批量建号 MergeCredentials 方向缺陷:BuildAccountCredentials 恒写官方 base_url,会覆盖导入请求指定的自定义转发地址;抽出 grokSSOImportCredentials 显式保留请求值(与 RefreshAccountToken 对齐) 前端: - isCustomGrokBaseUrl 仅默认 CLI 网关 host 视同未定制:api.x.ai 与区域 端点保存后正常回显(开关开启 + 显示地址),不再被静默吞掉 - 新增 GrokBaseUrlPresets 快捷端点组件(Grok Build CLI / 官方 API / us-east-1 / us-west-2 / eu-west-1),接入编辑(OAuth 自定义区 + apikey Base URL)、新增(同前)与批量编辑(所选平台全为 grok 时显示,点击 自动勾选 base_url);仅快速填充,输入框仍可自由填写任意第三方地址
303 lines
10 KiB
Go
303 lines
10 KiB
Go
//go:build unit
|
|
|
|
package service
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/xai"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGrokAPIKeyURLPolicyFollowsGlobalSecurityConfig(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeAPIKey,
|
|
Credentials: map[string]any{
|
|
"base_url": "http://grok.example.test/v1",
|
|
},
|
|
}
|
|
|
|
t.Run("insecure HTTP enabled with allowlist disabled", func(t *testing.T) {
|
|
cfg := &config.Config{}
|
|
cfg.Security.URLAllowlist.Enabled = false
|
|
cfg.Security.URLAllowlist.AllowInsecureHTTP = true
|
|
|
|
responsesURL, err := buildGrokResponsesURL(account, cfg)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "http://grok.example.test/v1/responses", responsesURL)
|
|
|
|
chatURL, err := buildGrokChatCompletionsURL(account, cfg)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "http://grok.example.test/v1/chat/completions", chatURL)
|
|
|
|
mediaURL, err := buildGrokMediaURL(account, cfg, GrokMediaEndpointImagesGenerations, "")
|
|
require.NoError(t, err)
|
|
require.Equal(t, "http://grok.example.test/v1/images/generations", mediaURL)
|
|
})
|
|
|
|
t.Run("insecure HTTP disabled", func(t *testing.T) {
|
|
cfg := &config.Config{}
|
|
cfg.Security.URLAllowlist.Enabled = false
|
|
cfg.Security.URLAllowlist.AllowInsecureHTTP = false
|
|
|
|
_, err := buildGrokResponsesURL(account, cfg)
|
|
require.EqualError(t, err, "invalid base url: base URL rejected by URL security policy")
|
|
})
|
|
|
|
t.Run("enabled allowlist remains HTTPS only", func(t *testing.T) {
|
|
cfg := &config.Config{}
|
|
cfg.Security.URLAllowlist.Enabled = true
|
|
cfg.Security.URLAllowlist.AllowInsecureHTTP = true
|
|
cfg.Security.URLAllowlist.UpstreamHosts = []string{"grok.example.test"}
|
|
|
|
_, err := buildGrokResponsesURL(account, cfg)
|
|
require.EqualError(t, err, "invalid base url: base URL rejected by URL security policy")
|
|
})
|
|
}
|
|
|
|
func TestGrokAPIKeyURLPolicyAppliesAllowlistAndPrivateHostControls(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeAPIKey,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://grok.example.test/v1",
|
|
},
|
|
}
|
|
cfg := &config.Config{}
|
|
cfg.Security.URLAllowlist.Enabled = true
|
|
cfg.Security.URLAllowlist.UpstreamHosts = []string{"grok.example.test"}
|
|
|
|
target, err := buildGrokResponsesURL(account, cfg)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "https://grok.example.test/v1/responses", target)
|
|
|
|
cfg.Security.URLAllowlist.UpstreamHosts = []string{"other.example.test"}
|
|
_, err = buildGrokResponsesURL(account, cfg)
|
|
require.EqualError(t, err, "invalid base url: base URL rejected by URL security policy")
|
|
|
|
account.Credentials["base_url"] = "https://127.0.0.1/v1"
|
|
cfg.Security.URLAllowlist.UpstreamHosts = []string{"127.0.0.1"}
|
|
_, err = buildGrokResponsesURL(account, cfg)
|
|
require.EqualError(t, err, "invalid base url: base URL rejected by URL security policy")
|
|
|
|
cfg.Security.URLAllowlist.AllowPrivateHosts = true
|
|
target, err = buildGrokResponsesURL(account, cfg)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "https://127.0.0.1/v1/responses", target)
|
|
}
|
|
|
|
func TestGrokAPIKeyURLPolicyRedactsMalformedConfiguredURL(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeAPIKey,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://%zz:secret@grok.example.test/v1",
|
|
},
|
|
}
|
|
cfg := &config.Config{}
|
|
cfg.Security.URLAllowlist.AllowInsecureHTTP = true
|
|
|
|
_, err := buildGrokResponsesURL(account, cfg)
|
|
require.EqualError(t, err, "invalid base url: base URL rejected by URL security policy")
|
|
require.NotContains(t, err.Error(), "secret")
|
|
}
|
|
|
|
func TestGrokOAuthURLPolicy(t *testing.T) {
|
|
t.Run("default CLI gateway always allowed under restrictive allowlist", func(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{},
|
|
}
|
|
cfg := &config.Config{}
|
|
cfg.Security.URLAllowlist.Enabled = true
|
|
cfg.Security.URLAllowlist.UpstreamHosts = []string{"other.example.test"}
|
|
|
|
target, err := buildGrokResponsesURL(account, cfg)
|
|
require.NoError(t, err)
|
|
require.Equal(t, xai.DefaultCLIBaseURL+"/responses", target)
|
|
})
|
|
|
|
t.Run("stored official API endpoint is honored (manual endpoint switch)", func(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{
|
|
"base_url": xai.DefaultBaseURL,
|
|
},
|
|
}
|
|
cfg := &config.Config{}
|
|
|
|
target, err := buildGrokResponsesURL(account, cfg)
|
|
require.NoError(t, err)
|
|
require.Equal(t, xai.DefaultBaseURL+"/responses", target)
|
|
})
|
|
|
|
t.Run("stored regional API endpoint is trusted even under restrictive allowlist", func(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://us-west-2.api.x.ai/v1",
|
|
},
|
|
}
|
|
cfg := &config.Config{}
|
|
cfg.Security.URLAllowlist.Enabled = true
|
|
cfg.Security.URLAllowlist.UpstreamHosts = []string{"other.example.test"}
|
|
|
|
target, err := buildGrokResponsesURL(account, cfg)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "https://us-west-2.api.x.ai/v1/responses", target)
|
|
})
|
|
|
|
t.Run("custom forwarding address follows operator policy", func(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://relay.example.test/v1",
|
|
},
|
|
}
|
|
cfg := &config.Config{}
|
|
cfg.Security.URLAllowlist.Enabled = false
|
|
|
|
target, err := buildGrokResponsesURL(account, cfg)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "https://relay.example.test/v1/responses", target)
|
|
})
|
|
|
|
t.Run("custom path prefix is preserved", func(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://relay.example.test/xai/v1",
|
|
},
|
|
}
|
|
cfg := &config.Config{}
|
|
|
|
target, err := buildGrokResponsesURL(account, cfg)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "https://relay.example.test/xai/v1/responses", target)
|
|
})
|
|
|
|
t.Run("custom forwarding address rejected by allowlist", func(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://relay.example.test/v1",
|
|
},
|
|
}
|
|
cfg := &config.Config{}
|
|
cfg.Security.URLAllowlist.Enabled = true
|
|
cfg.Security.URLAllowlist.UpstreamHosts = []string{"other.example.test"}
|
|
|
|
_, err := buildGrokResponsesURL(account, cfg)
|
|
require.EqualError(t, err, "invalid base url: base URL rejected by URL security policy")
|
|
})
|
|
|
|
t.Run("insecure HTTP custom address requires operator opt-in", func(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{
|
|
"base_url": "http://relay.example.test/v1",
|
|
},
|
|
}
|
|
cfg := &config.Config{}
|
|
cfg.Security.URLAllowlist.Enabled = false
|
|
cfg.Security.URLAllowlist.AllowInsecureHTTP = false
|
|
|
|
_, err := buildGrokResponsesURL(account, cfg)
|
|
require.EqualError(t, err, "invalid base url: base URL rejected by URL security policy")
|
|
|
|
cfg.Security.URLAllowlist.AllowInsecureHTTP = true
|
|
target, err := buildGrokResponsesURL(account, cfg)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "http://relay.example.test/v1/responses", target)
|
|
})
|
|
|
|
t.Run("unsafe override switch does not relax the operator allowlist for custom hosts", func(t *testing.T) {
|
|
// XAI_ALLOW_UNSAFE_URL_OVERRIDES relaxes the trusted-host validator to
|
|
// accept-any; a custom OAuth forwarding host must still be governed by
|
|
// the operator allowlist so the bearer token cannot reach arbitrary hosts.
|
|
t.Setenv(xai.EnvAllowUnsafeURLOverrides, "true")
|
|
cfg := &config.Config{}
|
|
cfg.Security.URLAllowlist.Enabled = true
|
|
cfg.Security.URLAllowlist.UpstreamHosts = []string{"cli-chat-proxy.grok.com"}
|
|
|
|
custom := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{
|
|
"base_url": "http://10.0.0.1/v1",
|
|
},
|
|
}
|
|
_, err := buildGrokResponsesURL(custom, cfg)
|
|
require.EqualError(t, err, "invalid base url: base URL rejected by URL security policy")
|
|
|
|
// The official gateway still resolves even under the restrictive allowlist.
|
|
official := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{},
|
|
}
|
|
target, err := buildGrokResponsesURL(official, cfg)
|
|
require.NoError(t, err)
|
|
require.Equal(t, xai.DefaultCLIBaseURL+"/responses", target)
|
|
})
|
|
}
|
|
|
|
func TestGrokBillingURLFollowsAccountBaseURL(t *testing.T) {
|
|
t.Run("oauth default stays on CLI gateway", func(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{},
|
|
}
|
|
|
|
weeklyURL, err := buildGrokBillingURL(account, nil, true)
|
|
require.NoError(t, err)
|
|
require.Equal(t, xai.DefaultCLIBaseURL+"/billing?format=credits", weeklyURL)
|
|
|
|
monthlyURL, err := buildGrokBillingURL(account, nil, false)
|
|
require.NoError(t, err)
|
|
require.Equal(t, xai.DefaultCLIBaseURL+"/billing", monthlyURL)
|
|
})
|
|
|
|
t.Run("oauth custom forwarding address carries billing probes", func(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://relay.example.test/v1",
|
|
},
|
|
}
|
|
|
|
weeklyURL, err := buildGrokBillingURL(account, nil, true)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "https://relay.example.test/v1/billing?format=credits", weeklyURL)
|
|
})
|
|
|
|
t.Run("billing probe honors the operator allowlist like forwarding", func(t *testing.T) {
|
|
// Probe paths must share the forwarding URL policy so a custom host the
|
|
// allowlist rejects cannot receive the OAuth bearer via a billing probe.
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://relay.example.test/v1",
|
|
},
|
|
}
|
|
cfg := &config.Config{}
|
|
cfg.Security.URLAllowlist.Enabled = true
|
|
cfg.Security.URLAllowlist.UpstreamHosts = []string{"cli-chat-proxy.grok.com"}
|
|
|
|
_, err := buildGrokBillingURL(account, cfg, true)
|
|
require.EqualError(t, err, "invalid base url: base URL rejected by URL security policy")
|
|
})
|
|
}
|