warp_response_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package integration
  2. import (
  3. "bytes"
  4. "net/http"
  5. "net/http/httptest"
  6. "path/filepath"
  7. "testing"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database"
  9. )
  10. // A hostile egress proxy (or a MITM on the WARP endpoint) could stream an
  11. // arbitrarily large body; doWarpRequest must cap the read at maxResponseSize so
  12. // the panel cannot be forced into an unbounded allocation.
  13. func TestDoWarpRequestCapsResponseBody(t *testing.T) {
  14. if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
  15. t.Fatalf("InitDB: %v", err)
  16. }
  17. t.Cleanup(func() { _ = database.CloseDB() })
  18. oversize := maxResponseSize + 4096
  19. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  20. w.WriteHeader(http.StatusOK)
  21. _, _ = w.Write(bytes.Repeat([]byte("a"), oversize))
  22. }))
  23. defer srv.Close()
  24. s := &WarpService{}
  25. req, err := http.NewRequest(http.MethodGet, srv.URL, nil)
  26. if err != nil {
  27. t.Fatalf("NewRequest: %v", err)
  28. }
  29. body, err := s.doWarpRequest(req)
  30. if err != nil {
  31. t.Fatalf("doWarpRequest: %v", err)
  32. }
  33. if len(body) != maxResponseSize {
  34. t.Fatalf("response body not capped: got %d bytes, want %d", len(body), maxResponseSize)
  35. }
  36. }