Archived
initial
This commit is contained in:
59 files changed
+11046
No files matched your search
@@ -0,0 +1,139 @@
|
||||
package webhook
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/meovv-mail/meovv-mail/internal/app"
|
||||
"github.com/meovv-mail/meovv-mail/internal/store"
|
||||
)
|
||||
|
||||
type Dispatcher struct {
|
||||
Store *store.Store
|
||||
Cipher app.TokenCipher
|
||||
HTTP *http.Client
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
func New(s *store.Store, cipher app.TokenCipher) *Dispatcher {
|
||||
return &Dispatcher{Store: s, Cipher: cipher, HTTP: &http.Client{Timeout: 10 * time.Second}, Now: time.Now}
|
||||
}
|
||||
|
||||
func Signature(secret string, timestamp int64, body []byte) string {
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
fmt.Fprintf(mac, "%d.", timestamp)
|
||||
mac.Write(body)
|
||||
return hex.EncodeToString(mac.Sum(nil))
|
||||
}
|
||||
func Verify(secret, signature, timestamp string, body []byte, maxSkew time.Duration) bool {
|
||||
unix, err := strconv.ParseInt(timestamp, 10, 64)
|
||||
if err != nil || time.Since(time.Unix(unix, 0)) > maxSkew || time.Until(time.Unix(unix, 0)) > maxSkew {
|
||||
return false
|
||||
}
|
||||
expected := Signature(secret, unix, body)
|
||||
return hmac.Equal([]byte(expected), []byte(strings.ToLower(signature)))
|
||||
}
|
||||
|
||||
func (d *Dispatcher) Run(ctx context.Context) {
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
d.DispatchOnce(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
func (d *Dispatcher) DispatchOnce(ctx context.Context) {
|
||||
items, err := d.Store.DueWebhooks(ctx, d.Now(), 25)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, item := range items {
|
||||
d.dispatch(ctx, item)
|
||||
}
|
||||
}
|
||||
func (d *Dispatcher) dispatch(ctx context.Context, item store.WebhookDelivery) {
|
||||
endpoint, err := d.Store.GetWebhookEndpoint(ctx, item.EndpointID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err = ValidatePublicURL(ctx, endpoint.URL); err != nil {
|
||||
d.fail(ctx, item, err)
|
||||
return
|
||||
}
|
||||
secretBytes, err := base64.RawStdEncoding.DecodeString(endpoint.Secret)
|
||||
if err != nil {
|
||||
d.fail(ctx, item, fmt.Errorf("decode webhook secret: %w", err))
|
||||
return
|
||||
}
|
||||
secret, err := d.Cipher.Decrypt(secretBytes)
|
||||
if err != nil {
|
||||
d.fail(ctx, item, fmt.Errorf("decrypt webhook secret: %w", err))
|
||||
return
|
||||
}
|
||||
timestamp := d.Now().Unix()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.URL, bytes.NewReader(item.Payload))
|
||||
if err != nil {
|
||||
d.fail(ctx, item, err)
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("User-Agent", "MEOVV-Mail-Webhooks/1.0")
|
||||
req.Header.Set("X-MEOVV-Event-ID", item.EventID)
|
||||
req.Header.Set("X-MEOVV-Event", item.EventType)
|
||||
req.Header.Set("X-MEOVV-Timestamp", strconv.FormatInt(timestamp, 10))
|
||||
req.Header.Set("X-MEOVV-Signature", "v1="+Signature(secret, timestamp, item.Payload))
|
||||
resp, err := d.HTTP.Do(req)
|
||||
if err == nil {
|
||||
io.Copy(io.Discard, io.LimitReader(resp.Body, 4096))
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||
_ = d.Store.CompleteWebhook(ctx, item.ID, true, time.Time{}, "")
|
||||
return
|
||||
}
|
||||
err = fmt.Errorf("endpoint returned %s", resp.Status)
|
||||
}
|
||||
d.fail(ctx, item, err)
|
||||
}
|
||||
func (d *Dispatcher) fail(ctx context.Context, item store.WebhookDelivery, err error) {
|
||||
age := d.Now().Sub(item.CreatedAt)
|
||||
if age >= 24*time.Hour {
|
||||
_ = d.Store.CompleteWebhook(ctx, item.ID, false, d.Now().Add(365*24*time.Hour), "retry window expired: "+err.Error())
|
||||
return
|
||||
}
|
||||
delay := time.Duration(math.Min(3600, math.Pow(2, float64(item.Attempt))*15)) * time.Second
|
||||
_ = d.Store.CompleteWebhook(ctx, item.ID, false, d.Now().Add(delay), err.Error())
|
||||
}
|
||||
|
||||
func ValidatePublicURL(ctx context.Context, raw string) error {
|
||||
parsed, err := url.Parse(raw)
|
||||
if err != nil || parsed.Scheme != "https" || parsed.Hostname() == "" {
|
||||
return fmt.Errorf("webhook URL must be public HTTPS")
|
||||
}
|
||||
ips, err := net.DefaultResolver.LookupIP(ctx, "ip", parsed.Hostname())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, ip := range ips {
|
||||
if ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsUnspecified() || ip.IsMulticast() {
|
||||
return fmt.Errorf("webhook URL resolves to a non-public address")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package webhook
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/meovv-mail/meovv-mail/internal/store"
|
||||
)
|
||||
|
||||
func TestSignatureAndVerification(t *testing.T) {
|
||||
body := []byte(`{"id":"evt_1"}`)
|
||||
timestamp := time.Now().Unix()
|
||||
signature := Signature("secret", timestamp, body)
|
||||
if !Verify("secret", signature, fmtInt(timestamp), body, 5*time.Minute) {
|
||||
t.Fatal("valid signature was rejected")
|
||||
}
|
||||
if Verify("wrong", signature, fmtInt(timestamp), body, 5*time.Minute) {
|
||||
t.Fatal("wrong secret was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetryScheduleStartsAtFifteenSeconds(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, err := store.Open(filepath.Join(t.TempDir(), "test.sqlite"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
now := time.Date(2026, 8, 12, 10, 0, 0, 0, time.UTC)
|
||||
endpoint := store.WebhookEndpoint{ID: "wh_1", Name: "test", URL: "https://example.com", Secret: "secret", Events: []string{"message.failed"}, Enabled: true, CreatedAt: now}
|
||||
if err = db.CreateWebhookEndpoint(ctx, endpoint); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = db.EnqueueEvent(ctx, "evt_1", "message.failed", []byte(`{}`), now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, err := db.DueWebhooks(ctx, now, 10)
|
||||
if err != nil || len(items) != 1 {
|
||||
t.Fatalf("due = %#v, %v", items, err)
|
||||
}
|
||||
dispatcher := &Dispatcher{Store: db, Now: func() time.Time { return now }}
|
||||
dispatcher.fail(ctx, items[0], context.DeadlineExceeded)
|
||||
if due, _ := db.DueWebhooks(ctx, now.Add(14*time.Second), 10); len(due) != 0 {
|
||||
t.Fatal("delivery retried too early")
|
||||
}
|
||||
if due, _ := db.DueWebhooks(ctx, now.Add(15*time.Second), 10); len(due) != 1 {
|
||||
t.Fatal("delivery was not scheduled after 15 seconds")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRejectsPrivateWebhookTargets(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
if err := ValidatePublicURL(ctx, "http://example.com/hook"); err == nil {
|
||||
t.Fatal("plain HTTP was accepted")
|
||||
}
|
||||
if err := ValidatePublicURL(ctx, "https://127.0.0.1/hook"); err == nil {
|
||||
t.Fatal("loopback target was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func fmtInt(value int64) string {
|
||||
return strconv.FormatInt(value, 10)
|
||||
}
|
||||
Reference in new issue
Block a user