Skip to content

Check projected note replacement size before allocating output #1897

Closed

Description

@phernandez

Problem

Follow-up from the reviewed CodeRabbit deep scan (K14). This ticket tracks one distinct fix; duplicate scan rows are consolidated here.

Reviewed evidence and scope

  • K14 (scan ID 584b9599-5f70-459e-a8de-cd497a8434c3, review A, Medium): Replacement string materializes before checking output size. Predict resulting bytes and reject before allocation.
    Source at current baseline: src/basic_memory/services/note_preparation.py:829.

Fix and validation

  • Check projected note replacement size before allocating output.
  • Recheck current callers and supported behavior before choosing the smallest complete fix.
  • Add a bounded regression at the affected public or service boundary; demonstrate it fails without the fix.
  • Preserve legitimate inputs and tenant/project boundaries. Run focused tests plus repository static/type checks.

The scan/review establishes the code-level concern described above; it does not establish production exploitation. Conditional branches and local/tooling-only exposure must retain that scope.

Activity

  1. added
    security
    on Oct 28, 2026
  2. claude commented on Oct 28, 2026

    @claude
    Contributor

    Claude finished @phernandez's task in 26s —— View job


    Triage

    • Type: Bug (hardening gap), labeled bug
    • Priority: Medium. This is a security-labeled resource-exhaustion concern, not demonstrated production exploitation. The issue itself says the scan does not establish that.
    • Component: MCP tools / services (edit_note find_replace path). Not Cloud.
    • Complexity: Simple

    Findings: apply_edit_operation in src/basic_memory/services/note_preparation.py (~line 829) runs current_content.replace(find_text, content) after counting occurrences. str.replace builds the whole result before any size check, so a large content with a high expected_replacements can allocate a very large string first.

    The occurrence count is already known by then, so the output size can be predicted before replacing:
    len(current_content) + actual_count * (len(content) - len(find_text))

    Rejecting when that exceeds the note-size limit would avoid the allocation. The fix should:

    • Reuse the existing max-note-size limit if there is one, rather than add a new one.
    • Raise a ValueError like the neighboring branches.
    • Keep legitimate edits under the limit working.
    • Add a regression test at the edit_note or service boundary that fails without the check.
    • Check whether append, prepend and replace_section need the same guard. The issue names only the replacement path.

    Duplicates: A search turned up no existing issue on this.

    I've done triage only and made no code changes.

  3. added
    bug
    on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsecurity

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions