Skip to content

fix(backend): clamp MCP SSE get_conversations limit and offset - #8270

Closed
ZachL111 wants to merge 2 commits into
BasedHardware:mainfrom
ZachL111:fix/mcp-sse-conversations-clamp
Closed

ZachL111 wants to merge 2 commits into
BasedHardware:mainfrom
ZachL111:fix/mcp-sse-conversations-clamp

Conversation

@ZachL111

@ZachL111 ZachL111 commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The MCP SSE server's get_conversations tool reads limit and offset straight from the tool arguments and passes them to Firestore with no bounds checking. A negative offset (for example -5) reaches the Firestore query and returns HTTP 500, taking down the whole tool call, and a non-integer limit does the same. Every other tool branch in this dispatcher already clamps these values through parse_mcp_int, so this branch was the odd one out. This PR routes limit and offset through parse_mcp_int with the same min/max bounds the sibling branches use. This is a proactive hardening change; there is no filed GitHub issue for it.

Root cause

In backend/routers/mcp_sse.py, the get_conversations branch of execute_tool reads the two values raw:

limit = arguments.get("limit", 20)
offset = arguments.get("offset", 0)

and hands them straight to the DB:

conversations = conversations_db.get_conversations(
    user_id,
    limit,
    offset,
    include_discarded=False,
    statuses=["completed"],
    ...
)

A negative offset is forwarded to the Firestore query and blows up there, surfacing as a 500. A limit that is not an integer (a string, a list) gets the same treatment. The get_memories branch a few lines up already guards both arguments with parse_mcp_int(arguments.get("limit"), "limit", default=100, minimum=1, maximum=500), so the clamp existed in the file. it just was not applied here.

Fix

Parse both values through parse_mcp_int, matching the sibling branches, and turn a bad value into a clean JSON-RPC invalid-params error instead of a 500:

try:
    limit = parse_mcp_int(arguments.get("limit"), "limit", default=20, minimum=1, maximum=500)
    offset = parse_mcp_int(arguments.get("offset"), "offset", default=0, minimum=0, maximum=100000)
except ValueError as e:
    raise ToolExecutionError(str(e), code=-32602)

parse_mcp_int clamps with max(minimum, min(parsed, maximum)), so a negative offset becomes 0 and an over-large value is capped before it can reach Firestore. Valid in-range arguments produce the same limit/offset as before, so the DB call and the response are unchanged for normal input.

Testing

Added backend/tests/unit/test_mcp_sse_conversations_clamp.py, registered in backend/test.sh. routers/mcp_sse.py has a heavy import graph, so the test follows the stubbing pattern from test_mcp_data_endpoints.py: it installs auto-mock modules for the database, Firebase, and Google Cloud dependencies under a stub finder, then imports routers.mcp_sse and calls execute_tool directly. With conversations_db patched, the cases assert the values actually handed to get_conversations: a negative offset clamps to 0, a negative limit clamps to 1, a huge offset clamps to 100000, a huge limit clamps to 500, and a non-integer limit raises ToolExecutionError with code -32602 rather than reaching the DB. Verified red to green: the test fails on current main and passes with this change.

PR status check

No open PR changes this logic. The clamp added here does not appear anywhere in this file's history, so it is not a previously reverted fix being resubmitted. No filed GitHub issue matches.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 3 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Tip: cubic could auto-approve low-risk PRs like this, if it thinks it's safe to merge. Learn more

Re-trigger cubic

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Backend hardening bug fix with regression test; matches established pattern. (auto-review)

@ZachL111
ZachL111 force-pushed the fix/mcp-sse-conversations-clamp branch from 3d3994e to 1ff794a Compare June 24, 2026 18:41
@Git-on-my-level

Copy link
Copy Markdown
Member

Hi @ZachL111 — thank you for finding this and writing up a candidate fix.

We reviewed it during the ZachL111 aggregate triage. This one needs a bit more product/API/design decision than we want to bundle into the current aggregate PRs (#8362, #8364, #8366, #8367), so we’re deferring rather than merging this exact patch.

I’m closing the individual PR to keep the review queue manageable. The report is still appreciated and may be revisited as a more explicit follow-up. Thanks again for the contribution.

@Git-on-my-level

Copy link
Copy Markdown
Member

Closing after aggregate triage: deferring this design/API decision rather than merging the one-off patch. Thanks @ZachL111 for surfacing it.

@github-actions

Copy link
Copy Markdown
Contributor

Hey @ZachL111 👋

Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request.

After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:

  • Project standards — Ensuring consistency across the codebase
  • User needs — Making sure changes align with what our users need
  • Code best practices — Maintaining code quality and maintainability
  • Project direction — Keeping aligned with our roadmap and vision

Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out.

Thank you for being part of the Omi community! 💜

Git-on-my-level added a commit that referenced this pull request Jun 25, 2026
## Summary

This aggregates the high-signal request/protocol-boundary fixes from
@ZachL111 into shared validation helpers instead of endpoint-by-endpoint
patches.

Changes:
- add `utils.request_validation` for multipart JSON form parsing,
bounded query params, real calendar dates, UTC-safe sync filename
timestamps, and image chunk envelopes
- route app/persona multipart JSON parsing through the shared helper
- enforce bounded list/history query parameters and real calendar dates
at request boundaries
- validate sync upload filename timestamps with one UTC-aware parser for
v1/v2 paths
- reject invalid websocket image chunk envelopes/sequences instead of
silently ignoring malformed chunks
- validate parallel event/action-item status arrays on the Pydantic
request models
- replace a previous `sys.meta_path`/MagicMock-heavy app JSON test with
direct helper contract coverage

Supersedes/redesigns parts of #8218, #8219, #8220, #8222, #8227, #8231,
#8260, #8263, #8275, #8278, #8279, #8280, #8295, #8303, #8309, #8321.

Original issue discovery, reproductions, and candidate fixes by
@ZachL111 — thank you for surfacing these boundary bugs. This PR keeps
the useful findings while moving the implementation toward shared
contracts and smaller tests.

## Deferred from the Oracle boundary bucket

Not included here because they need separate design or would broaden
this PR:
- #8224 shared-chat malformed record behavior
- #8240 notification payload model cleanup
- #8244 task due-date validation
- #8257 app generate-description request validation
- #8266/#8270 MCP SSE JSON-RPC validation
- #8271 segment assignment bounds beyond the shared-array model work
- #8289 social/persona not-found policy

## Test plan

- [x] `pytest tests/unit/test_request_validation_contracts.py
tests/unit/test_sync_file_paths_filename_none.py
tests/unit/test_apps_create_app_json.py
tests/unit/test_conversation_events_bounds.py -q` → 34 passed
- [x] `python -m py_compile ...` over modified modules/tests
- [x] `git diff --check`
- [x] `bash backend/testing/e2e/run.sh -q --tb=short` → 73 passed, 6
skipped


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/BasedHardware/omi/pull/8367?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants