Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/server-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
14 changes: 8 additions & 6 deletions internal/oauth/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")})
Expand Down
38 changes: 30 additions & 8 deletions internal/oauth/callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -55,15 +77,15 @@ 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")
assert.Contains(t, res.err.Error(), "user said no")
}

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, "<script>", "error message must be HTML-escaped")
Expand Down
66 changes: 40 additions & 26 deletions pkg/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import (
"strings"
)

// maxLineSize is the maximum size for a single log line (10MB).
// GitHub Actions logs can contain extremely long lines (base64 content, minified JS, etc.)
const maxLineSize = 10 * 1024 * 1024
// MaxLogBytes bounds retained log content, including truncation markers.
const MaxLogBytes = 1 << 20 // 1 MiB

const (
maxLineSize = 16 << 10 // 16 KiB per line
truncationMarker = "... [TRUNCATED]"
)

// ProcessResponseAsRingBufferToEnd reads the body of an HTTP response line by line,
// storing only the last maxJobLogLines lines using a ring buffer (sliding window).
Expand All @@ -30,7 +34,8 @@ const maxLineSize = 10 * 1024 * 1024
//
// The function uses a ring buffer to efficiently store only the last maxJobLogLines lines.
// If the response contains more lines than maxJobLogLines, only the most recent lines are kept.
// Lines exceeding maxLineSize are truncated with a marker.
// Lines exceeding maxLineSize are truncated with a marker. Oldest lines are
// also evicted when necessary to keep the output within MaxLogBytes.
func ProcessResponseAsRingBufferToEnd(httpResp *http.Response, maxJobLogLines int) (string, int, *http.Response, error) {
if maxJobLogLines <= 0 {
maxJobLogLines = 500
Expand All @@ -40,12 +45,19 @@ func ProcessResponseAsRingBufferToEnd(httpResp *http.Response, maxJobLogLines in
}

lines := make([]string, maxJobLogLines)
validLines := make([]bool, maxJobLogLines)
totalLines := 0
writeIndex := 0
first, count, retainedBytes := 0, 0, 0
byteTruncated := false
// Reserve room for the marker and its newline if the byte budget evicts lines.
contentBudget := MaxLogBytes - len(truncationMarker) - 1
evict := func() {
retainedBytes -= len(lines[first]) + 1
lines[first] = ""
first = (first + 1) % maxJobLogLines
count--
}

const readBufferSize = 64 * 1024 // 64KB read buffer
const maxDisplayLength = 1000 // Keep first 1000 chars of truncated lines

readBuf := make([]byte, readBufferSize)
var currentLine strings.Builder
Expand All @@ -54,16 +66,20 @@ func ProcessResponseAsRingBufferToEnd(httpResp *http.Response, maxJobLogLines in
// storeLine saves the current line to the ring buffer and resets state
storeLine := func() {
line := currentLine.String()
if lineTruncated && len(line) > maxDisplayLength {
line = line[:maxDisplayLength]
}
if lineTruncated {
line += "... [TRUNCATED]"
line = strings.ToValidUTF8(line, "") + truncationMarker
}
lines[writeIndex] = line
validLines[writeIndex] = true
if count == maxJobLogLines {
evict()
}
for count > 0 && retainedBytes+len(line)+1 > contentBudget {
evict()
byteTruncated = true
}
lines[(first+count)%maxJobLogLines] = line
count++
retainedBytes += len(line) + 1
totalLines++
writeIndex = (writeIndex + 1) % maxJobLogLines
currentLine.Reset()
lineTruncated = false
}
Expand Down Expand Up @@ -114,20 +130,18 @@ func ProcessResponseAsRingBufferToEnd(httpResp *http.Response, maxJobLogLines in
}
}

var result []string
linesInBuffer := min(totalLines, maxJobLogLines)

startIndex := 0
if totalLines > maxJobLogLines {
startIndex = writeIndex
var result strings.Builder
result.Grow(retainedBytes + len(truncationMarker) + 1)
if byteTruncated {
result.WriteString(truncationMarker)
result.WriteByte('\n')
}

for i := range linesInBuffer {
idx := (startIndex + i) % maxJobLogLines
if validLines[idx] {
result = append(result, lines[idx])
for i := range count {
if i > 0 {
result.WriteByte('\n')
}
result.WriteString(lines[(first+i)%maxJobLogLines])
}

return strings.Join(result, "\n"), totalLines, httpResp, nil
return result.String(), totalLines, httpResp, nil
}
71 changes: 60 additions & 11 deletions pkg/buffer/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strings"
"testing"
"unicode/utf8"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -42,9 +43,8 @@ func TestProcessResponseAsRingBufferToEnd(t *testing.T) {
assert.Equal(t, "line3\nline4\nline5", result)
})

t.Run("handles very long line exceeding 10MB", func(t *testing.T) {
// Create a line that exceeds maxLineSize (10MB)
longLine := strings.Repeat("x", 11*1024*1024) // 11MB
t.Run("handles very long line exceeding limit", func(t *testing.T) {
longLine := strings.Repeat("x", 2*maxLineSize)
body := "line1\n" + longLine + "\nline3\n"
resp := &http.Response{
Body: io.NopCloser(strings.NewReader(body)),
Expand All @@ -60,9 +60,8 @@ func TestProcessResponseAsRingBufferToEnd(t *testing.T) {
assert.Contains(t, result, "TRUNCATED")
})

t.Run("handles line at exactly max size", func(t *testing.T) {
// Create a line just under maxLineSize
longLine := strings.Repeat("a", 1024*1024) // 1MB - should work fine
t.Run("preserves line below limit", func(t *testing.T) {
longLine := strings.Repeat("a", maxLineSize-1)
body := "start\n" + longLine + "\nend\n"
resp := &http.Response{
Body: io.NopCloser(strings.NewReader(body)),
Expand All @@ -76,6 +75,8 @@ func TestProcessResponseAsRingBufferToEnd(t *testing.T) {
assert.Equal(t, 3, totalLines)
assert.Contains(t, result, "start")
assert.Contains(t, result, "end")
assert.Contains(t, result, longLine)
assert.NotContains(t, result, "TRUNCATED")
})

t.Run("ring buffer with long line in middle of many lines", func(t *testing.T) {
Expand All @@ -85,8 +86,7 @@ func TestProcessResponseAsRingBufferToEnd(t *testing.T) {
for i := 1; i <= 10; i++ {
fmt.Fprintf(&sb, "line%d\n", i)
}
// Insert an 11MB line (exceeds maxLineSize of 10MB)
longLine := strings.Repeat("x", 11*1024*1024)
longLine := strings.Repeat("x", 2*maxLineSize)
sb.WriteString(longLine)
sb.WriteString("\n")
for i := 11; i <= 20; i++ {
Expand Down Expand Up @@ -132,8 +132,7 @@ func TestProcessResponseAsRingBufferToEnd(t *testing.T) {
})

t.Run("line at exactly maxLineSize boundary", func(t *testing.T) {
// Create a line at exactly maxLineSize (10MB) - should be truncated
exactLine := strings.Repeat("z", 10*1024*1024)
exactLine := strings.Repeat("z", maxLineSize)
body := "before\n" + exactLine + "\nafter\n"
resp := &http.Response{
Body: io.NopCloser(strings.NewReader(body)),
Expand All @@ -152,7 +151,7 @@ func TestProcessResponseAsRingBufferToEnd(t *testing.T) {

t.Run("ring buffer keeps truncated line when in last N", func(t *testing.T) {
// Long line followed by only 2 more lines, with ring buffer size 5
longLine := strings.Repeat("y", 11*1024*1024)
longLine := strings.Repeat("y", 2*maxLineSize)
body := "line1\nline2\nline3\n" + longLine + "\nlineA\nlineB\n"
resp := &http.Response{
Body: io.NopCloser(strings.NewReader(body)),
Expand All @@ -174,3 +173,53 @@ func TestProcessResponseAsRingBufferToEnd(t *testing.T) {
assert.NotContains(t, result, "line1")
})
}

func TestLogBufferBoundsContentBytes(t *testing.T) {
for _, lineBytes := range []int{8 * 1024, 1 << 20} {
t.Run(fmt.Sprint(lineBytes), func(t *testing.T) {
// Smaller lines exercise total-byte eviction; larger ones exercise
// per-line truncation as well as eviction and ring wraparound.
const count = 200
line := strings.Repeat("x", lineBytes) + "\n"
body := io.MultiReader(io.LimitReader(&repeatingLogReader{line: line}, int64(len(line)*count)), strings.NewReader("last line\n"))
resp := &http.Response{Body: io.NopCloser(body)}
result, total, respOut, err := ProcessResponseAsRingBufferToEnd(resp, 150)
if respOut != nil && respOut.Body != nil {
defer respOut.Body.Close()
}
require.NoError(t, err)
assert.Equal(t, count+1, total)
assert.LessOrEqual(t, len(result), MaxLogBytes)
assert.True(t, strings.HasPrefix(result, truncationMarker+"\n"))
assert.True(t, strings.HasSuffix(result, "last line"))
})
}
}

// Stream a large input without retaining the whole fixture in memory.
type repeatingLogReader struct {
line string
pos int
}

func (r *repeatingLogReader) Read(p []byte) (int, error) {
n := copy(p, r.line[r.pos:])
r.pos = (r.pos + n) % len(r.line)
return n, nil
}

func TestLogBufferTruncationPreservesUTF8(t *testing.T) {
body := "before\n" + strings.Repeat("界", maxLineSize) + "\nafter\n"
resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))}
result, count, respOut, err := ProcessResponseAsRingBufferToEnd(resp, 3)
if respOut != nil && respOut.Body != nil {
defer respOut.Body.Close()
}
require.NoError(t, err)
assert.Equal(t, 3, count)
assert.True(t, utf8.ValidString(result))
assert.Contains(t, result, truncationMarker)
assert.True(t, strings.HasPrefix(result, "before\n"))
assert.True(t, strings.HasSuffix(result, "\nafter"))
assert.LessOrEqual(t, len(strings.Split(result, "\n")[1]), maxLineSize+len(truncationMarker))
}
Loading