Skip to content

fix(core): bound find-replace output before allocation - #1923

Merged
phernandez merged 4 commits into
mainfrom
codex/security-find-replace-budget
Oct 28, 2026
Merged

phernandez merged 4 commits into
mainfrom
codex/security-find-replace-budget

Conversation

@phernandez

Copy link
Copy Markdown
Member

Why

find_replace allocates the expanded string before any output budget is checked. A bounded reproduction expanded a 4 KiB note and a 4 KiB replacement to 16 MiB. Slightly larger inputs exceed that output size. Fixes #1897.

What Changed

Cap the projected replacement result at 16 MiB of UTF-8 text and reject larger results before str.replace. This policy applies only to find_replace; attachment uploads and other note operations keep their existing behavior. Document the limit in bm-edit(3).

Implementation Details

After validating the exact nonoverlapping replacement count, compute the current UTF-8 byte length plus count times the replacement/find byte-length difference. This accounts for multibyte text and shrinking edits without constructing the expanded output. The 16 MiB budget matches the hosted proxy request-size budget; the user approved a limit specifically for find/replace.

Stacked on #1922, following #1921 and #1920. Only the replacement budget, manual update, and tests are new here.

Testing

  • Before the fix: three oversized ASCII/Unicode regressions failed because no refusal occurred.
  • uv run pytest tests/services/test_find_replace_budget.py tests/services/test_entity_service.py tests/services/test_note_section_edits.py -q --no-cov: 102 passed.
  • Allocation regressions verify rejected edits use less than 1 MiB of tracked allocation, excluding inputs, without mocks. Exact-limit results, multibyte find/replacement text, nonoverlapping matches, and shrinking oversized notes are covered.
  • A real service/filesystem/database regression verifies rejection preserves note bytes and stored checksum.
  • uv run pytest tests/mcp/test_tool_edit_note.py tests/api/v2/test_accepted_note_atomicity.py -q --no-cov: 81 passed.
  • just fast-check: passed, including repository type checking.
  • just doctor: passed sandboxed write/index/search self-test.
  • git diff --check: passed.

Risks / Follow-ups

Find/replace calls whose immediate replacement output exceeds 16 MiB are now refused in both local and hosted Core. The budget covers the replacement result, before later metadata/frontmatter preparation. This is an operation budget, not a general note or binary-file size limit. Cloud must update its Core dependency after merge to receive the guard.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T20:53:04.893212Z ce4a849 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Base automatically changed from codex/security-upload-symlinks to main October 28, 2026 22:18
@phernandez
phernandez merged commit 74e41b7 into main Oct 28, 2026
84 of 85 checks passed
@phernandez
phernandez deleted the codex/security-find-replace-budget branch October 28, 2026 22:18
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.

Check projected note replacement size before allocating output

1 participant