From 9128605d0168ce09e9d623d1b880c48efb8f81e1 Mon Sep 17 00:00:00 2001 From: Adam Holt Date: Mon, 9 Nov 2026 12:33:56 +0100 Subject: [PATCH] Avoid overflow in validation error capacity Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- pkg/errors/error.go | 2 +- pkg/errors/error_test.go | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/pkg/errors/error.go b/pkg/errors/error.go index 7c9fb049e1..cdc8e551cc 100644 --- a/pkg/errors/error.go +++ b/pkg/errors/error.go @@ -213,7 +213,7 @@ func formatGitHubValidationError(resp *github.Response, err error) error { return err } - parts := make([]string, 0, len(ghErr.Errors)+1) + var parts []string if summary := sanitizeGitHubValidationText(ghErr.Message); summary != "" { parts = append(parts, summary) } diff --git a/pkg/errors/error_test.go b/pkg/errors/error_test.go index dc47b9d58b..54873c649f 100644 --- a/pkg/errors/error_test.go +++ b/pkg/errors/error_test.go @@ -3,13 +3,15 @@ package errors import ( "context" "fmt" + "net/http" + "strings" + "testing" + "time" + "github.com/google/go-github/v92/github" "github.com/modelcontextprotocol/go-sdk/mcp" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "net/http" - "testing" - "time" ) func TestGitHubErrorContext(t *testing.T) { @@ -689,6 +691,34 @@ func TestNewGitHubAPIErrorResponse_RateLimits(t *testing.T) { } func TestNewGitHubAPIErrorResponse_ValidationMessages(t *testing.T) { + t.Run("large validation error lists are formatted without preallocating from their length", func(t *testing.T) { + const errorCount = 1000 + validationErrors := make([]github.Error, errorCount) + for i := range validationErrors { + validationErrors[i] = github.Error{ + Resource: "Repository", + Field: fmt.Sprintf("field-%d", i), + Code: "invalid", + } + } + + originalErr := &github.ErrorResponse{ + Response: &http.Response{StatusCode: http.StatusUnprocessableEntity}, + Message: "Validation Failed", + Errors: validationErrors, + } + result := NewGitHubAPIErrorResponse( + ContextWithGitHubErrors(context.Background()), + "API call failed", + nil, + originalErr, + ) + + text := requireErrorText(t, result) + assert.Equal(t, errorCount+1, len(strings.Split(text, "\n"))) + assert.Contains(t, text, "Repository.field-999 (invalid)") + }) + t.Run("ruleset ErrorResponse includes sanitized structured validation messages", func(t *testing.T) { ctx := ContextWithGitHubErrors(context.Background())