outbound_subscription_ssrf_test.go 786 B

123456789101112131415161718192021222324252627282930
  1. package service
  2. import (
  3. "context"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/mhsanaei/3x-ui/v3/internal/util/netsafe"
  9. )
  10. func TestSubscriptionFetchClientBlocksPrivateDial(t *testing.T) {
  11. setupSettingTestDB(t)
  12. client := (&OutboundSubscriptionService{}).subscriptionFetchClient(5 * time.Second)
  13. ctx := netsafe.ContextWithAllowPrivate(context.Background(), false)
  14. req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://127.0.0.1:1/", nil)
  15. if err != nil {
  16. t.Fatalf("new request: %v", err)
  17. }
  18. _, err = client.Do(req)
  19. if err == nil {
  20. t.Fatal("the fetch client dialed a private address instead of blocking it")
  21. }
  22. if !strings.Contains(err.Error(), "blocked private") {
  23. t.Fatalf("expected an SSRF-guard block, got a plain dial error: %v", err)
  24. }
  25. }