protection_review_test.go.txt 12 KB

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