Skip to content

fix(api): cap memory message limit and top_n at REST_API_MAX_PAGE_SIZE - #15376

Merged
yuzhichang merged 1 commit into
infiniflow:mainfrom
pekar9781:fix/memory-messages-limit-cap
Jul 6, 2026
Merged

yuzhichang merged 1 commit into
infiniflow:mainfrom
pekar9781:fix/memory-messages-limit-cap

Conversation

@pekar9781

@pekar9781 pekar9781 commented May 28, 2026 •

Copy link
Copy Markdown

Related issues

Closes #15375

What problem does this PR solve?

GET /api/v1/messages and GET /api/v1/messages/search accepted unbounded limit / top_n query parameters while other REST list endpoints enforce REST_API_MAX_PAGE_SIZE (100) via validate_rest_api_page_size(). Oversized values can trigger expensive memory index queries and large result sets (DoS risk).

Type of change

  • Bug Fix (non-breaking change which fixes an issue)

Changes

File Change
api/apps/restful_apis/memory_api.py Cap limit and top_n with validate_rest_api_page_size; return argument error when exceeded
test/testcases/test_web_api/test_message_app/test_message_routes_unit.py Regression tests for oversized limit / top_n

Test plan

  • Unit tests added
  • pytest test/testcases/test_web_api/test_message_app/test_message_routes_unit.py

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. 🐖api The modified files are located under directory 'api/apps/sdk' 🐞 bug Something isn't working, pull request that fix bug. 🧪 test Pull requests that update test cases. labels May 28, 2026
@coderabbitai

coderabbitai Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Memory API endpoints now enforce maximum bounds on limit and top_n parameters by calling validate_rest_api_page_size() and catching ValueError, returning argument errors when values exceed 100 items. New unit tests verify this validation behavior.

Changes

Parameter Bounds Validation

Layer / File(s) Summary
Parameter validation in endpoints
api/apps/restful_apis/memory_api.py
search_message and get_messages endpoints wrap top_n and limit parsing in try/except ValueError, using validate_rest_api_page_size() to enforce a maximum of 100 items and return formatted argument error responses.
Validation tests
test/testcases/test_web_api/test_message_app/test_message_routes_unit.py
Unit tests assert that get_messages and search_message reject parameter values exceeding 100, expecting RetCode.ARGUMENT_ERROR with appropriate error messages.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit hops with validation tight,
No million-message DoS in sight!
With bounds now capped at hundred high,
The API rests as clients comply. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: capping memory message limit and top_n parameters at REST_API_MAX_PAGE_SIZE.
Linked Issues check ✅ Passed The PR fully addresses issue #15375 by capping limit/top_n at REST_API_MAX_PAGE_SIZE (100) with validation and argument error returns [#15375].
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the unbounded limit/top_n parameters in memory message APIs and adding corresponding regression tests.
Description check ✅ Passed The pull request description is comprehensive and well-structured, covering the problem, type of change, detailed changes, and test plan.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
api/apps/restful_apis/memory_api.py (1)

264-267: ⚡ Quick win

Add error logging in the new ValueError branches.

Line 266 and Line 298 return argument errors directly but skip logging, which makes these new rejection paths less observable in production.

Proposed patch
     try:
         top_n = validate_rest_api_page_size(int(args.get("top_n", 5)))
     except ValueError as exc:
+        logging.warning("Invalid top_n in /messages/search: %s", exc)
         return get_error_argument_result(str(exc))
@@
     try:
         limit = validate_rest_api_page_size(int(args.get("limit", 10)))
     except ValueError as exc:
+        logging.warning("Invalid limit in /messages: %s", exc)
         return get_error_argument_result(str(exc))

As per coding guidelines, "**/*.py: Add logging for new flows".

Also applies to: 296-299

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@api/apps/restful_apis/memory_api.py` around lines 264 - 267, The ValueError
branches that return get_error_argument_result(...) in the top_n
parsing/validation (validate_rest_api_page_size called with
int(args.get("top_n", 5))) and the similar branch at the later block must log
the error before returning; update those catch blocks to call the module logger
(e.g., logger.error or logger.exception) with a clear message including the
exception details (or use exc_info=True) and context (argument name and value)
and then return get_error_argument_result(str(exc)) so the rejection is
observable in production.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@api/apps/restful_apis/memory_api.py`:
- Around line 264-267: The ValueError branches that return
get_error_argument_result(...) in the top_n parsing/validation
(validate_rest_api_page_size called with int(args.get("top_n", 5))) and the
similar branch at the later block must log the error before returning; update
those catch blocks to call the module logger (e.g., logger.error or
logger.exception) with a clear message including the exception details (or use
exc_info=True) and context (argument name and value) and then return
get_error_argument_result(str(exc)) so the rejection is observable in
production.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 48c6c6f0-63ba-474e-bdda-438b5dd60c8a

📥 Commits

Reviewing files that changed from the base of the PR and between 7fc909a and 6008317.

📒 Files selected for processing (2)
  • api/apps/restful_apis/memory_api.py
  • test/testcases/test_web_api/test_message_app/test_message_routes_unit.py

@xugangqiang

Copy link
Copy Markdown
Contributor

@jony376
May I ask for your help to check if there's any other such places? If so, please list them and we can discuss how to fix them in one PR.

@xugangqiang xugangqiang added the ci Continue Integration label May 29, 2026
@xugangqiang

Copy link
Copy Markdown
Contributor

@JinHai-CN @yingfeng @yuzhichang Can any one of you help to trigger CI?

@yingfeng
yingfeng marked this pull request as draft May 29, 2026 09:27
@yingfeng
yingfeng marked this pull request as ready for review May 29, 2026 09:27
@codecov

codecov Bot commented May 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (7fc909a) to head (6008317).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15376      +/-   ##
==========================================
- Coverage   94.56%   93.16%   -1.40%     
==========================================
  Files          10       10              
  Lines         717      717              
  Branches      118      118              
==========================================
- Hits          678      668      -10     
- Misses         25       29       +4     
- Partials       14       20       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yuzhichang
yuzhichang merged commit aaade35 into infiniflow:main Jul 6, 2026
1 check passed
yuzhichang pushed a commit that referenced this pull request Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐖api The modified files are located under directory 'api/apps/sdk' 🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:S This PR changes 10-29 lines, ignoring generated files. 🧪 test Pull requests that update test cases.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants