From 26ff9888238de05db115784724e9abaca05b48ac Mon Sep 17 00:00:00 2001 From: linus-liu-web <395141741@qq.com> Date: Sat, 17 Oct 2026 23:17:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(notion):=20=E9=80=92=E5=BD=92=E5=AD=90?= =?UTF-8?q?=E5=9D=97=E5=88=86=E9=A1=B5=E8=A1=A5=E9=87=8D=E5=A4=8D=E6=B8=B8?= =?UTF-8?q?=E6=A0=87=E4=B8=8E=E8=B7=B3=E6=95=B0=E4=B8=8A=E9=99=90=EF=BC=8C?= =?UTF-8?q?=E5=85=9C=E5=BA=95=E4=B8=8D=E5=86=8D=E8=A2=AB=E7=A9=BA=E9=A1=B5?= =?UTF-8?q?=E7=BB=95=E8=BF=87=20(#4411)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../datasource/connector/notion/client.go | 12 ++- .../connector/notion/pagination_guard_test.go | 76 +++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/internal/datasource/connector/notion/client.go b/internal/datasource/connector/notion/client.go index 86c16819..11a72038 100644 --- a/internal/datasource/connector/notion/client.go +++ b/internal/datasource/connector/notion/client.go @@ -333,8 +333,9 @@ func (c *notionClient) GetBlockChildrenAll(ctx context.Context, blockID string) func (c *notionClient) getBlockChildrenRecursive(ctx context.Context, blockID string, depth int) ([]notionBlock, error) { var allBlocks []notionBlock var startCursor string + seenCursors := make(map[string]struct{}) - for { + for page := 1; ; page++ { path := fmt.Sprintf("/v1/blocks/%s/children", blockID) if startCursor != "" { path += "?start_cursor=" + startCursor @@ -376,7 +377,14 @@ func (c *notionClient) getBlockChildrenRecursive(ctx context.Context, blockID st if len(allBlocks) >= maxBlocksPerPage || !resp.HasMore || resp.NextCursor == "" { break } - startCursor = resp.NextCursor + // maxBlocksPerPage counts appended blocks, so an empty page carrying + // has_more=true never reaches it. Guard the cursor itself as well: a + // repeated one would otherwise be requested forever. + next, err := advancePaginationCursor(ctx, seenCursors, resp.NextCursor, page) + if err != nil { + return nil, fmt.Errorf("get block children for %s: %w", blockID, err) + } + startCursor = next } if depth >= maxBlockDepth { diff --git a/internal/datasource/connector/notion/pagination_guard_test.go b/internal/datasource/connector/notion/pagination_guard_test.go index a2d5d5c8..729234fb 100644 --- a/internal/datasource/connector/notion/pagination_guard_test.go +++ b/internal/datasource/connector/notion/pagination_guard_test.go @@ -148,6 +148,82 @@ func TestGetBlockChildrenFlatRejectsRepeatedCursor(t *testing.T) { require.Contains(t, err.Error(), `repeated next_cursor "same-cursor"`) } +// GetBlockChildrenAll is the recursive sibling of GetBlockChildrenFlat and +// carried the same unbounded cursor loop. Its own maxBlocksPerPage fallback +// counts blocks already appended, so a vendor answering empty results with +// has_more=true keeps that counter at zero forever and the loop never exits. +func TestGetBlockChildrenAllRejectsRepeatedCursor(t *testing.T) { + var requests atomic.Int64 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + if requests.Add(1) > fakeLoopBudget { + w.WriteHeader(http.StatusBadRequest) + return + } + writeListPage(w, `[]`, true, "same-cursor") + })) + defer server.Close() + + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + _, err := paginationTestClient(t, server).GetBlockChildrenAll(ctx, "block-1") + require.Error(t, err) + require.Equal(t, int64(2), requests.Load(), + "the guard must reject the repeated cursor on the second page; err=%v", err) + require.Contains(t, err.Error(), `repeated next_cursor "same-cursor"`) +} + +// A vendor that mints a fresh cursor for every empty page never repeats one, so +// only the hop cap can end the recursive walk. +func TestGetBlockChildrenAllStopsAtPaginationCap(t *testing.T) { + var requests atomic.Int64 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + n := requests.Add(1) + if n > fakeLoopBudget { + w.WriteHeader(http.StatusBadRequest) + return + } + writeListPage(w, `[]`, true, fmt.Sprintf("cursor-%d", n)) + })) + defer server.Close() + + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + defer cancel() + + _, err := paginationTestClient(t, server).GetBlockChildrenAll(ctx, "block-1") + require.Error(t, err) + require.Contains(t, err.Error(), "pagination exceeded 10000 pages") + require.Equal(t, int64(paginationHopCap), requests.Load()) +} + +// The recursive walk must still merge a genuinely multi-page listing. +func TestGetBlockChildrenAllMergesRealPages(t *testing.T) { + var requests atomic.Int64 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests.Add(1) + if r.URL.Query().Get("start_cursor") == "" { + writeListPage(w, `[{"id":"block-1","type":"paragraph",`+ + `"paragraph":{"rich_text":[{"type":"text","text":{"content":"one"},"plain_text":"one"}]}}]`, + true, "cursor-2") + return + } + writeListPage(w, `[{"id":"block-2","type":"paragraph",`+ + `"paragraph":{"rich_text":[{"type":"text","text":{"content":"two"},"plain_text":"two"}]}}]`, + false, "") + })) + defer server.Close() + + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + blocks, err := paginationTestClient(t, server).GetBlockChildrenAll(ctx, "block-1") + require.NoError(t, err) + require.Len(t, blocks, 2) + require.Equal(t, "block-1", blocks[0].ID) + require.Equal(t, "block-2", blocks[1].ID) + require.Equal(t, int64(2), requests.Load()) +} + // Data source queries page through queryDataSourceWindow since #3845, so the // repeated-cursor guard has to cover that loop too. func TestQueryDataSourceAllRejectsRepeatedCursor(t *testing.T) {