Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
This commit is contained in:
1 parent
9dd425f765
commit
d5d21b7841
302 files changed
+20037
-3492
No files matched your search
@@ -2,18 +2,14 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdh"
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
webpush "github.com/SherClockHolmes/webpush-go"
|
||||
"pageswiki/internal/ordoschnellic"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
@@ -22,32 +18,10 @@ type browserPushInput struct {
|
||||
Focused bool `json:"focused"`
|
||||
Seen []string `json:"seen"`
|
||||
}
|
||||
type storedBrowserPush struct {
|
||||
webpush.Subscription
|
||||
Origin string `json:"origin"`
|
||||
}
|
||||
type storedBrowserPush = ordoschnellic.StoredBrowserPush
|
||||
|
||||
// Only browser push vendors are destinations, never arbitrary user-provided URLs.
|
||||
// DNS pinning/public-address checks at dial time provide a second SSRF boundary.
|
||||
func validPushSubscription(sub webpush.Subscription) bool {
|
||||
u, e := url.Parse(sub.Endpoint)
|
||||
if e != nil || u.Scheme != "https" || u.User != nil || u.Fragment != "" || u.Port() != "" || len(sub.Endpoint) > 4096 {
|
||||
return false
|
||||
}
|
||||
host := u.Hostname()
|
||||
if host != "fcm.googleapis.com" && host != "updates.push.services.mozilla.com" && host != "web.push.apple.com" && !strings.HasSuffix(host, ".push.services.mozilla.com") {
|
||||
return false
|
||||
}
|
||||
auth, e := base64.RawURLEncoding.DecodeString(sub.Keys.Auth)
|
||||
if e != nil || len(auth) != 16 {
|
||||
return false
|
||||
}
|
||||
key, e := base64.RawURLEncoding.DecodeString(sub.Keys.P256dh)
|
||||
if e != nil {
|
||||
return false
|
||||
}
|
||||
_, e = ecdh.P256().NewPublicKey(key)
|
||||
return e == nil
|
||||
return ordoschnellic.ValidPushSubscription(sub)
|
||||
}
|
||||
|
||||
func (s *Server) notificationPushKeys(ctx context.Context) (private, public string, err error) {
|
||||
@@ -93,7 +67,7 @@ func (s *Server) handleNotificationPush(w http.ResponseWriter, r *http.Request,
|
||||
if r.Method == "DELETE" {
|
||||
err = s.store.DeleteNotificationPush(ctx, domain, owner, input.Subscription.Endpoint)
|
||||
} else {
|
||||
raw, _ := json.Marshal(storedBrowserPush{input.Subscription, s.socialOrigin(r)})
|
||||
raw, _ := json.Marshal(storedBrowserPush{Subscription: input.Subscription, Origin: s.socialOrigin(r)})
|
||||
err = s.store.SaveNotificationPush(ctx, domain, owner, s.authTokenFromRequest(r), input.Subscription.Endpoint, string(raw), input.Focused)
|
||||
if err == nil && input.Focused {
|
||||
err = s.store.AcknowledgeNotificationPush(ctx, domain, owner, input.Subscription.Endpoint, input.Seen)
|
||||
@@ -110,79 +84,16 @@ func (s *Server) handleNotificationPush(w http.ResponseWriter, r *http.Request,
|
||||
http.Error(w, "method not allowed", 405)
|
||||
}
|
||||
|
||||
// Compatibility entry points for existing callers; event registration passes
|
||||
// its worker context through deliverNotificationPushContext.
|
||||
func (s *Server) deliverNotificationPush(job *store.EventJob) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
|
||||
defer cancel()
|
||||
// Bounded retention even for abandoned devices. Inbox is not affected.
|
||||
_, err := s.store.DB.ExecContext(ctx, `DELETE FROM notification_push_subscriptions WHERE id IN (SELECT id FROM notification_push_subscriptions WHERE expires_at_ms<? LIMIT 100)`, time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.store.DB.ExecContext(ctx, `DELETE FROM notification_push_queue WHERE rowid IN (SELECT rowid FROM notification_push_queue WHERE created_at_ms<? LIMIT 500)`, time.Now().Add(-24*time.Hour).UnixMilli())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
client := calendarSourceClient()
|
||||
client.Timeout = 5 * time.Second
|
||||
client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
|
||||
sent, failed := 0, 0
|
||||
for i := 0; i < 20 && ctx.Err() == nil; i++ {
|
||||
d, err := s.store.ClaimNotificationPush(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d == nil {
|
||||
break
|
||||
}
|
||||
if d.Focused {
|
||||
if err = s.store.DeferFocusedNotificationPush(ctx, d); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
retry, expired := false, false
|
||||
if d.Subscription != "" && !d.Focused {
|
||||
var sub storedBrowserPush
|
||||
if json.Unmarshal([]byte(d.Subscription), &sub) != nil || !validPushSubscription(sub.Subscription) {
|
||||
expired = true
|
||||
} else {
|
||||
var success bool
|
||||
success, retry, expired = s.sendNotificationPush(ctx, d, sub, client)
|
||||
if success {
|
||||
sent++
|
||||
} else {
|
||||
failed++
|
||||
}
|
||||
}
|
||||
}
|
||||
// Finish using an independent short context if outbound delivery exhausted its budget.
|
||||
finish, stop := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
err = s.store.FinishNotificationPush(finish, d, retry, expired)
|
||||
stop()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if sent+failed > 0 {
|
||||
_ = s.store.AddEventLog(job.UUID, job.EventUUID, "info", fmt.Sprintf("push sent=%d failed=%d (no payload or endpoint logged)", sent, failed))
|
||||
}
|
||||
return nil
|
||||
return s.deliverNotificationPushContext(context.Background(), job)
|
||||
}
|
||||
|
||||
func (s *Server) sendNotificationPush(ctx context.Context, d *store.NotificationPushDelivery, sub storedBrowserPush, client webpush.HTTPClient) (success, retry, expired bool) {
|
||||
if !validPushSubscription(sub.Subscription) {
|
||||
return false, false, true
|
||||
}
|
||||
private, public, e := s.notificationPushKeys(ctx)
|
||||
if e != nil {
|
||||
return false, true, false
|
||||
}
|
||||
// No task titles, body text or credentials leave the server in a push payload.
|
||||
payload, _ := json.Marshal(map[string]string{"uuid": d.Notification, "owner": d.Owner, "domain": d.Domain})
|
||||
resp, e := webpush.SendNotificationWithContext(ctx, payload, &sub.Subscription, &webpush.Options{HTTPClient: client, Subscriber: sub.Origin, VAPIDPrivateKey: private, VAPIDPublicKey: public, TTL: 300, Urgency: webpush.UrgencyNormal, Topic: "pageswiki-inbox"})
|
||||
if e != nil {
|
||||
return false, true, false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return resp.StatusCode >= 200 && resp.StatusCode < 300, resp.StatusCode == 429 || resp.StatusCode >= 500, resp.StatusCode == 404 || resp.StatusCode == 410
|
||||
copy := *d
|
||||
raw, _ := json.Marshal(sub)
|
||||
copy.Subscription = string(raw)
|
||||
outcome := (ordoschnellic.WebPush{Keys: s.notificationPushKeys, Client: client}).Send(ctx, ©)
|
||||
return outcome.Success, outcome.Retry, outcome.Expired
|
||||
}
|
||||
Reference in new issue
Block a user