account_protection_regression_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "net/http"
  6. "net/http/httptest"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/Wei-Shaw/sub2api/internal/config"
  11. coderws "github.com/coder/websocket"
  12. "github.com/gin-gonic/gin"
  13. "github.com/stretchr/testify/require"
  14. "github.com/tidwall/gjson"
  15. )
  16. // Regression coverage for policy transitions, metadata and real local WSS.
  17. func protectionRegressionServices(a *Account) (*adminServiceImpl, *AntiDegradeService) {
  18. repo := &upstreamBillingProbeAccountRepo{accounts: map[int64]*Account{a.ID: a}}
  19. admin := &adminServiceImpl{accountRepo: repo}
  20. return admin, NewAntiDegradeService(admin)
  21. }
  22. type protectionRegressionFailSecondWrite struct {
  23. AntiDegradeStore
  24. writes int
  25. failAt int
  26. }
  27. func (s *protectionRegressionFailSecondWrite) UpdateAccount(ctx context.Context, id int64, in *UpdateAccountInput) (*Account, error) {
  28. s.writes++
  29. if s.writes == s.failAt {
  30. return nil, errors.New("injected write failure")
  31. }
  32. return s.AntiDegradeStore.UpdateAccount(ctx, id, in)
  33. }
  34. func TestProtectionRegression(t *testing.T) {
  35. ctx := context.Background()
  36. t.Run("standard_strategy_revert_restores_tls_enabled", func(t *testing.T) {
  37. a := &Account{ID: 1, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Concurrency: 32,
  38. Extra: map[string]any{"enable_tls_fingerprint": true, "tls_fingerprint_profile_id": 9}}
  39. _, svc := protectionRegressionServices(a)
  40. applied, err := svc.ApplyAntiDegradeMode(ctx, 1, AntiDegradeModeMinimal)
  41. require.NoError(t, err)
  42. require.False(t, applied.IsTLSFingerprintEnabled())
  43. restored, err := svc.Revert(ctx, 1)
  44. require.NoError(t, err)
  45. require.True(t, restored.IsTLSFingerprintEnabled(), "restoring a true TLS snapshot must re-enable TLS")
  46. })
  47. t.Run("legacy_revert_preserves_custom_tls_profile", func(t *testing.T) {
  48. a := &Account{ID: 2, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Concurrency: 32,
  49. Extra: map[string]any{"enable_tls_fingerprint": true, "tls_fingerprint_profile_id": 9}}
  50. _, svc := protectionRegressionServices(a)
  51. _, err := svc.ApplyAntiDegradeMode(ctx, 2, AntiDegradeModeLegacy)
  52. require.NoError(t, err)
  53. restored, err := svc.Revert(ctx, 2)
  54. require.NoError(t, err)
  55. require.EqualValues(t, 9, restored.GetTLSFingerprintProfileID(), "revert must not delete a pre-existing custom profile")
  56. })
  57. t.Run("switch_failure_keeps_original_policy", func(t *testing.T) {
  58. a := &Account{ID: 3, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Concurrency: 32}
  59. PrepareNewAccountProtection(a)
  60. admin, _ := protectionRegressionServices(a)
  61. failing := &protectionRegressionFailSecondWrite{AntiDegradeStore: admin, failAt: 1}
  62. _, err := NewAntiDegradeService(failing).ApplyAntiDegradeMode(ctx, 3, AntiDegradeModeMinimal)
  63. require.ErrorContains(t, err, "write failure")
  64. current, err := admin.GetAccount(ctx, 3)
  65. require.NoError(t, err)
  66. require.True(t, current.AntiDegradationEnabled(), "failed switch already committed protection=false")
  67. successful := &protectionRegressionFailSecondWrite{AntiDegradeStore: admin}
  68. switched, err := NewAntiDegradeService(successful).ApplyAntiDegradeMode(ctx, 3, AntiDegradeModeMinimal)
  69. require.NoError(t, err)
  70. require.Equal(t, 1, successful.writes, "switch must publish only the final state")
  71. require.Equal(t, "minimal_compat", switched.ProtectionMode())
  72. })
  73. t.Run("mode2_ordinary_edit_cannot_disable_fingerprint", func(t *testing.T) {
  74. a := &Account{ID: 4, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Concurrency: 32}
  75. admin, svc := protectionRegressionServices(a)
  76. _, err := svc.ApplyAntiDegradeMode(ctx, 4, AntiDegradeMode2)
  77. require.NoError(t, err)
  78. updated, err := admin.UpdateAccount(ctx, 4, &UpdateAccountInput{Extra: map[string]any{"codex_fingerprint_mode": "off", "enable_tls_fingerprint": false}})
  79. require.NoError(t, err)
  80. require.True(t, updated.AntiDegradationEnabled())
  81. require.Equal(t, codexFingerprintFull, updated.GetCodexFingerprintMode(), "badge stays enabled while ordinary edit removes full identity policy")
  82. })
  83. t.Run("generic_can_upgrade_after_selecting_fixed_proxy", func(t *testing.T) {
  84. a := &Account{ID: 5, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Concurrency: 32,
  85. Extra: map[string]any{"proxy_mode": "random"}}
  86. PrepareNewAccountProtection(a)
  87. require.Equal(t, "generic_v1", a.ProtectionScope())
  88. admin, svc := protectionRegressionServices(a)
  89. _, err := admin.UpdateAccount(ctx, 5, &UpdateAccountInput{Extra: map[string]any{"proxy_mode": "fixed"}})
  90. require.NoError(t, err)
  91. updated, err := svc.ApplyAntiDegradeMode(ctx, 5, AntiDegradeMode1)
  92. require.NoError(t, err, "generic is normalized to mode1, so an eligible upgrade is rejected as invalid mode1")
  93. require.True(t, updated.IsMode1ProtectionEnabled())
  94. })
  95. t.Run("openai_only_profile_rejects_gemini", func(t *testing.T) {
  96. a := &Account{ID: 6, Platform: PlatformGemini, Type: AccountTypeAPIKey, Concurrency: 32}
  97. _, svc := protectionRegressionServices(a)
  98. _, err := svc.ApplyAntiDegradeMode(ctx, 6, AntiDegradeModeMinimal)
  99. require.Error(t, err, "registry requires OpenAI OAuth, but the service accepts Gemini and labels it protected")
  100. })
  101. t.Run("unknown_strategy_rejected", func(t *testing.T) {
  102. a := &Account{ID: 7, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Concurrency: 32}
  103. _, svc := protectionRegressionServices(a)
  104. _, err := svc.ApplyAntiDegradeMode(ctx, 7, AntiDegradeMode("mode_typo"))
  105. require.Error(t, err, "unknown mode silently applies mode1")
  106. })
  107. t.Run("preview_reports_global_tls_override", func(t *testing.T) {
  108. a := &Account{ID: 8, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Concurrency: 32}
  109. _, svc := protectionRegressionServices(a)
  110. a, err := svc.ApplyAntiDegradeMode(ctx, 8, AntiDegradeModeLegacy)
  111. require.NoError(t, err)
  112. svc.cfg = &config.Config{}
  113. preview, err := svc.PreviewMode(ctx, 8, AntiDegradeModeLegacy)
  114. require.NoError(t, err)
  115. upstream := &mode1HTTPRecorder{}
  116. gw := &OpenAIGatewayService{cfg: &config.Config{}, httpUpstream: upstream}
  117. resp, err := gw.doOpenAIUpstream(httptest.NewRequest(http.MethodPost, "https://local.invalid/responses", nil), "", a)
  118. require.NoError(t, err)
  119. resp.Body.Close()
  120. require.Equal(t, 1, upstream.normal)
  121. require.Zero(t, upstream.tls)
  122. require.Equal(t, "nodejs24", preview.Runtime.ConfiguredTLS)
  123. require.Equal(t, "standard", preview.Runtime.EffectiveTLS)
  124. require.NotEmpty(t, preview.Runtime.TLSReason)
  125. require.False(t, preview.Runtime.Observed)
  126. })
  127. t.Run("mode1_roundtrip_preserves_original_configuration_control", func(t *testing.T) {
  128. a := &Account{ID: 9, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Concurrency: 32,
  129. Extra: map[string]any{"enable_tls_fingerprint": true, "tls_fingerprint_profile_id": 9}}
  130. _, svc := protectionRegressionServices(a)
  131. _, err := svc.ApplyAntiDegradeMode(ctx, 9, AntiDegradeMode1)
  132. require.NoError(t, err)
  133. restored, err := svc.Revert(ctx, 9)
  134. require.NoError(t, err)
  135. require.True(t, restored.IsTLSFingerprintEnabled())
  136. require.EqualValues(t, 9, restored.GetTLSFingerprintProfileID())
  137. require.Equal(t, 32, restored.Concurrency)
  138. })
  139. }
  140. func TestProtectionRegressionNativeWSSIdentity(t *testing.T) {
  141. gin.SetMode(gin.TestMode)
  142. for _, mode := range []AntiDegradeMode{AntiDegradeMode1, AntiDegradeMode2, AntiDegradeModeLegacy} {
  143. t.Run(string(mode), func(t *testing.T) {
  144. capturedHeaders := make(chan http.Header, 1)
  145. upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  146. conn, err := coderws.Accept(w, r, nil)
  147. if err != nil {
  148. return
  149. }
  150. defer conn.CloseNow()
  151. capturedHeaders <- r.Header.Clone()
  152. for {
  153. kind, payload, err := conn.Read(r.Context())
  154. if err != nil {
  155. return
  156. }
  157. if conn.Write(r.Context(), kind, payload) != nil {
  158. return
  159. }
  160. terminal := []byte(`{"type":"response.completed","response":{"id":"resp_local","status":"completed","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"OK"}]}]}}`)
  161. if conn.Write(r.Context(), coderws.MessageText, terminal) != nil {
  162. return
  163. }
  164. }
  165. }))
  166. defer upstream.Close()
  167. dialer := &mode1V3StandardTestDialer{target: "wss" + strings.TrimPrefix(upstream.URL, "https"), client: upstream.Client(), protected: make(chan bool, 4)}
  168. cfg := passthroughLifecycleConfig()
  169. cfg.Gateway.TLSFingerprint.Enabled = false
  170. cfg.Gateway.OpenAIWS.OAuthEnabled = true
  171. a := &Account{ID: 72, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Concurrency: 4,
  172. Credentials: map[string]any{"chatgpt_account_id": "00000000-0000-4000-8000-000000000072"}}
  173. _, adminSvc := protectionRegressionServices(a)
  174. a, err := adminSvc.ApplyAntiDegradeMode(context.Background(), 72, mode)
  175. require.NoError(t, err)
  176. a.Extra["openai_oauth_responses_websockets_v2_mode"] = OpenAIWSIngressModePassthrough
  177. svc := newPassthroughLifecycleService(cfg, newStagedPassthroughConn())
  178. svc.openaiWSPassthroughDialer = dialer
  179. ctx, cancel := context.WithTimeout(context.Background(), 6*time.Second)
  180. defer cancel()
  181. server, serverErr := startPassthroughLifecycleServer(t, ctx, svc, a)
  182. defer server.Close()
  183. client := dialPassthroughLifecycleClientWithPayload(t, server, `{"type":"response.create","model":"gpt-5.2","input":"Return OK","client_metadata":{"installation_id":"client-device","session_id":"client-session","thread_id":"client-thread"}}`)
  184. defer client.CloseNow()
  185. _, output, err := client.Read(ctx)
  186. require.NoError(t, err)
  187. _, _, err = client.Read(ctx)
  188. require.NoError(t, err)
  189. require.NoError(t, client.Write(ctx, coderws.MessageText, []byte(`{"type":"response.create","model":"gpt-5.2","input":"Second turn","client_metadata":{"installation_id":"client-device","session_id":"client-session","thread_id":"client-thread"}}`)))
  190. _, second, err := client.Read(ctx)
  191. require.NoError(t, err)
  192. _, _, err = client.Read(ctx)
  193. require.NoError(t, err)
  194. require.NoError(t, client.Close(coderws.StatusNormalClosure, "done"))
  195. select {
  196. case <-serverErr:
  197. case <-ctx.Done():
  198. t.Fatal("local relay did not close")
  199. }
  200. headers := <-capturedHeaders
  201. expected := resolveCodexFingerprintIDs(a, "client-session", a.GetCodexFingerprintMode())
  202. require.NotNil(t, expected)
  203. t.Logf("strategy=%s header_installation=%q body_installation=%q expected=%q", mode, headers.Get("x-codex-installation-id"), gjson.GetBytes(output, "client_metadata.installation_id").String(), expected.installationID)
  204. require.Equal(t, expected.installationID, gjson.GetBytes(output, "client_metadata.installation_id").String(), "native WSS body must carry the same policy identity as HTTP")
  205. require.Equal(t, expected.installationID, headers.Get("x-codex-installation-id"))
  206. require.Equal(t, expected.installationID, gjson.GetBytes(second, "client_metadata.installation_id").String())
  207. require.Equal(t, gjson.GetBytes(output, "client_metadata.thread_id").String(), gjson.GetBytes(second, "client_metadata.thread_id").String())
  208. })
  209. }
  210. }
  211. func TestProtectionDeviceStrategiesKeepPoolConversationsSeparate(t *testing.T) {
  212. for _, mode := range []AntiDegradeMode{AntiDegradeModeMinimal, AntiDegradeModeTLSNode24} {
  213. t.Run(string(mode), func(t *testing.T) {
  214. _, svc := protectionRegressionServices(&Account{ID: 91, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Concurrency: 4})
  215. a, err := svc.ApplyAntiDegradeMode(context.Background(), 91, mode)
  216. require.NoError(t, err)
  217. first := http.Header{"X-Codex-Installation-Id": {"same-device"}, "Session-Id": {"session-a"}, "Thread-Id": {"thread-a"}}
  218. second := first.Clone()
  219. second.Set("session-id", "session-b")
  220. second.Set("thread-id", "thread-b")
  221. require.NotEqual(t, normalizeOpenAIWSHandshakeCompatibility(a, first), normalizeOpenAIWSHandshakeCompatibility(a, second))
  222. })
  223. }
  224. }