| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/aes" |
| 7 | "crypto/cipher" |
| 8 | "crypto/rand" |
| 9 | "encoding/base64" |
| 10 | "encoding/json" |
| 11 | "errors" |
| 12 | "io" |
| 13 | "net" |
| 14 | "net/http" |
| 15 | "net/http/httptest" |
| 16 | "net/netip" |
| 17 | "strconv" |
| 18 | "testing" |
| 19 | "time" |
| 20 | |
| 21 | "github.com/jackc/pgx/v5/pgxpool" |
| 22 | ) |
| 23 | |
| 24 | func TestBlocked(t * |
| 25 | tests := []struct |
| 26 | addr string |
| 27 | blockedStrict, blockedLAN bool |
| 28 | }{ |
| 29 | {"93.184.216.34", false, |
| 30 | {"2606:2800:220:1::1", false, |
| 31 | {"127.0.0.1", true, |
| 32 | {"::1", true, |
| 33 | {"10.1.2.3", true, |
| 34 | {"172.16.0.1", true, |
| 35 | {"192.168.1.4", true, |
| 36 | {"100.64.0.1", true, |
| 37 | {"fd00::1", true, |
| 38 | {"::ffff:127.0.0.1", true, |
| 39 | {"169.254.169.254", true, |
| 40 | {"::ffff:169.254.169.254", true, |
| 41 | {"fe80::1", true, |
| 42 | {"0.0.0.0", true, |
| 43 | {"224.0.0.1", true, |
| 44 | } |
| 45 | for _, tt |
| 46 | a := netip. |
| 47 | if got := blocked |
| 48 | t.Errorf("blocked( |
| 49 | } |
| 50 | if got := blocked |
| 51 | t.Errorf("blocked( |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestWebhookClientRefusesLoopbackOnTheDialedIP(t * |
| 57 | hits := make( |
| 58 | srv := httptest. |
| 59 | defer srv.Close() |
| 60 | |
| 61 | // "localhost" resolves to 127.0.0.1: the check runs after DNS, on the real address. |
| 62 | url := "http://localhost:" |
| 63 | _, err := |
| 64 | if !errors.Is |
| 65 | t.Fatalf("err = |
| 66 | } |
| 67 | if len(hits) |
| 68 | t.Error("request reached the loopback server |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestSignedHeaders(t * |
| 73 | secret, body := |
| 74 | now := time. |
| 75 | h := signedHeaders( |
| 76 | |
| 77 | if h["X-Ghost-Timestamp" |
| 78 | t.Errorf("headers = |
| 79 | } |
| 80 | // What a receiver computes: HMAC over "<timestamp>.<raw body>". |
| 81 | if want := " |
| 82 | t.Errorf("X-Ghost-Signature-256 = |
| 83 | } |
| 84 | if want := " |
| 85 | t.Errorf("X-Hub-Signature-256 = |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestOpenSecret(t * |
| 90 | key := make([] |
| 91 | rand.Read(key |
| 92 | got, err := |
| 93 | if err != nil |
| 94 | t.Fatalf("openSecret = |
| 95 | } |
| 96 | other := make([] |
| 97 | if _, err |
| 98 | t.Error("opened a secret with the wrong key |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestParseRetryAfter(t * |
| 103 | now := time. |
| 104 | tests := []struct |
| 105 | v string |
| 106 | want time. |
| 107 | ok bool |
| 108 | }{ |
| 109 | {"120", 2 * |
| 110 | {"Tue, 29 Sep 2026 12:05:00 GMT", 5 * |
| 111 | {"", 0, false} |
| 112 | {"soon", 0, |
| 113 | } |
| 114 | for _, tt |
| 115 | got, ok := |
| 116 | if got != tt |
| 117 | t.Errorf("parseRetryAfter( |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // sealSecret encrypts like the API does: "v1:" + base64(nonce || AES-256-GCM ciphertext). |
| 123 | func sealSecret(t * |
| 124 | t.Helper() |
| 125 | block, err := |
| 126 | if err != nil |
| 127 | t.Fatal(err |
| 128 | } |
| 129 | gcm, err := |
| 130 | if err != nil |
| 131 | t.Fatal(err |
| 132 | } |
| 133 | nonce := make([] |
| 134 | rand.Read(nonce |
| 135 | return "v1:" + base64 |
| 136 | } |
| 137 | |
| 138 | // webhookFixture inserts an organization, an endpoint pointing at url and one |
| 139 | // webhook job. Deleting the organization cascades to all three. |
| 140 | func webhookFixture(t * |
| 141 | t.Helper() |
| 142 | key := make([] |
| 143 | rand.Read(key |
| 144 | suffix := strconv. |
| 145 | org := "org_test_delivery_" |
| 146 | |
| 147 | _, err := |
| 148 | `INSERT INTO organization (id, name, slug, created_at) VALUES ($1, 'Delivery test', $2, now())`, |
| 149 | org, "test-delivery-"+ |
| 150 | if err != nil |
| 151 | t.Fatal(err |
| 152 | } |
| 153 | t.Cleanup(func() |
| 154 | if _, err |
| 155 | t.Errorf("cleanup: |
| 156 | } |
| 157 | }) |
| 158 | |
| 159 | var endpointID string |
| 160 | err = db. |
| 161 | `INSERT INTO webhook_endpoint (id, organization_id, url, secret, events) |
| 162 | VALUES ($1, $2, $3, $4, '{issue.opened}') RETURNING id`, |
| 163 | "whk_test_"+suffix, |
| 164 | if err != nil |
| 165 | t.Fatal(err |
| 166 | } |
| 167 | |
| 168 | payload, _ := |
| 169 | j := Job |
| 170 | err = db. |
| 171 | `INSERT INTO delivery_job (kind, idempotency_key, payload, endpoint_id) |
| 172 | VALUES ('webhook', $1 || gen_random_uuid()::text, $2, $3) RETURNING id`, |
| 173 | testKeyPrefix, payload, |
| 174 | if err != nil |
| 175 | t.Fatal(err |
| 176 | } |
| 177 | return webhookSender{db |
| 178 | } |
| 179 | |
| 180 | func TestWebhookSendSignsAndRecordsTheAttempt(t * |
| 181 | db := testDB( |
| 182 | type received struct |
| 183 | header http. |
| 184 | body []byte |
| 185 | } |
| 186 | reqs := make( |
| 187 | srv := httptest. |
| 188 | body, _ := |
| 189 | reqs <- received |
| 190 | w.Write([]byte |
| 191 | })) |
| 192 | defer srv.Close() |
| 193 | s, j, |
| 194 | |
| 195 | if err := s |
| 196 | t.Fatal(err |
| 197 | } |
| 198 | r := <-reqs |
| 199 | gotBody := r. |
| 200 | got := struct{ |
| 201 | |
| 202 | if string(gotBody) |
| 203 | t.Errorf("body = |
| 204 | } |
| 205 | ts := got. |
| 206 | if want := " |
| 207 | t.Error("signature does not verify with the endpoint secret |
| 208 | } |
| 209 | if got.Header. |
| 210 | t.Errorf("X-Ghost-Delivery = |
| 211 | } |
| 212 | |
| 213 | var ( |
| 214 | status int |
| 215 | body string |
| 216 | ) |
| 217 | err := db. |
| 218 | `SELECT status_code, response_body FROM delivery_attempt WHERE job_id = $1`, j. |
| 219 | if err != nil |
| 220 | t.Fatal(err |
| 221 | } |
| 222 | if status != 200 |
| 223 | t.Errorf("attempt = |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | func TestWebhookResponseClassification(t * |
| 228 | db := testDB( |
| 229 | tests := []struct |
| 230 | name string |
| 231 | status int |
| 232 | header map[string] |
| 233 | check func(t |
| 234 | }{ |
| 235 | {"500 retries", 500, |
| 236 | var p permanentError |
| 237 | if err == nil |
| 238 | t.Errorf("err = |
| 239 | } |
| 240 | }}, |
| 241 | {"410 disables the endpoint", 410, |
| 242 | var p permanentError |
| 243 | if !errors.As |
| 244 | t.Errorf("err = |
| 245 | } |
| 246 | if active { |
| 247 | t.Error("endpoint still active after 410 |
| 248 | } |
| 249 | }}, |
| 250 | {"429 honors Retry-After", 429, |
| 251 | var r retryAfterError |
| 252 | if !errors.As |
| 253 | t.Errorf("err = |
| 254 | } |
| 255 | }}, |
| 256 | {"redirect is not followed", 302, |
| 257 | if err == nil |
| 258 | t.Errorf("err = |
| 259 | } |
| 260 | }}, |
| 261 | } |
| 262 | for _, tt |
| 263 | t.Run(tt |
| 264 | srv := httptest. |
| 265 | for k, v |
| 266 | w.Header().Set |
| 267 | } |
| 268 | w.WriteHeader(tt |
| 269 | })) |
| 270 | defer srv.Close() |
| 271 | s, j, |
| 272 | |
| 273 | err := s. |
| 274 | |
| 275 | var active bool |
| 276 | if err := db |
| 277 | t.Fatal(err |
| 278 | } |
| 279 | tt.check(t |
| 280 | }) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | func TestWebhookSendDropsJobForDisabledEndpoint(t * |
| 285 | db := testDB( |
| 286 | hits := make( |
| 287 | srv := httptest. |
| 288 | defer srv.Close() |
| 289 | s, j, |
| 290 | if _, err |
| 291 | t.Fatal(err |
| 292 | } |
| 293 | |
| 294 | var p permanentError |
| 295 | if err := s |
| 296 | t.Errorf("err = |
| 297 | } |
| 298 | if len(hits) |
| 299 | t.Error("sent to a disabled endpoint |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // Sealed by sealWebhookSecret in apps/api/src/lib/webhooks/webhooks.ts with a |
| 304 | // key of 32 bytes of 7. If this breaks, the API and delivery disagree on the format. |
| 305 | func TestOpenSecretSealedByTheAPI(t * |
| 306 | key := bytes. |
| 307 | got, err := |
| 308 | if err != nil |
| 309 | t.Fatalf("openSecret = |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | func TestHostLimiter(t * |
| 314 | h := newHostLimiter( |
| 315 | if !h.acquire |
| 316 | t.Fatal("could not take the first two slots |
| 317 | } |
| 318 | if h.acquire( |
| 319 | t.Error("took a third slot for a:443 |
| 320 | } |
| 321 | if !h.acquire |
| 322 | t.Error("a busy a:443 blocked b:443 |
| 323 | } |
| 324 | h.release("a:443 |
| 325 | if !h.acquire |
| 326 | t.Error("released slot was not reusable |
| 327 | } |
| 328 | var none *hostLimiter |
| 329 | if !none.acquire |
| 330 | t.Error("a nil limiter refused |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | func TestWebhookSendWaitsWhenItsHostIsBusy(t * |
| 335 | db := testDB( |
| 336 | hits := make( |
| 337 | srv := httptest. |
| 338 | defer srv.Close() |
| 339 | s, j, |
| 340 | s.hosts = |
| 341 | s.hosts.acquire |
| 342 | |
| 343 | var r retryAfterError |
| 344 | if err := s |
| 345 | t.Errorf("err = |
| 346 | } |
| 347 | if len(hits) |
| 348 | t.Error("sent while the host was at its limit |
| 349 | } |
| 350 | } |