Skip to content

fix(rg): reject empty pattern with --only-matching - #1649

Merged
chaliy merged 1 commit into
mainfrom
2026-05-22-fix-rg-o-memory-amplification-vulnerability
May 24, 2026
Merged

chaliy merged 1 commit into
mainfrom
2026-05-22-fix-rg-o-memory-amplification-vulnerability

Conversation

@chaliy

@chaliy chaliy commented May 22, 2026

Copy link
Copy Markdown
Contributor

Motivation

  • Prevent resource-exhaustion triggered by rg -o/--only-matching with an empty pattern, which produces zero-width matches at every UTF-8 boundary and can amplify builtin output allocations before interpreter truncation.

Description

  • Add a parse-time check in RgOptions::parse (in crates/bashkit/src/builtins/rg.rs) to return Err(Error::Execution("rg: empty pattern is not allowed with --only-matching")) when --only-matching is enabled and any provided pattern is empty, and add a unit test test_rg_only_matching_rejects_empty_pattern to cover -o -e ''.

Testing

  • Ran cargo test -p bashkit rg_only_matching and the targeted tests including test_rg_only_matching_rejects_empty_pattern passed.

Codex Task

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented May 22, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bashkit 150a0ad Commit Preview URL May 23 2026, 09:15 AM

@chaliy
chaliy force-pushed the 2026-05-22-fix-rg-o-memory-amplification-vulnerability branch from d2d3a6b to a2f802a Compare May 22, 2026 09:09
Prevent resource-exhaustion triggered by rg -o/--only-matching with
an empty pattern, which produces zero-width matches at every UTF-8
boundary and can amplify builtin output allocations before interpreter
truncation.

Add a parse-time check in RgOptions::parse that returns
Error::Execution when --only-matching is enabled and any provided
pattern is empty.
@chaliy
chaliy force-pushed the 2026-05-22-fix-rg-o-memory-amplification-vulnerability branch from a2f802a to 150a0ad Compare May 23, 2026 09:15
@chaliy
chaliy merged commit 0e76b4c into main May 24, 2026
42 of 44 checks passed
@chaliy
chaliy deleted the 2026-05-22-fix-rg-o-memory-amplification-vulnerability branch May 24, 2026 09:06
chaliy added a commit that referenced this pull request May 30, 2026
### Motivation
- Prevent resource-exhaustion triggered by `rg -o/--only-matching` with
an empty pattern, which produces zero-width matches at every UTF-8
boundary and can amplify builtin output allocations before interpreter
truncation.

### Description
- Add a parse-time check in `RgOptions::parse` (in
`crates/bashkit/src/builtins/rg.rs`) to return
`Err(Error::Execution("rg: empty pattern is not allowed with
--only-matching"))` when `--only-matching` is enabled and any provided
pattern is empty, and add a unit test
`test_rg_only_matching_rejects_empty_pattern` to cover `-o -e ''`.

### Testing
- Ran `cargo test -p bashkit rg_only_matching` and the targeted tests
including `test_rg_only_matching_rejects_empty_pattern` passed.

------
[Codex
Task](https://chatgpt.com/codex/cloud/tasks/task_e_6a0fa7290f08832b97de581326fb2b30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant