mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
- Reject JSON-RPC batches and non-object bodies before the SDK; return each tool result once as text and bound the encoded payload (H1). - Enable MCP only when MCP_PUBLIC_URL is set; drop the frontend URL fallback and expose a read-only mcp_enabled public setting (M1). - Expire never-authorized clients after 24h, cap pending registrations globally, aggregate MCP IP limits by IPv6 /64, and limit registration bodies to 4 KiB (M2). - Revoke MCP grants with revoke-all-sessions and always require step-up for administrator write grants (M3). - Rate limit /api/v1/mcp per IP before authentication and write last_used_at at most once per minute (M4). - Persistently revoke grants when an account is disabled or demoted, derive result redaction from SensitiveCredentialKeys, allowlist browser sessions for step-up, audit every MCP read, reject invisible characters in client_name, and document site-root-only deployment (L1-L5, L7). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
337 lines
13 KiB
Go
337 lines
13 KiB
Go
//go:build unit
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type settingPublicRepoStub struct {
|
|
values map[string]string
|
|
err error
|
|
}
|
|
|
|
func (s *settingPublicRepoStub) Get(ctx context.Context, key string) (*Setting, error) {
|
|
panic("unexpected Get call")
|
|
}
|
|
|
|
func (s *settingPublicRepoStub) GetValue(ctx context.Context, key string) (string, error) {
|
|
panic("unexpected GetValue call")
|
|
}
|
|
|
|
func (s *settingPublicRepoStub) Set(ctx context.Context, key, value string) error {
|
|
panic("unexpected Set call")
|
|
}
|
|
|
|
func (s *settingPublicRepoStub) GetMultiple(ctx context.Context, keys []string) (map[string]string, error) {
|
|
if s.err != nil {
|
|
return nil, s.err
|
|
}
|
|
out := make(map[string]string, len(keys))
|
|
for _, key := range keys {
|
|
if value, ok := s.values[key]; ok {
|
|
out[key] = value
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (s *settingPublicRepoStub) SetMultiple(ctx context.Context, settings map[string]string) error {
|
|
panic("unexpected SetMultiple call")
|
|
}
|
|
|
|
func (s *settingPublicRepoStub) GetAll(ctx context.Context) (map[string]string, error) {
|
|
panic("unexpected GetAll call")
|
|
}
|
|
|
|
func (s *settingPublicRepoStub) Delete(ctx context.Context, key string) error {
|
|
panic("unexpected Delete call")
|
|
}
|
|
|
|
func TestSettingService_GetPublicSettings_ExposesRegistrationEmailSuffixWhitelist(t *testing.T) {
|
|
repo := &settingPublicRepoStub{
|
|
values: map[string]string{
|
|
SettingKeyRegistrationEnabled: "true",
|
|
SettingKeyEmailVerifyEnabled: "true",
|
|
SettingKeyRegistrationEmailSuffixWhitelist: `["@EXAMPLE.com"," @foo.bar ","*.EDU.CN","@invalid_domain",""]`,
|
|
},
|
|
}
|
|
svc := NewSettingService(repo, &config.Config{})
|
|
|
|
settings, err := svc.GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.Equal(t, []string{"@example.com", "@foo.bar", "*.edu.cn"}, settings.RegistrationEmailSuffixWhitelist)
|
|
}
|
|
|
|
func TestSettingService_GetPublicSettings_ExposesTablePreferences(t *testing.T) {
|
|
repo := &settingPublicRepoStub{
|
|
values: map[string]string{
|
|
SettingKeyTableDefaultPageSize: "50",
|
|
SettingKeyTablePageSizeOptions: "[20,50,100]",
|
|
},
|
|
}
|
|
svc := NewSettingService(repo, &config.Config{})
|
|
|
|
settings, err := svc.GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.Equal(t, 50, settings.TableDefaultPageSize)
|
|
require.Equal(t, []int{20, 50, 100}, settings.TablePageSizeOptions)
|
|
}
|
|
|
|
func TestSettingService_GetPublicSettings_ExposesCompactHomeEnabled(t *testing.T) {
|
|
repo := &settingPublicRepoStub{
|
|
values: map[string]string{
|
|
SettingKeyCompactHomeEnabled: "true",
|
|
},
|
|
}
|
|
svc := NewSettingService(repo, &config.Config{})
|
|
|
|
settings, err := svc.GetPublicSettings(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
require.True(t, settings.CompactHomeEnabled)
|
|
|
|
missingSettings, err := NewSettingService(&settingPublicRepoStub{values: map[string]string{}}, &config.Config{}).
|
|
GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.False(t, missingSettings.CompactHomeEnabled)
|
|
}
|
|
|
|
func TestSettingService_ChannelMonitorHideThroughputDefaultsToPrivate(t *testing.T) {
|
|
missing := NewSettingService(&settingPublicRepoStub{values: map[string]string{}}, &config.Config{}).GetChannelMonitorRuntime(context.Background())
|
|
require.True(t, missing.HideThroughput)
|
|
public, err := NewSettingService(&settingPublicRepoStub{values: map[string]string{}}, &config.Config{}).GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.True(t, public.ChannelMonitorHideThroughput)
|
|
|
|
for _, value := range []string{"false", "0", "off", "disabled"} {
|
|
runtime := NewSettingService(&settingPublicRepoStub{values: map[string]string{
|
|
SettingKeyChannelMonitorHideThroughput: value,
|
|
}}, &config.Config{}).GetChannelMonitorRuntime(context.Background())
|
|
require.False(t, runtime.HideThroughput, "value=%q", value)
|
|
}
|
|
}
|
|
|
|
func TestSettingService_ChannelMonitorShowQuotaFailsClosed(t *testing.T) {
|
|
// 缺省(迁移插入 'false' / 老库无行)一律不展示。
|
|
missingRuntime := NewSettingService(&settingPublicRepoStub{values: map[string]string{}}, &config.Config{}).GetChannelMonitorRuntime(context.Background())
|
|
require.False(t, missingRuntime.ShowQuota)
|
|
missingPublic, err := NewSettingService(&settingPublicRepoStub{values: map[string]string{}}, &config.Config{}).
|
|
GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.False(t, missingPublic.ChannelMonitorShowQuota)
|
|
|
|
// 仅字面 "true" 视为开启;其余值(含异常值)fail-closed。
|
|
runtime := NewSettingService(&settingPublicRepoStub{values: map[string]string{
|
|
SettingKeyChannelMonitorShowQuota: "true",
|
|
}}, &config.Config{}).GetChannelMonitorRuntime(context.Background())
|
|
require.True(t, runtime.ShowQuota)
|
|
|
|
for _, value := range []string{"false", "TRUE", "1", "yes", "on", "garbage"} {
|
|
rt := NewSettingService(&settingPublicRepoStub{values: map[string]string{
|
|
SettingKeyChannelMonitorShowQuota: value,
|
|
}}, &config.Config{}).GetChannelMonitorRuntime(context.Background())
|
|
require.False(t, rt.ShowQuota, "value=%q", value)
|
|
}
|
|
}
|
|
|
|
func TestSettingService_ChannelMonitorHideUserRankingDefaultsToVisible(t *testing.T) {
|
|
missing := NewSettingService(&settingPublicRepoStub{values: map[string]string{}}, &config.Config{}).GetChannelMonitorRuntime(context.Background())
|
|
require.False(t, missing.HideUserRanking)
|
|
public, err := NewSettingService(&settingPublicRepoStub{values: map[string]string{}}, &config.Config{}).GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.False(t, public.ChannelMonitorHideUserRanking)
|
|
|
|
for _, value := range []string{"true", "1", "on", "enabled"} {
|
|
runtime := NewSettingService(&settingPublicRepoStub{values: map[string]string{
|
|
SettingKeyChannelMonitorHideUserRanking: value,
|
|
}}, &config.Config{}).GetChannelMonitorRuntime(context.Background())
|
|
require.True(t, runtime.HideUserRanking, "value=%q", value)
|
|
}
|
|
}
|
|
|
|
func TestSettingService_GetPublicSettings_ExposesForceEmailOnThirdPartySignup(t *testing.T) {
|
|
repo := &settingPublicRepoStub{
|
|
values: map[string]string{
|
|
SettingKeyForceEmailOnThirdPartySignup: "true",
|
|
},
|
|
}
|
|
svc := NewSettingService(repo, &config.Config{})
|
|
|
|
settings, err := svc.GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.True(t, settings.ForceEmailOnThirdPartySignup)
|
|
}
|
|
|
|
func TestSettingService_GetPublicSettings_ExposesAllowUserViewErrorRequests(t *testing.T) {
|
|
repo := &settingPublicRepoStub{
|
|
values: map[string]string{
|
|
SettingKeyAllowUserViewErrorRequests: "true",
|
|
},
|
|
}
|
|
svc := NewSettingService(repo, &config.Config{})
|
|
|
|
settings, err := svc.GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.True(t, settings.AllowUserViewErrorRequests)
|
|
}
|
|
|
|
func TestSettingService_GetPublicSettings_ExposesWeChatOAuthModeCapabilities(t *testing.T) {
|
|
svc := NewSettingService(&settingPublicRepoStub{
|
|
values: map[string]string{
|
|
SettingKeyWeChatConnectEnabled: "true",
|
|
SettingKeyWeChatConnectAppID: "wx-mp-app",
|
|
SettingKeyWeChatConnectAppSecret: "wx-mp-secret",
|
|
SettingKeyWeChatConnectMode: "mp",
|
|
SettingKeyWeChatConnectScopes: "snsapi_base",
|
|
SettingKeyWeChatConnectOpenEnabled: "true",
|
|
SettingKeyWeChatConnectMPEnabled: "true",
|
|
SettingKeyWeChatConnectRedirectURL: "https://api.example.com/api/v1/auth/oauth/wechat/callback",
|
|
SettingKeyWeChatConnectFrontendRedirectURL: "/auth/wechat/callback",
|
|
},
|
|
}, &config.Config{})
|
|
|
|
settings, err := svc.GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.True(t, settings.WeChatOAuthEnabled)
|
|
require.True(t, settings.WeChatOAuthOpenEnabled)
|
|
require.True(t, settings.WeChatOAuthMPEnabled)
|
|
}
|
|
|
|
func TestSettingService_GetPublicSettings_DoesNotExposeMobileOnlyWeChatAsWebOAuthAvailable(t *testing.T) {
|
|
svc := NewSettingService(&settingPublicRepoStub{
|
|
values: map[string]string{
|
|
SettingKeyWeChatConnectEnabled: "true",
|
|
SettingKeyWeChatConnectMobileEnabled: "true",
|
|
SettingKeyWeChatConnectMode: "mobile",
|
|
SettingKeyWeChatConnectMobileAppID: "wx-mobile-app",
|
|
SettingKeyWeChatConnectMobileAppSecret: "wx-mobile-secret",
|
|
SettingKeyWeChatConnectFrontendRedirectURL: "/auth/wechat/callback",
|
|
},
|
|
}, &config.Config{})
|
|
|
|
settings, err := svc.GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.False(t, settings.WeChatOAuthEnabled)
|
|
require.False(t, settings.WeChatOAuthOpenEnabled)
|
|
require.False(t, settings.WeChatOAuthMPEnabled)
|
|
require.True(t, settings.WeChatOAuthMobileEnabled)
|
|
}
|
|
|
|
func TestSettingService_GetPublicSettings_FallsBackToConfigForWeChatOAuthCapabilities(t *testing.T) {
|
|
svc := NewSettingService(&settingPublicRepoStub{values: map[string]string{}}, &config.Config{
|
|
WeChat: config.WeChatConnectConfig{
|
|
Enabled: true,
|
|
OpenEnabled: true,
|
|
OpenAppID: "wx-open-config",
|
|
OpenAppSecret: "wx-open-secret",
|
|
FrontendRedirectURL: "/auth/wechat/config-callback",
|
|
},
|
|
})
|
|
|
|
settings, err := svc.GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.True(t, settings.WeChatOAuthEnabled)
|
|
require.True(t, settings.WeChatOAuthOpenEnabled)
|
|
require.False(t, settings.WeChatOAuthMPEnabled)
|
|
require.False(t, settings.WeChatOAuthMobileEnabled)
|
|
}
|
|
|
|
// subscription_enabled gates the user-facing "My Subscriptions" sidebar entry and
|
|
// is opt-out: only an explicit "false" hides it.
|
|
func TestSettingService_GetPublicSettings_SubscriptionEnabledOnlyExplicitFalseDisables(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
values map[string]string
|
|
want bool
|
|
}{
|
|
{name: "missing key defaults to enabled", values: map[string]string{}, want: true},
|
|
{name: "empty value stays enabled", values: map[string]string{SettingKeySubscriptionEnabled: ""}, want: true},
|
|
{name: "explicit true", values: map[string]string{SettingKeySubscriptionEnabled: "true"}, want: true},
|
|
{name: "explicit false disables", values: map[string]string{SettingKeySubscriptionEnabled: "false"}, want: false},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
svc := NewSettingService(&settingPublicRepoStub{values: tc.values}, &config.Config{})
|
|
|
|
settings, err := svc.GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.want, settings.SubscriptionEnabled)
|
|
})
|
|
}
|
|
}
|
|
|
|
// The SSR injection payload must mirror the switch so the opt-out flag does not
|
|
// flicker off on first render before the async public-settings fetch resolves.
|
|
func TestSettingService_GetPublicSettingsForInjection_MirrorsSubscriptionEnabled(t *testing.T) {
|
|
for _, value := range []string{"false", "true"} {
|
|
repo := &settingPublicRepoStub{values: map[string]string{SettingKeySubscriptionEnabled: value}}
|
|
svc := NewSettingService(repo, &config.Config{})
|
|
|
|
raw, err := svc.GetPublicSettingsForInjection(context.Background())
|
|
require.NoError(t, err)
|
|
payload, ok := raw.(*PublicSettingsInjectionPayload)
|
|
require.True(t, ok)
|
|
require.Equal(t, value == "true", payload.SubscriptionEnabled, "value=%q", value)
|
|
}
|
|
}
|
|
|
|
// payment_balance_disabled is exposed publicly so the user shell can derive the site
|
|
// billing mode (recharge & subscription / recharge only / subscription only) before any
|
|
// authenticated checkout call. Strict true, mirroring the payment-config parser.
|
|
func TestSettingService_GetPublicSettings_PaymentBalanceDisabledStrictTrue(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
value map[string]string
|
|
want bool
|
|
}{
|
|
{name: "missing key stays enabled", value: map[string]string{}, want: false},
|
|
{name: "explicit false", value: map[string]string{SettingBalancePayDisabled: "false"}, want: false},
|
|
{name: "explicit true disables balance recharge", value: map[string]string{SettingBalancePayDisabled: "true"}, want: true},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
svc := NewSettingService(&settingPublicRepoStub{values: tc.value}, &config.Config{})
|
|
|
|
settings, err := svc.GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.want, settings.PaymentBalanceDisabled)
|
|
|
|
raw, err := svc.GetPublicSettingsForInjection(context.Background())
|
|
require.NoError(t, err)
|
|
payload, ok := raw.(*PublicSettingsInjectionPayload)
|
|
require.True(t, ok)
|
|
require.Equal(t, tc.want, payload.PaymentBalanceDisabled)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSettingService_GetPublicSettings_MCPEnabledFollowsExplicitPublicURL(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
publicURL, frontendURL string
|
|
want bool
|
|
}{
|
|
{"https://api.example.com/api/v1/mcp", "", true},
|
|
// A frontend URL alone never enables MCP.
|
|
{"", "https://platform.example.com", false},
|
|
{" ", "", false},
|
|
} {
|
|
cfg := &config.Config{MCP: config.MCPConfig{PublicURL: tc.publicURL}}
|
|
cfg.Server.FrontendURL = tc.frontendURL
|
|
settings, err := NewSettingService(&settingPublicRepoStub{values: map[string]string{}}, cfg).GetPublicSettings(context.Background())
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.want, settings.MCPEnabled, tc)
|
|
payload, err := NewSettingService(&settingPublicRepoStub{values: map[string]string{}}, cfg).GetPublicSettingsForInjection(context.Background())
|
|
require.NoError(t, err)
|
|
encoded, err := json.Marshal(payload)
|
|
require.NoError(t, err)
|
|
require.Contains(t, string(encoded), fmt.Sprintf(`"mcp_enabled":%t`, tc.want))
|
|
}
|
|
}
|