Repository navigation
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Symptom
GET /api/v1/knowledge-bases/{kb_id}/chunks(and the legacyGET /api/chunk/list) build the SQL offset straight from the caller-suppliedpage/page_sizewith no bounds check. Out-of-range values are forwarded verbatim into the document store:?page=0sendsOFFSET -20, which SQLite treats as0, so page 0 returns page 1's rows while the response body still echoes"page": 0. Any client that keeps advancing until it sees its own page number back loops forever on the same rows.?page_size=-1sendsLIMIT -1, which SQLite interprets as "no limit", so a single request returns every chunk of the document instead of one page.?page_size=0sendsLIMIT 0and returns an empty page even whentotalis non-zero.Trigger input
Both routes parse the query with
_to_int(request.query_params.get("page"), 1), which only supplies a default when the value is missing or unparsable — it does not bound a supplied negative or zero value. They are reachable by any authenticated dashboard user / Open API key with the knowledge-base scope.Root cause
astrbot/dashboard/services/knowledge_base_service.py:785(before the fix):The sibling handlers in the same service validate their inputs first —
list_kbsatastrbot/dashboard/services/knowledge_base_service.py:324andlist_documentsatastrbot/dashboard/services/knowledge_base_service.py:506both runpage = max(page, 1)/page_size = max(page_size, 1)with the comment "Clamp page and page_size to at least 1 before calculating offsets/slices."list_chunkswas simply missing that guard, so the un-clamped pair is passed straight toDocumentStorage.get_documents()→ SQLAlchemy.limit(limit).offset(offset)(astrbot/core/db/vec_db/faiss_impl/document_storage.py:270-273).Fix
Apply the same clamp that its siblings already use, so
page/page_sizeare at least 1 before the offset is computed and before they are echoed back in the response. No API surface, schema, or route signature changes, so the generated dashboard API client is unaffected.Red (unmodified
origin/master)Green
Regression check
ruff format .andruff check .pass on the touched files. The change is pure Python logic and stays compatible with Python 3.10+.Prepared with an AI coding agent.
Summary by Sourcery
Prevent invalid knowledge-base chunk pagination parameters from producing incorrect or unbounded query results.
Bug Fixes:
Tests: