endpoint_internal_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package push
  2. import (
  3. "context"
  4. "net"
  5. "net/http"
  6. "testing"
  7. "time"
  8. )
  9. func TestBlockedIP(t *testing.T) {
  10. blocked := []string{
  11. "127.0.0.1", "10.1.2.3", "172.16.0.1", "192.168.1.1", "169.254.169.254",
  12. "100.64.0.1", "198.18.0.1", "192.0.2.1", "198.51.100.1", "203.0.113.1",
  13. "240.0.0.1", "0.0.0.0", "::1", "fc00::1", "fe80::1", "2001:db8::1",
  14. "64:ff9b::7f00:1",
  15. }
  16. for _, raw := range blocked {
  17. ip := net.ParseIP(raw)
  18. if ip == nil {
  19. t.Fatalf("IP invalida en el test: %s", raw)
  20. }
  21. if !blockedIP(ip) {
  22. t.Errorf("blockedIP(%s) = false, want true", raw)
  23. }
  24. }
  25. for _, raw := range []string{"8.8.8.8", "1.1.1.1", "2001:4860:4860::8888"} {
  26. ip := net.ParseIP(raw)
  27. if blockedIP(ip) {
  28. t.Errorf("blockedIP(%s) = true, want false", raw)
  29. }
  30. }
  31. }
  32. func TestSafeDialContextRejectsPrivateHost(t *testing.T) {
  33. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  34. defer cancel()
  35. if _, err := safeDialContext(ctx, "tcp", "localhost:443"); err == nil {
  36. t.Fatal("safeDialContext acepto localhost")
  37. }
  38. }
  39. func TestSafeClientRejectsPrivateRedirect(t *testing.T) {
  40. client := newSafeHTTPClient()
  41. req, err := http.NewRequest(http.MethodGet, "https://127.0.0.1/x", nil)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. if err := client.CheckRedirect(req, nil); err == nil {
  46. t.Fatal("CheckRedirect acepto una redireccion a loopback")
  47. }
  48. }