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