From c029b50ef928d5c21ffb1922f6d541b0d27bb7a4 Mon Sep 17 00:00:00 2001 From: yeliheng-010 <81519560+yeliheng-010@users.noreply.github.com> Date: Thu, 24 Sep 2026 12:34:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(rss):=20=E6=8B=92=E7=BB=9D=E8=B6=85?= =?UTF-8?q?=E9=99=90=E5=93=8D=E5=BA=94=E4=BB=A5=E9=81=BF=E5=85=8D=E6=AD=A3?= =?UTF-8?q?=E6=96=87=E9=9D=99=E9=BB=98=E6=88=AA=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/datasource/connector/rss/client.go | 5 +- .../datasource/connector/rss/client_test.go | 70 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 internal/datasource/connector/rss/client_test.go diff --git a/internal/datasource/connector/rss/client.go b/internal/datasource/connector/rss/client.go index f5042961..9b2610a2 100644 --- a/internal/datasource/connector/rss/client.go +++ b/internal/datasource/connector/rss/client.go @@ -84,10 +84,13 @@ func (c *client) fetch(ctx context.Context, rawURL string, maxSize int64, withAu return nil, fmt.Errorf("HTTP %d %s", resp.StatusCode, resp.Status) } - body, err := io.ReadAll(io.LimitReader(resp.Body, maxSize)) + body, err := io.ReadAll(io.LimitReader(resp.Body, maxSize+1)) if err != nil { return nil, fmt.Errorf("read body failed: %w", err) } + if int64(len(body)) > maxSize { + return nil, fmt.Errorf("response exceeds maximum size (%d bytes)", maxSize) + } return body, nil } diff --git a/internal/datasource/connector/rss/client_test.go b/internal/datasource/connector/rss/client_test.go new file mode 100644 index 00000000..9e9f5700 --- /dev/null +++ b/internal/datasource/connector/rss/client_test.go @@ -0,0 +1,70 @@ +package rss + +import ( + "context" + "io" + "net/http" + "net/http/httptest" + "strconv" + "strings" + "testing" + + "github.com/mmcdole/gofeed" +) + +func TestFetchResponseSizeLimit(t *testing.T) { + const limit = 16 + for _, chunked := range []bool{false, true} { + for _, size := range []int{limit - 1, limit, limit + 1} { + name := strconv.Itoa(size) + "/chunked=" + strconv.FormatBool(chunked) + t.Run(name, func(t *testing.T) { + body := strings.Repeat("x", size) + server := serveResponseBody(t, body, chunked) + got, err := newClient(nil).fetch(context.Background(), server.URL, limit, false) + if size > limit { + if err == nil { + t.Fatalf("expected oversized response error, got %d bytes and no error", len(got)) + } + if got != nil { + t.Fatal("oversized response must not return a truncated body") + } + return + } + if err != nil { + t.Fatalf("fetch within limit: %v", err) + } + if string(got) != body { + t.Fatalf("expected %q, got %q", body, got) + } + }) + } + } +} + +func TestResolveItemOversizedArticleUsesFeedContent(t *testing.T) { + const feedContent = "Complete feed fallback content" + body := "
" + longArticleBody + + strings.Repeat(" ", maxArticleSize) + "

Final paragraph

" + server := serveResponseBody(t, body, true) + item := &gofeed.Item{Title: "Article", Link: server.URL} + resolved := NewConnector().resolveItem(context.Background(), newClient(nil), + &gofeed.Feed{Title: "Feed"}, item, server.URL+"/feed", "item-1", feedContent) + if string(resolved.item.Content) != feedContent { + t.Fatalf("expected feed fallback %q, got %q", feedContent, resolved.item.Content) + } +} + +func serveResponseBody(t *testing.T, body string, chunked bool) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + if chunked { + w.(http.Flusher).Flush() + } else { + w.Header().Set("Content-Length", strconv.Itoa(len(body))) + } + _, _ = io.WriteString(w, body) + })) + t.Cleanup(server.Close) + return server +}