Files
pages/internal/ordoschnellic/webpush.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
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.
2026-11-01 15:23:31 +09:00

87 lines
2.7 KiB
Go

package ordoschnellic
import (
"context"
"crypto/ecdh"
"encoding/base64"
"encoding/json"
"net/url"
"strings"
webpush "github.com/SherClockHolmes/webpush-go"
)
type StoredBrowserPush struct {
webpush.Subscription
Origin string `json:"origin"`
}
// 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
}
type KeyProvider func(context.Context) (private, public string, err error)
// Client must enforce public-address DNS pinning, TLS, a bounded timeout and
// no redirects. There is deliberately no default HTTP client fallback.
type WebPush struct {
Keys KeyProvider
Client webpush.HTTPClient
}
// wakePayload intentionally has no access to Inbox titles, bodies or tokens.
func wakePayload(item *PushDelivery) []byte {
payload, _ := json.Marshal(map[string]string{"uuid": item.Notification, "owner": item.Owner, "domain": item.Domain})
return payload
}
func (w WebPush) Send(ctx context.Context, item *PushDelivery) Outcome {
if item == nil {
return Outcome{}
}
var sub StoredBrowserPush
if json.Unmarshal([]byte(item.Subscription), &sub) != nil || !ValidPushSubscription(sub.Subscription) {
return Outcome{Expired: true}
}
if w.Keys == nil || w.Client == nil || ctx.Err() != nil {
return Outcome{Retry: true, Attempted: true}
}
private, public, err := w.Keys(ctx)
if err != nil {
return Outcome{Retry: true, Attempted: true}
}
resp, err := webpush.SendNotificationWithContext(ctx, wakePayload(item), &sub.Subscription, &webpush.Options{HTTPClient: w.Client, Subscriber: sub.Origin, VAPIDPrivateKey: private, VAPIDPublicKey: public, TTL: 300, Urgency: webpush.UrgencyNormal, Topic: "pageswiki-inbox"})
if err != nil {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
return Outcome{Retry: true, Attempted: true}
}
if resp == nil {
return Outcome{Retry: true, Attempted: true}
}
if resp.Body != nil {
defer resp.Body.Close()
}
return ClassifyPushStatus(resp.StatusCode)
}