package ordoschnellic import ( "context" "crypto/ecdh" "crypto/rand" "encoding/base64" "encoding/json" "errors" "io" "net/http" "strings" "testing" webpush "github.com/SherClockHolmes/webpush-go" ) type httpFixture func(*http.Request) (*http.Response, error) func (f httpFixture) Do(r *http.Request) (*http.Response, error) { return f(r) } type trackedBody struct { io.Reader closed bool } func (b *trackedBody) Close() error { b.closed = true; return nil } func subscriptionFixture(t *testing.T) StoredBrowserPush { t.Helper() key, err := ecdh.P256().GenerateKey(rand.Reader) if err != nil { t.Fatal(err) } return StoredBrowserPush{Subscription: webpush.Subscription{Endpoint: "https://fcm.googleapis.com/fcm/send/synthetic", Keys: webpush.Keys{Auth: base64.RawURLEncoding.EncodeToString(make([]byte, 16)), P256dh: base64.RawURLEncoding.EncodeToString(key.PublicKey().Bytes())}}, Origin: "https://wiki.example"} } func deliveryFixture(t *testing.T, sub StoredBrowserPush) *PushDelivery { t.Helper() b, err := json.Marshal(sub) if err != nil { t.Fatal(err) } return &PushDelivery{Notification: "synthetic-notification", Owner: "synthetic-owner", Domain: "synthetic-domain", Subscription: string(b)} } func TestWebPushPrivacyAndResponseHandling(t *testing.T) { private, public, err := webpush.GenerateVAPIDKeys() if err != nil { t.Fatal(err) } d := deliveryFixture(t, subscriptionFixture(t)) var payload map[string]string if err := json.Unmarshal(wakePayload(d), &payload); err != nil || len(payload) != 3 || payload["uuid"] != d.Notification || payload["owner"] != d.Owner || payload["domain"] != d.Domain { t.Fatal("payload contract", payload, err) } for _, code := range []int{201, 302, 400, 404, 410, 429, 503} { body := &trackedBody{Reader: strings.NewReader("")} w := WebPush{Keys: func(context.Context) (string, string, error) { return private, public, nil }, Client: httpFixture(func(r *http.Request) (*http.Response, error) { data, err := io.ReadAll(r.Body) if err != nil { t.Fatal(err) } if strings.Contains(string(data), d.Notification) || strings.Contains(string(data), d.Owner) || r.Header.Get("Content-Encoding") != "aes128gcm" || r.Header.Get("TTL") != "300" || r.Header.Get("Topic") != "pageswiki-inbox" { t.Fatal("unprotected/changed payload") } return &http.Response{StatusCode: code, Body: body}, nil })} if o := w.Send(context.Background(), d); o != ClassifyPushStatus(code) || !body.closed { t.Fatal(code, o, body.closed) } } } func TestWebPushFailsClosedWithoutNetwork(t *testing.T) { sub := subscriptionFixture(t) d := deliveryFixture(t, sub) for _, endpoint := range []string{"http://fcm.googleapis.com/x", "https://localhost/x", "https://127.0.0.1/x", "https://fcm.googleapis.com.evil.example/x", "https://fcm.googleapis.com:443/x", "https://user@fcm.googleapis.com/x", "https://web.push.apple.com/x#f"} { sub.Endpoint = endpoint w := WebPush{Keys: func(context.Context) (string, string, error) { t.Fatal("unsafe target requested keys") return "", "", nil }, Client: httpFixture(func(*http.Request) (*http.Response, error) { t.Fatal("unsafe target contacted"); return nil, nil })} if o := w.Send(context.Background(), deliveryFixture(t, sub)); !o.Expired || o.Attempted { t.Fatal(endpoint, o) } } for _, w := range []WebPush{{}, {Keys: func(context.Context) (string, string, error) { t.Fatal("no HTTP client must fail closed") return "", "", nil }}, {Client: httpFixture(func(*http.Request) (*http.Response, error) { t.Fatal("no keys must fail closed"); return nil, nil })}} { if o := w.Send(context.Background(), d); !o.Retry || !o.Attempted { t.Fatal(o) } } ctx, cancel := context.WithCancel(context.Background()) cancel() w := WebPush{Keys: func(context.Context) (string, string, error) { t.Fatal("cancelled request loaded keys") return "", "", nil }, Client: httpFixture(func(*http.Request) (*http.Response, error) { t.Fatal("cancelled request sent"); return nil, nil })} if o := w.Send(ctx, d); !o.Retry { t.Fatal(o) } } func TestWebPushTransportFailure(t *testing.T) { private, public, err := webpush.GenerateVAPIDKeys() if err != nil { t.Fatal(err) } d := deliveryFixture(t, subscriptionFixture(t)) for _, mode := range []string{"keys", "network", "nil-response"} { w := WebPush{Keys: func(context.Context) (string, string, error) { if mode == "keys" { return "", "", errors.New("secret must not escape") } return private, public, nil }, Client: httpFixture(func(*http.Request) (*http.Response, error) { if mode == "keys" { t.Fatal("sent after key failure") } if mode == "network" { return nil, errors.New("endpoint must not escape") } return nil, nil })} if o := w.Send(context.Background(), d); o != (Outcome{Retry: true, Attempted: true}) { t.Fatal(mode, o) } } }