Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
188 lines
7.1 KiB
Go
188 lines
7.1 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ecdh"
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
webpush "github.com/SherClockHolmes/webpush-go"
|
|
"github.com/google/uuid"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"pageswiki/internal/store"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type pushHTTPFixture func(*http.Request) (*http.Response, error)
|
|
|
|
func (f pushHTTPFixture) Do(r *http.Request) (*http.Response, error) { return f(r) }
|
|
|
|
func TestNotificationPushEncryptedDeliveryResponses(t *testing.T) {
|
|
s, _, domain, owner := taskFixture(t)
|
|
sub := storedBrowserPush{testPushSubscription(t), "https://wiki.example"}
|
|
d := &store.NotificationPushDelivery{Notification: uuid.NewString(), Domain: domain, Owner: owner}
|
|
for _, code := range []int{201, 400, 404, 410, 429, 500} {
|
|
client := pushHTTPFixture(func(r *http.Request) (*http.Response, error) {
|
|
if r.Method != "POST" || r.URL.String() != sub.Endpoint || r.Header.Get("TTL") != "300" || r.Header.Get("Content-Encoding") != "aes128gcm" || !strings.HasPrefix(r.Header.Get("Authorization"), "vapid ") {
|
|
t.Fatal("invalid Web Push request")
|
|
}
|
|
body, e := io.ReadAll(r.Body)
|
|
if e != nil || strings.Contains(string(body), d.Notification) || strings.Contains(string(body), owner) {
|
|
t.Fatal("plaintext notification payload", e)
|
|
}
|
|
return &http.Response{StatusCode: code, Body: io.NopCloser(strings.NewReader(""))}, nil
|
|
})
|
|
success, retry, expired := s.sendNotificationPush(context.Background(), d, sub, client)
|
|
if success != (code == 201) || retry != (code == 429 || code == 500) || expired != (code == 404 || code == 410) {
|
|
t.Fatalf("delivery status %d: %v %v %v", code, success, retry, expired)
|
|
}
|
|
}
|
|
}
|
|
|
|
func testPushSubscription(t *testing.T) webpush.Subscription {
|
|
t.Helper()
|
|
key, err := ecdh.P256().GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return webpush.Subscription{Endpoint: "https://fcm.googleapis.com/fcm/send/test-fixture", Keys: webpush.Keys{Auth: base64.RawURLEncoding.EncodeToString(make([]byte, 16)), P256dh: base64.RawURLEncoding.EncodeToString(key.PublicKey().Bytes())}}
|
|
}
|
|
func TestNotificationPushEndpointsAndPrivacy(t *testing.T) {
|
|
s, token, domain, owner := taskFixture(t)
|
|
s.mux.HandleFunc("/_/api/v1/notifications/", s.handleNotificationsAPI)
|
|
call := func(method, token string, input any) *httptest.ResponseRecorder {
|
|
b, _ := json.Marshal(input)
|
|
r := httptest.NewRequest(method, "http://localhost/_/api/v1/notifications/push", strings.NewReader(string(b)))
|
|
r.Header.Set("Content-Type", "application/json")
|
|
if token != "" {
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
if w := call("GET", "", nil); w.Code != 401 {
|
|
t.Fatalf("anonymous %d", w.Code)
|
|
}
|
|
w := call("GET", token, nil)
|
|
if w.Code != 200 || strings.Contains(w.Body.String(), "private") || !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
|
|
t.Fatalf("keys: %d %s", w.Code, w.Body)
|
|
}
|
|
sub := testPushSubscription(t)
|
|
if w = call("POST", token, browserPushInput{Subscription: sub, Focused: true}); w.Code != 200 {
|
|
t.Fatalf("subscribe %d %s", w.Code, w.Body)
|
|
}
|
|
var raw string
|
|
if err := s.store.DB.QueryRow(`SELECT session_hash FROM notification_push_subscriptions WHERE owner_uuid=? AND domain_uuid=?`, owner, domain).Scan(&raw); err != nil || raw == token || raw == "" {
|
|
t.Fatalf("token storage %v", err)
|
|
}
|
|
if w = call("DELETE", token, browserPushInput{Subscription: sub}); w.Code != 200 {
|
|
t.Fatalf("unsubscribe %d", w.Code)
|
|
}
|
|
for _, endpoint := range []string{"http://fcm.googleapis.com/test", "https://localhost/test", "https://127.0.0.1/test", "https://fcm.googleapis.com.evil.test/test", "https://fcm.googleapis.com:444/test", "https://user@fcm.googleapis.com/test", "https://fcm.googleapis.com/test#fragment"} {
|
|
copy := sub
|
|
copy.Endpoint = endpoint
|
|
if validPushSubscription(copy) {
|
|
t.Errorf("unsafe endpoint accepted: %s", endpoint)
|
|
}
|
|
}
|
|
sub.Keys.P256dh = "bad"
|
|
if validPushSubscription(sub) {
|
|
t.Fatal("bad curve accepted")
|
|
}
|
|
}
|
|
|
|
func TestNotificationPushQueueAtomicityRevocationAndFocus(t *testing.T) {
|
|
s, token, domain, owner := taskFixture(t)
|
|
ctx := context.Background()
|
|
sub := testPushSubscription(t)
|
|
raw, _ := json.Marshal(storedBrowserPush{sub, "https://wiki.example"})
|
|
if err := s.store.SaveNotificationPush(ctx, domain, owner, token, sub.Endpoint, string(raw), true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
makeNotification := func(rollback bool) store.Notification {
|
|
t.Helper()
|
|
tx, e := s.store.DB.Begin()
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
n, _, e := s.store.CreateNotificationTx(tx, store.Notification{DomainUUID: domain, RecipientUUID: owner, EventUUID: uuid.NewString(), Kind: "task.due", Message: "private fixture"})
|
|
if e != nil {
|
|
tx.Rollback()
|
|
t.Fatal(e)
|
|
}
|
|
if rollback {
|
|
tx.Rollback()
|
|
} else if e = tx.Commit(); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return n
|
|
}
|
|
makeNotification(true)
|
|
if d, e := s.store.ClaimNotificationPush(ctx); e != nil || d != nil {
|
|
t.Fatalf("rollback queued: %+v %v", d, e)
|
|
}
|
|
n := makeNotification(false)
|
|
d, e := s.store.ClaimNotificationPush(ctx)
|
|
if e != nil || d == nil || !d.Focused || d.Notification != n.UUID {
|
|
t.Fatalf("claim: %+v %v", d, e)
|
|
}
|
|
if second, e := s.store.ClaimNotificationPush(ctx); e != nil || second != nil {
|
|
t.Fatal("lease failed", e)
|
|
}
|
|
if e = s.store.DeferFocusedNotificationPush(ctx, d); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.store.AcknowledgeNotificationPush(ctx, domain, "wrong owner", sub.Endpoint, []string{n.UUID}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
var count int
|
|
s.store.DB.QueryRow(`SELECT COUNT(*) FROM notification_push_queue`).Scan(&count)
|
|
if count != 1 {
|
|
t.Fatal("cross-owner acknowledgement")
|
|
}
|
|
if e = s.store.AcknowledgeNotificationPush(ctx, domain, owner, sub.Endpoint, []string{n.UUID}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.store.SaveNotificationPush(ctx, domain, owner, token, sub.Endpoint, string(raw), false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
makeNotification(false)
|
|
d, e = s.store.ClaimNotificationPush(ctx)
|
|
if e != nil || d == nil || d.Focused || d.Subscription == "" {
|
|
t.Fatalf("background claim: %+v %v", d, e)
|
|
}
|
|
if e = s.store.FinishNotificationPush(ctx, d, true, false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if retry, e := s.store.ClaimNotificationPush(ctx); e != nil || retry != nil {
|
|
t.Fatal("retry not backed off", e)
|
|
}
|
|
if _, e = s.store.DB.Exec(`UPDATE notification_push_queue SET next_at_ms=0`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.store.RevokeAuthSession(token); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
d, e = s.store.ClaimNotificationPush(ctx)
|
|
if e != nil || d == nil || d.Subscription != "" {
|
|
t.Fatalf("revoked session delivery: %+v %v", d, e)
|
|
}
|
|
if e = s.store.FinishNotificationPush(ctx, d, false, false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.store.EnsureNotificationPushEvent(); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
s.store.DB.Exec(`UPDATE event_definitions SET enabled=0,interval_seconds=120 WHERE uuid='builtin.notification.push'`)
|
|
s.store.EnsureNotificationPushEvent()
|
|
var enabled, interval int
|
|
s.store.DB.QueryRow(`SELECT enabled,interval_seconds FROM event_definitions WHERE uuid='builtin.notification.push'`).Scan(&enabled, &interval)
|
|
if enabled != 0 || interval != 120 {
|
|
t.Fatal("operator scheduler settings overwritten")
|
|
}
|
|
}
|