signature.go97 lines, 2.9 KB
| 1 | // Package signature signs webhooks, and verifies them on the receiving side. |
| 2 | // The scheme is Standard Webhooks': an HMAC-SHA256 of the message's ID, its |
| 3 | // timestamp and its body, sent in three headers. |
| 4 | package signature |
| 5 | |
| 6 | import ( |
| 7 | "crypto/hmac" |
| 8 | "crypto/rand" |
| 9 | "crypto/sha256" |
| 10 | "encoding/base64" |
| 11 | "errors" |
| 12 | "fmt" |
| 13 | "net/http" |
| 14 | "strconv" |
| 15 | "strings" |
| 16 | "time" |
| 17 | ) |
| 18 | |
| 19 | // The headers every delivery carries. |
| 20 | const ( |
| 21 | HeaderID = "Webhook-Id" |
| 22 | HeaderTimestamp = "Webhook-Timestamp" |
| 23 | HeaderSignature = "Webhook-Signature" |
| 24 | ) |
| 25 | |
| 26 | // Tolerance is how far a message's timestamp may be from the receiver's clock. |
| 27 | const Tolerance = 5 * time.Minute |
| 28 | |
| 29 | // Why Verify refused a message. |
| 30 | var ( |
| 31 | ErrMissingHeaders = errors.New("signature: missing webhook headers") |
| 32 | ErrTimestamp = errors.New("signature: timestamp outside the tolerance") |
| 33 | ErrNoMatch = errors.New("signature: no signature matches") |
| 34 | ) |
| 35 | |
| 36 | const secretPrefix = "whsec_" |
| 37 | |
| 38 | // NewSecret returns a new random secret: whsec_, then 32 bytes in base64. |
| 39 | func NewSecret() string { |
| 40 | key := make([]byte, 32) |
| 41 | rand.Read(key) // never fails, says crypto/rand |
| 42 | return secretPrefix + base64.StdEncoding.EncodeToString(key) |
| 43 | } |
| 44 | |
| 45 | // mac returns v1, then the base64 HMAC-SHA256 of id.timestamp.body. |
| 46 | func mac(secret, id string, ts int64, body []byte) (string, error) { |
| 47 | key, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(secret, secretPrefix)) |
| 48 | if err != nil { |
| 49 | return "", fmt.Errorf("signature: invalid secret: %w", err) |
| 50 | } |
| 51 | h := hmac.New(sha256.New, key) |
| 52 | fmt.Fprintf(h, "%s.%d.", id, ts) |
| 53 | h.Write(body) |
| 54 | return "v1," + base64.StdEncoding.EncodeToString(h.Sum(nil)), nil |
| 55 | } |
| 56 | |
| 57 | // Sign sets the three headers on h, for a message with this id and body, |
| 58 | // sent at time at. |
| 59 | func Sign(h http.Header, secret, id string, at time.Time, body []byte) error { |
| 60 | sig, err := mac(secret, id, at.Unix(), body) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | h.Set(HeaderID, id) |
| 65 | h.Set(HeaderTimestamp, strconv.FormatInt(at.Unix(), 10)) |
| 66 | h.Set(HeaderSignature, sig) |
| 67 | return nil |
| 68 | } |
| 69 | |
| 70 | // Verify checks that a message was signed with secret, and recently: now is |
| 71 | // the receiver's clock. Run it on the raw body, before decoding anything. |
| 72 | func Verify(h http.Header, secret string, body []byte, now time.Time) error { |
| 73 | id, ts, sigs := h.Get(HeaderID), h.Get(HeaderTimestamp), h.Get(HeaderSignature) |
| 74 | if id == "" || ts == "" || sigs == "" { |
| 75 | return ErrMissingHeaders |
| 76 | } |
| 77 | sec, err := strconv.ParseInt(ts, 10, 64) |
| 78 | if err != nil { |
| 79 | return ErrTimestamp |
| 80 | } |
| 81 | if d := now.Sub(time.Unix(sec, 0)); d > Tolerance || d < -Tolerance { |
| 82 | return ErrTimestamp |
| 83 | } |
| 84 | want, err := mac(secret, id, sec, body) |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | // Several signatures, separated by spaces, while a secret is rotated. |
| 89 | for sig := range strings.FieldsSeq(sigs) { |
| 90 | // In constant time: == stops at the first wrong byte, and how long it |
| 91 | // takes tells an attacker how many bytes they got right. |
| 92 | if hmac.Equal([]byte(sig), []byte(want)) { |
| 93 | return nil |
| 94 | } |
| 95 | } |
| 96 | return ErrNoMatch |
| 97 | } |