Repository navigation
FIX: Make ShellCommandOutputScorer destructive-shell regex linear time - #3702
Open
Muhammad Waqar Anwar (WaqarAnwar) wants to merge 1 commit into
Open
Muhammad Waqar Anwar (WaqarAnwar) wants to merge 1 commit into
Muhammad Waqar Anwar (WaqarAnwar) wants to merge 1 commit into
Conversation
The "Destructive Shell" pattern let any repeated flag serve as the recursive/force flag, so long nonmatching input such as `rm -rf -rf ... relative` backtracked quadratically (and cubically when `rm -rf` itself repeats), blocking the event loop during async scoring. Anchor the rm branch at the start of a command segment and commit, via atomic groups, to the first `rm` and first flag before searching for a protected target. Detection is unchanged; add long nonmatching-input regression tests. Fixes microsoft#3698
This branch has not been deployed
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.
Description
Fixes #3698.
The
Destructive Shellpattern inShellCommandOutputScorer(added in #3597) allowed any repeated flag to act as the recursive/force flag, with unbounded argument repetition on both sides. On long nonmatching input the engine retried every split. BecauseRegexScorerrunspattern.searchsynchronously inside its async method, this also blocked the event loop.It is worse than quadratic when
rm -rfitself repeats, since eachrmrestarts the search:"rm " + "-rf " * 1000 + "relative"(issue repro)"rm -rf " * 500 + "relative""rm -rf " * 1000 + "relative""rm -rf " * 8000 + "relative"Fix
The
rmbranch now:;,|,&,>,<),rmand the first recursive/force flag after it,/other than/tmp,~,$HOME,${HOME}).If any
rm … flag … targetsequence exists in a segment, then the firstrmfollowed by its first flag also precedes that target. So the result is the same, but each segment is scanned in linear time. The flag must now be a whole token ((?=\s)). This is what the old pattern effectively required too, and committing to the first flag would otherwise break on tokens like-rf.x.Separator behavior is unchanged (
rm -rf build; cd /varstill does not match).Tests and Documentation
rm, flags, targets, separators andrm-like paths, plus every existing test case. They returned the same result on every input.sudo rm -rf /,/bin/rm -rf /,rm -v -rf /etc,rm -rf build /etc,rm -rf a/rm /,rm -rf.x -rf /,cd /app && rm -rf ~.rm -rf.x /,rm / -rf.test_destructive_shell_long_nonmatching_input_is_fast: six long nonmatching inputs (repeated flags, repeatedrm, repeatedrm -rf,rm-suffixed paths, a single huge flag token). Each must finish in under 0.5 s and scoreFalsethrough the scorer.pytest tests/unit/score/regex/: 1225 passed.ruff checkandruff format --checkare clean.No documentation changes needed.