From 2f231dd2963d52206cac9520b9a67b887a5f409b Mon Sep 17 00:00:00 2001 From: "stackweaver-release-bot[bot]" <287320289+stackweaver-release-bot@users.noreply.github.com> Date: Fri, 4 Dec 2026 06:29:23 +0000 Subject: [PATCH] fix(api): limit the request size of the GitHub App webhook and provisioning callbacks --- .../anonymous_body_caps_integration_test.go | 95 +++++++++++++++++++ backend/internal/api/v2/routes/routes.go | 7 +- 2 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 backend/internal/api/v2/routes/anonymous_body_caps_integration_test.go diff --git a/backend/internal/api/v2/routes/anonymous_body_caps_integration_test.go b/backend/internal/api/v2/routes/anonymous_body_caps_integration_test.go new file mode 100644 index 0000000..9a41e67 --- /dev/null +++ b/backend/internal/api/v2/routes/anonymous_body_caps_integration_test.go @@ -0,0 +1,95 @@ +// Copyright (c) 2025 VH & Co BV. Licensed under the Business Source License 1.1. See LICENSE for details. + +//go:build integration + +package routes_test + +import ( + "bytes" + "context" + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/pem" + "io" + "net/http" + "net/http/httptest" + "os" + "strings" + "testing" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + v2routes "github.com/michielvha/stackweaver/backend/internal/api/v2/routes" + "github.com/michielvha/stackweaver/core/dbtest" + "github.com/michielvha/stackweaver/core/services/vcs" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +// TestAnonymousBodyCaps: the two routes a caller reaches without a credential and that read a body +// before checking anything refuse an oversized body first, on the production router. +func TestAnonymousBodyCaps(t *testing.T) { + dbURL := os.Getenv("TEST_DATABASE_URL") + if dbURL == "" { + t.Skip("TEST_DATABASE_URL not set - skipping integration test") + } + t.Setenv("DEV_INSECURE_KEY", "1") + key, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + t.Setenv("GITHUB_APP_ID", "1") + t.Setenv("GITHUB_APP_NAME", "body-cap-test") + t.Setenv("GITHUB_APP_PRIVATE_KEY", string(pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}))) + t.Setenv("GITHUB_WEBHOOK_SECRET", "body-cap-test-secret") + manager, err := vcs.NewGitHubAppManager() + if err != nil || !manager.IsEnabled() { + t.Fatalf("an enabled GitHub App manager is needed for the webhook route: %v", err) + } + db, err := dbtest.Open(t, dbURL, &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}) + if err != nil { + t.Fatalf("connect test db: %v", err) + } + gin.SetMode(gin.TestMode) + r := gin.New() + v2routes.SetupV2Routes(r, db, nil, manager) + + send := func(path string, body io.Reader, length int64) int { + req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, path, body) + req.Header.Set("Content-Type", "application/json") + req.ContentLength = length + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + return w.Code + } + padded := func(size int) []byte { + head := `{"host_config_key":"k","pad":"` + return []byte(head + strings.Repeat("a", size-len(head)-2) + `"}`) + } + callback := "/api/v2/ansible/job-templates/" + uuid.NewString() + "/callback" + webhook := "/api/v2/vcs-connections/github/webhook" + + const callbackCap, webhookCap = 64 << 10, 25 << 20 + big := padded(callbackCap + 1) + if got := send(callback, bytes.NewReader(big), int64(len(big))); got != http.StatusRequestEntityTooLarge { + t.Errorf("callback with a %d byte body answered %d, want 413", len(big), got) + } + // A chunked body declares no length; it must still be cut off before the template lookup, + // which would answer 404 for this unknown template. + if got := send(callback, bytes.NewReader(big), -1); got == http.StatusNotFound { + t.Errorf("chunked oversized callback reached the template lookup (404)") + } + small := padded(256) + if got := send(callback, bytes.NewReader(small), int64(len(small))); got != http.StatusNotFound { + t.Errorf("callback with a small body answered %d, want 404 for the unknown template", got) + } + + huge := bytes.Repeat([]byte("a"), webhookCap+1) + if got := send(webhook, bytes.NewReader(huge), int64(len(huge))); got != http.StatusRequestEntityTooLarge { + t.Errorf("webhook with a %d byte body answered %d, want 413", len(huge), got) + } + if got := send(webhook, bytes.NewReader([]byte(`{}`)), 2); got != http.StatusUnauthorized { + t.Errorf("unsigned webhook with a small body answered %d, want 401 from the signature check", got) + } +} diff --git a/backend/internal/api/v2/routes/routes.go b/backend/internal/api/v2/routes/routes.go index bd1614a..d007482 100644 --- a/backend/internal/api/v2/routes/routes.go +++ b/backend/internal/api/v2/routes/routes.go @@ -1111,7 +1111,9 @@ func SetupV2Routes( repository.NewAnsibleJobTemplateRepository(db), newWebhookTemplateLaunchService(db, ansibleQueueForWebhook, vcsRegistry, vcsConnectionRepo), ) - vcsWebhook.POST("/webhook", appHandler.HandleInstallationWebhook) + // GitHub sends no delivery above 25 MB, so a larger body is refused before it is read + // and before the signature check. + vcsWebhook.POST("/webhook", middleware.MaxBodyBytes(25<<20), appHandler.HandleInstallationWebhook) } } @@ -1460,7 +1462,8 @@ func SetupV2Routes( repository.NewAnsibleInventoryRepository(db), ansibleServices.JobService, ) - r.POST("/api/v2/ansible/job-templates/:id/callback", provisioningCallbackHandler.Handle) + // The body is one short key, so anything past 64 KiB is refused before it is read. + r.POST("/api/v2/ansible/job-templates/:id/callback", middleware.MaxBodyBytes(64<<10), provisioningCallbackHandler.Handle) // ========================================== // Runner Agent API (for self-hosted runners)