| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/aes" |
| 6 | "crypto/cipher" |
| 7 | "crypto/hmac" |
| 8 | "crypto/sha256" |
| 9 | "encoding/base64" |
| 10 | "encoding/hex" |
| 11 | "encoding/json" |
| 12 | "errors" |
| 13 | "fmt" |
| 14 | "io" |
| 15 | "log" |
| 16 | "net/http" |
| 17 | "strconv" |
| 18 | "strings" |
| 19 | "time" |
| 20 | |
| 21 | "github.com/jackc/pgx/v5" |
| 22 | "github.com/jackc/pgx/v5/pgxpool" |
| 23 | ) |
| 24 | |
| 25 | // webhookPayload is what the API puts in delivery_job.payload for kind 'webhook'. |
| 26 | // Body is the JSON the receiver gets, frozen at fan-out so a retry sends the same bytes. |
| 27 | type webhookPayload struct { |
| 28 | Event string `json:"event" |
| 29 | Body string `json:"body" |
| 30 | } |
| 31 | |
| 32 | // Stored response bodies are cut to this; the delivery log only needs a glance. |
| 33 | const maxResponseBody = 4 |
| 34 | |
| 35 | type webhookSender struct { |
| 36 | db *pgxpool |
| 37 | client *http |
| 38 | key []byte // |
| 39 | } |
| 40 | |
| 41 | func (w webhookSender |
| 42 | if j.EndpointID |
| 43 | return permanentError{errors |
| 44 | } |
| 45 | var p webhookPayload |
| 46 | if err := json |
| 47 | return permanentError{fmt |
| 48 | } |
| 49 | |
| 50 | // Read at send time: the endpoint may have been disabled or its URL changed since enqueue. |
| 51 | var ( |
| 52 | url, sealed string |
| 53 | active bool |
| 54 | ) |
| 55 | err := w. |
| 56 | `SELECT url, secret, active FROM webhook_endpoint WHERE id = $1`, *j |
| 57 | ).Scan(&url |
| 58 | if errors.Is( |
| 59 | return permanentError{errors |
| 60 | } |
| 61 | if err != nil |
| 62 | return err |
| 63 | } |
| 64 | if !active { |
| 65 | return permanentError{errors |
| 66 | } |
| 67 | secret, err := |
| 68 | if err != nil |
| 69 | return err |
| 70 | } |
| 71 | |
| 72 | req, err := |
| 73 | if err != nil |
| 74 | return permanentError{err |
| 75 | } |
| 76 | for k, v |
| 77 | req.Header.Set |
| 78 | } |
| 79 | |
| 80 | started := time. |
| 81 | res, sendErr := |
| 82 | var ( |
| 83 | status *int |
| 84 | resBody *string |
| 85 | ) |
| 86 | if sendErr == nil |
| 87 | b, _ := |
| 88 | res.Body.Close |
| 89 | status, resBody = |
| 90 | } |
| 91 | w.recordAttempt(ctx |
| 92 | |
| 93 | if sendErr != nil |
| 94 | return sendErr |
| 95 | } |
| 96 | return w.classify( |
| 97 | } |
| 98 | |
| 99 | // classify turns a response into the job's next step. |
| 100 | func (w webhookSender |
| 101 | switch code := res |
| 102 | case code >= 200 |
| 103 | return nil |
| 104 | case code == http |
| 105 | _, err := |
| 106 | UPDATE webhook_endpoint |
| 107 | SET active = false, disabled_reason = 'The endpoint responded 410 Gone.', updated_at = now() |
| 108 | WHERE id = $1`, endpointID) |
| 109 | if err != nil |
| 110 | return err |
| 111 | } |
| 112 | return permanentError{errors |
| 113 | case code == http |
| 114 | err := fmt. |
| 115 | if after, ok |
| 116 | return retryAfterError{err |
| 117 | } |
| 118 | return err |
| 119 | default: |
| 120 | // ponytail: every other status retries with backoff, 4xx included; a receiver fixing its config then catches up. Fail fast on 4xx if retries waste too much. |
| 121 | return fmt.Errorf( |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // recordAttempt writes one delivery_attempt row. A failure to write it is logged, not fatal: |
| 126 | // the job's own result matters more than its log line. |
| 127 | func (w webhookSender |
| 128 | flat := make( |
| 129 | for k := range |
| 130 | flat[k] = |
| 131 | } |
| 132 | var errText *string |
| 133 | if sendErr != nil |
| 134 | errText = ptr( |
| 135 | } |
| 136 | _, err := |
| 137 | INSERT INTO delivery_attempt (job_id, started_at, duration_ms, status_code, error, request_headers, response_body) |
| 138 | VALUES ($1, $2, $3, $4, $5, $6, $7)`, |
| 139 | jobID, started, |
| 140 | if err != nil |
| 141 | log.Printf("job |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // signedHeaders signs "<timestamp>.<body>" so a receiver can reject replays older |
| 146 | // than a few minutes. X-Hub-Signature-256 signs the body alone, for GitHub-style receivers. |
| 147 | func signedHeaders(secret [] |
| 148 | ts := strconv. |
| 149 | return map[string]string |
| 150 | "Content-Type": "application/json", |
| 151 | "User-Agent": "Ghost-Hookshot/1", |
| 152 | "X-Ghost-Event": event, |
| 153 | "X-Ghost-Delivery": deliveryID, |
| 154 | "X-Ghost-Timestamp": ts, |
| 155 | "X-Ghost-Signature-256": "sha256=" |
| 156 | "X-Hub-Signature-256": "sha256=" |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func hmacHex(secret [] |
| 161 | mac := hmac. |
| 162 | for _, p |
| 163 | mac.Write(p |
| 164 | } |
| 165 | return hex.EncodeToString( |
| 166 | } |
| 167 | |
| 168 | // openSecret decrypts webhook_endpoint.secret, stored as |
| 169 | // "v1:" + base64(12-byte nonce || AES-256-GCM ciphertext and tag). |
| 170 | func openSecret(key [] |
| 171 | if len(key) |
| 172 | return nil, errors. |
| 173 | } |
| 174 | raw, ok := |
| 175 | if !ok { |
| 176 | return nil, errors. |
| 177 | } |
| 178 | data, err := |
| 179 | if err != nil |
| 180 | return nil, fmt. |
| 181 | } |
| 182 | block, err := |
| 183 | if err != nil |
| 184 | return nil, err |
| 185 | } |
| 186 | gcm, err := |
| 187 | if err != nil |
| 188 | return nil, err |
| 189 | } |
| 190 | if len(data) |
| 191 | return nil, errors. |
| 192 | } |
| 193 | nonce, ciphertext := |
| 194 | plain, err := |
| 195 | if err != nil |
| 196 | return nil, fmt. |
| 197 | } |
| 198 | return plain, nil |
| 199 | } |
| 200 | |
| 201 | // parseRetryAfter reads Retry-After as seconds or an HTTP date. |
| 202 | func parseRetryAfter(v string |
| 203 | if v == "" |
| 204 | return 0, false |
| 205 | } |
| 206 | if s, err |
| 207 | return time.Duration( |
| 208 | } |
| 209 | if t, err |
| 210 | return max(t. |
| 211 | } |
| 212 | return 0, false |
| 213 | } |
| 214 | |
| 215 | func ptr[T any |