subject_1974_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // subject_1974_test.go - contrato del sujeto VAPID: Apple rechaza con 403
  2. // BadJwtToken un sub mailto:localhost (#1974). El Notifier debe enviar un
  3. // https: valido o un mailto: con dominio real.
  4. package push_test
  5. import (
  6. "encoding/base64"
  7. "encoding/json"
  8. "io"
  9. "net/http"
  10. "net/http/httptest"
  11. "strings"
  12. "testing"
  13. "time"
  14. "github.com/gnacho/netpulse/server-go/internal/push"
  15. )
  16. func TestSubjectFor(t *testing.T) {
  17. const fallback = "https://github.com/gnacho/netpulse"
  18. cases := []struct {
  19. name string
  20. conf string
  21. public string
  22. expected string
  23. }{
  24. {"configured wins", "mailto:ops@example.com", "https://net.example.com", "mailto:ops@example.com"},
  25. {"public https", "", "https://net.example.com", "https://net.example.com"},
  26. {"public https with trailing space", "", " https://net.example.com ", "https://net.example.com"},
  27. {"public http is not valid", "", "http://192.168.1.50:8080", fallback},
  28. {"localhost email is not valid", "mailto:netpulse@localhost", "", fallback},
  29. {"email without domain dot is not valid", "mailto:ops@intranet", "", fallback},
  30. {"empty email address", "mailto:@example.com", "", fallback},
  31. {"nothing configured", "", "", fallback},
  32. {"fallback is never localhost", "", "", fallback},
  33. }
  34. for _, c := range cases {
  35. t.Run(c.name, func(t *testing.T) {
  36. got := push.SubjectFor(c.conf, c.public)
  37. if got != c.expected {
  38. t.Fatalf("SubjectFor(%q, %q) = %q, esperaba %q", c.conf, c.public, got, c.expected)
  39. }
  40. if strings.Contains(strings.ToLower(got), "localhost") {
  41. t.Fatalf("subject con localhost: %q", got)
  42. }
  43. })
  44. }
  45. }
  46. // El JWT enviado al push service lleva el sub elegido, no el constante viejo.
  47. func TestNotifierSendsConfiguredSubject(t *testing.T) {
  48. d, _ := openDB(t)
  49. ua := newBrowserSub(t)
  50. got := make(chan capturedPush, 1)
  51. srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  52. _, _ = io.Copy(io.Discard, r.Body)
  53. got <- capturedPush{method: r.Method, header: r.Header.Clone()}
  54. w.WriteHeader(http.StatusCreated)
  55. }))
  56. defer srv.Close()
  57. if _, err := push.UpsertSubscription(d, push.Subscription{
  58. Endpoint: "https://push.example/send", Auth: ua.authB, P256dh: ua.pubB,
  59. }); err != nil {
  60. t.Fatalf("upsert: %v", err)
  61. }
  62. n := newNotifier(t, d, testPushClient(srv))
  63. n.SetSubject("mailto:ops@example.com")
  64. n.Notify(testEvent())
  65. var req capturedPush
  66. select {
  67. case req = <-got:
  68. case <-time.After(15 * time.Second):
  69. t.Fatal("el push service simulado no recibio nada en 15 s")
  70. }
  71. sub := jwtSubject(t, req.header.Get("Authorization"))
  72. if sub != "mailto:ops@example.com" {
  73. t.Fatalf("sub del JWT = %q, esperaba mailto:ops@example.com", sub)
  74. }
  75. }
  76. // jwtSubject extrae el claim sub del JWT de la cabecera VAPID (vapid t=<jwt>, k=...).
  77. func jwtSubject(t *testing.T, authz string) string {
  78. t.Helper()
  79. const marker = "t="
  80. i := strings.Index(authz, marker)
  81. if i < 0 {
  82. t.Fatalf("Authorization sin token: %q", authz)
  83. }
  84. tok := authz[i+len(marker):]
  85. if j := strings.IndexByte(tok, ','); j >= 0 {
  86. tok = tok[:j]
  87. }
  88. parts := strings.Split(tok, ".")
  89. if len(parts) != 3 {
  90. t.Fatalf("JWT con %d partes: %q", len(parts), tok)
  91. }
  92. raw, err := base64.RawURLEncoding.DecodeString(parts[1])
  93. if err != nil {
  94. t.Fatalf("payload base64: %v", err)
  95. }
  96. var claims struct {
  97. Sub string `json:"sub"`
  98. }
  99. if err := json.Unmarshal(raw, &claims); err != nil {
  100. t.Fatalf("claims JSON: %v", err)
  101. }
  102. return claims.Sub
  103. }