From f884ddf7711b23071938526781896c4f906fe3cc Mon Sep 17 00:00:00 2001 From: Mauricio Perera Date: Fri, 6 Nov 2026 20:44:15 -0600 Subject: [PATCH] fix: address MCP audit findings Bound and cancel Actions log downloads, cap retained log text, paginate failed jobs, reject unrelated OAuth callbacks without aborting login, and handle CRLF icon data. Add regression tests and document log limits. --- docs/server-configuration.md | 15 +++ internal/oauth/callback.go | 14 +- internal/oauth/callback_test.go | 38 ++++-- pkg/buffer/buffer.go | 66 ++++++---- pkg/buffer/buffer_test.go | 71 ++++++++-- pkg/github/actions.go | 65 +++++++--- pkg/github/actions_regression_test.go | 180 ++++++++++++++++++++++++++ pkg/octicons/octicons.go | 7 +- pkg/octicons/octicons_test.go | 18 +++ 9 files changed, 404 insertions(+), 70 deletions(-) create mode 100644 pkg/github/actions_regression_test.go diff --git a/docs/server-configuration.md b/docs/server-configuration.md index 25c641153e..31d2230175 100644 --- a/docs/server-configuration.md +++ b/docs/server-configuration.md @@ -442,6 +442,21 @@ See [Scope Filtering](./scope-filtering.md) for details on how filtering works w --- +### Actions log downloads + +When `get_job_logs` uses `return_content=true`, each log download has a +30-second timeout and stops when the MCP call is cancelled. The server retains +at most 1 MiB of log text per response, including when collecting multiple failed +jobs. Each line retains at most 16 KiB of text plus a truncation marker. + +The server preserves recent lines when the byte or line limit is reached and +marks omitted content with `[TRUNCATED]`. If the combined byte budget is exhausted +while collecting failed jobs, remaining jobs have an error explaining that their +logs should be retrieved individually. JSON encoding and job metadata add to the +size of the response beyond the log-text budget. + +--- + ## Troubleshooting | Problem | Cause | Solution | diff --git a/internal/oauth/callback.go b/internal/oauth/callback.go index 1e643e207d..7d4ec4e5c8 100644 --- a/internal/oauth/callback.go +++ b/internal/oauth/callback.go @@ -82,6 +82,14 @@ func (cs *callbackServer) handler(expectedState string) http.Handler { mux := http.NewServeMux() mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) { q := r.URL.Query() + // Requests unrelated to this flow must not consume its result or close + // the listener. Validate state even when the provider reports an error. + if q.Get("state") != expectedState { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.WriteHeader(http.StatusBadRequest) + renderError(w, "state mismatch") + return + } if errCode := q.Get("error"); errCode != "" { msg := errCode @@ -93,12 +101,6 @@ func (cs *callbackServer) handler(expectedState string) http.Handler { return } - if q.Get("state") != expectedState { - cs.report(callbackResult{err: fmt.Errorf("state mismatch (possible CSRF)")}) - renderError(w, "state mismatch") - return - } - code := q.Get("code") if code == "" { cs.report(callbackResult{err: fmt.Errorf("no authorization code in callback")}) diff --git a/internal/oauth/callback_test.go b/internal/oauth/callback_test.go index 45a8fa71c4..aaade00eb3 100644 --- a/internal/oauth/callback_test.go +++ b/internal/oauth/callback_test.go @@ -39,12 +39,34 @@ func TestCallbackHandlerSuccess(t *testing.T) { } func TestCallbackHandlerStateMismatch(t *testing.T) { - rec, res := serveCallback(t, "expected", "code=the-code&state=attacker") - - require.Error(t, res.err) - assert.Empty(t, res.code) - assert.Contains(t, res.err.Error(), "state mismatch") - assert.Contains(t, rec.Body.String(), "state mismatch") + for _, query := range []string{ + "code=the-code&state=attacker", + "code=the-code", + "error=access_denied", + "error=access_denied&state=attacker", + } { + t.Run(query, func(t *testing.T) { + cs := &callbackServer{results: make(chan callbackResult, 1)} + handler := cs.handler("expected") + rec := httptest.NewRecorder() + handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/callback?"+query, nil)) + assert.Equal(t, http.StatusBadRequest, rec.Code) + assert.Contains(t, rec.Body.String(), "state mismatch") + select { + case res := <-cs.results: + t.Fatalf("unrelated callback consumed login result: %+v", res) + default: + } + handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/callback?code=legitimate&state=expected", nil)) + select { + case res := <-cs.results: + require.NoError(t, res.err) + assert.Equal(t, "legitimate", res.code) + default: + t.Fatal("legitimate callback was not accepted") + } + }) + } } func TestCallbackHandlerMissingCode(t *testing.T) { @@ -55,7 +77,7 @@ func TestCallbackHandlerMissingCode(t *testing.T) { } func TestCallbackHandlerOAuthError(t *testing.T) { - _, res := serveCallback(t, "state123", "error=access_denied&error_description=user+said+no") + _, res := serveCallback(t, "state123", "state=state123&error=access_denied&error_description=user+said+no") require.Error(t, res.err) assert.Contains(t, res.err.Error(), "access_denied") @@ -63,7 +85,7 @@ func TestCallbackHandlerOAuthError(t *testing.T) { } func TestCallbackHandlerEscapesError(t *testing.T) { - rec, _ := serveCallback(t, "state123", "error=evil&error_description=%3Cscript%3Ealert(1)%3C%2Fscript%3E") + rec, _ := serveCallback(t, "state123", "state=state123&error=evil&error_description=%3Cscript%3Ealert(1)%3C%2Fscript%3E") body := rec.Body.String() assert.NotContains(t, body, "