fix(rg): cap replacement expansion output#1650
Open
chaliy wants to merge 1 commit into
Open
Conversation
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
bashkit | 51ae404 | Commit Preview URL | May 22 2026, 09:16 AM |
The rg builtin applied attacker-controlled --replace text with replace_all(...).into_owned() which can allocate output proportional to replacement_len * match_count, enabling memory amplification and OOM before interpreter stdout truncation. Add RG_MAX_REPLACEMENT_OUTPUT_BYTES and safe_replace_all/safe_replace_first helpers that project replacement size and return an Error::Execution if the projected output exceeds the cap. Route replacement formatting through the safe helpers by changing format_rg_line, format_rg_output_line, write_rg_context, and format_rg_multiline_replacement to return Result and updating all call sites to propagate errors instead of allocating first. Apply the same cap to single-match replace sites in only-matching paths to prevent the same amplification via -o.
c2abb6d to
51ae404
Compare
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.
Motivation
rgbuiltin applied attacker-controlled--replacetext withreplace_all(...).into_owned()which can allocate output proportional toreplacement_len * match_count, enabling memory amplification and OOM before interpreter stdout truncation.rgbehavior for normal cases by rejecting oversized expansions early.Description
RG_MAX_REPLACEMENT_OUTPUT_BYTESand asafe_replace_all(...) -> Result<String>helper that projects replacement size and returns anError::Executionif the projected output exceeds the cap.safe_replace_allby changingformat_rg_lineto returnResult<String>and makingwrite_rg_contextreturnResult<()>, and update all call sites (passthru,only_matching, context, and normal output paths) to propagate errors instead of allocating first.rg_replace_rejects_oversized_expansionthat constructs a scenario where replacement expansion would exceed the cap and asserts the builtin rejects it with anrg: replacement output exceedserror.Testing
cargo fmton the modified file (crates/bashkit/src/builtins/rg/mod.rs). — OK.cargo test -p bashkit rg_replace_rejects_oversized_expansion -- --nocapture, and the test passed.rgtests and observed all run tests passing locally.Codex Task