Repository navigation
fix(repo): repo security reads repeated rm -r, git -C push chains, long flag words, curl, pip install and path parts in linear time - #1353
Conversation
…tel#688) The destructive-delete rule started its scan at every `rm` and read the words after the recursive flag up to the end of the segment for a target, so a line of repeated `rm -r` or `rm -rf` was read once per `rm` (0.64 s for 3000 repeats of `rm -rf `). The rule now reads from the first `rm` of each segment that has a recursive flag, and takes the first recursive flag of that `rm` only: a later `rm` or flag of the same segment finds its target only where that one does. A flag word that holds a `;`, `&` or `|` ends the segment inside the flags, so a second branch reads those chains from each `rm` as before. The lines the rule matches are the same, which a table and a random-lines comparison against the old pattern pin. The pattern moves to `repos/security_patterns.py` with the constants it uses (`BLANKS`, `SYSTEM_DIRS`, `CLEANUP_PARTS`): `repos/security.py` is at its length cap. The tests of the linear scans are in a new file for the same reason: `tests/test_linear_scan_sites.py` is near its cap. The `git -c` scans of history-rewrite and hook-bypass (intel#1270) are already linear, as is the secret-read scan of a line of repeated `a/ ` words: growth tests keep them so. This commit does not change those rules. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
…n linear time (intel#1267) In `git -C push -C push ...` the option `-C` may take `push` as its argument or leave it as the subcommand, so the opener had one end at every `push` of the chain and the rest of the segment was scanned for the closer from each end: a chain of `-C push`, `-C reset` or `-C commit` was quadratic in history-rewrite, the push branch and the commit branch of hook-bypass. The opener now takes `-C` as a word of its own before it takes the next word as its argument, and is cut at its first end. A later end finds its closers only where the first does, because the closers read the same segment (or line) to its end, so the lines the rules match are the same. The issue's first option, refusing the subcommand's name as the argument of `-C`, changes results: `git -C push\npush -f` (`-C` takes `push`, the second `push` is on the next line) stops matching in history-rewrite, so it is not taken. The push branch's opener is read on one line only: an opener with a newline in its whitespace is the across-lines branch's, which ends on a later line. A table of lines (including `git -C push push -f`, `git -C reset reset --hard`, `git -C commit commit --no-verify` and the newline variants) and a random-lines comparison against the old patterns pin the results; growth tests pin the chains. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
…inear time (intel#1268) The force flag of history-rewrite, `\s-[a-z]*f[a-z]*\b`, tried every split of a long letter word around its `f` when the word ends in a character that is no word boundary (`git push -fff...f1`), which is quadratic in the word. The flag is now `\s-(?=[a-z]*f)[a-z]++\b`: a lookahead finds the `f` once and the letters are taken possessively. The word can only end at the end of its letters, since a `\b` after an earlier letter never holds, so the possessive form gives the same hit as the old flag on `-fff1`, `-ff-x`, `-f_` and the other words of the table, and on random words of letters, digits, `_`, `-` and non-ASCII letters. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
…n linear time (intel#1269) Both rules started at every opener word (`curl`, `wget`, `pip install`) and read the rest of the segment for their tail, so a line of repeated openers with no tail was read once per opener. pipe-to-shell now starts at the first download of each segment (up to `|`, `;` or `&`): a later download reaches the same `|`, so it finds the tail only where the first does. unpinned-install starts at the first `pip install` followed by a blank, after the start of its segment (a newline, `;`, `&`, `|`) or after an option word such as `-i=` that no word can follow (a later `pip install` reads the words after it again, so that point is a start of its own). The first `pip install` after such a point reads the same words as a later one and finds the same tail. Starting at the first opener of the segment alone would have lost `pip install -i= pip install https://x`. The lines the rules match are the same: tables and random-lines comparisons against the old patterns pin them, and growth tests pin the repeated openers. The anchors are in `repos/security_patterns.py`, with `PIP_URL_OPTS` moved there from `repos/security.py`. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
…time (intel#688) The `secret-read` rule started its `/\S*?/.name` scan at every `/` of a word, so a word of repeated path parts (`a/a/a/...`, `~/~/...`, `/etc/etc/...`) was read again to its end from each `/`: quadratic in the length of the word. The intel#1270 `a/ ` timing used separate words and did not show it. The scan now starts at the word's start and takes its first `/` only (`SECRET_WORD_START`, possessive over the characters before it): the first `/` of a word reaches every `/.name` that a later `/` does, so the lines it matches are the same. A table of words and a random-lines comparison against the old pattern pin that, and growth tests cover `a/`, `~/` and `/etc` repeated in one word. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
|
🔍 OpenCodeReview found 3 issue(s) in this PR. CI at `53e9ace`: 1 failing, 24 passing, 4 skipped
By label: 1 should fix, 2 not shown by a run
Review details
|
| _rx(r"(?:^|[\n;&|]|" + PIP_STUCK + r")(?:(?!" + PIP_INSTALL + r")(?!" + PIP_STUCK + r")[^\n;&|])*" + PIP_INSTALL | ||
| + r"(?:" + BLANKS + r"(?:(?:" + PIP_URL_OPTS + r")(?:" + BLANKS | ||
| + r"|=)[^\s;&|]+|(?!(?:" + PIP_URL_OPTS + r")(?:[\s=;&|]|$))[^\s;&|]+))*?" | ||
| + BLANKS + r"(?:[^\s;&|]*git\+|[\"']?https?://|--break-system-packages\b)")), |
There was a problem hiding this comment.
Impact: repeated installs can still make this synchronous security scan take quadratic work, leaving #1269 incompletely fixed.
Trigger: RULES['unpinned-install'].pattern.search('pip install -i -i= ' * n).
Evidence: the option/value branch consumes -i -i= as one pair, so each opener reads the entire remaining suffix without finding a target. Nevertheless, every -i= also satisfies the new PIP_STUCK restart anchor, which retries from the next pip install; the suffix lengths sum to Θ(n²). The existing growth cases cover pip install -i= but not this paired-option shape.
Workaround: cap command-line length pending a fix. Make restarts account for options consumed as values (or scan/tokenize each segment once), and add this shape to the growth tests.
Should fix: A run shows it, and the code before this change fails the same way.
Shown by running: exit 1 (at the head) · exit 1 (at the merge base) · exit 0 (at the head with the fix)
python proof_c1.py
What the proof does: Times the unpinned-install pattern on 'pip install -i -i= ' * n at n and 16n and fails if the CPU-time ratio reaches the growth limit.
The problem shows as the output QUADRATIC_GROWTH.
Script: dokimi/proof_c1.py (17 lines).
ratio 260.2140768174964
QUADRATIC_GROWTH ratio 260.2
| + _BLANKS + r"(?:[^\s;&|]*git\+|[\"']?https?://|--break-system-packages\b)")), | ||
| _rx(r"(?:^|[\n;&|]|" + PIP_STUCK + r")(?:(?!" + PIP_INSTALL + r")(?!" + PIP_STUCK + r")[^\n;&|])*" + PIP_INSTALL | ||
| + r"(?:" + BLANKS + r"(?:(?:" + PIP_URL_OPTS + r")(?:" + BLANKS | ||
| + r"|=)[^\s;&|]+|(?!(?:" + PIP_URL_OPTS + r")(?:[\s=;&|]|$))[^\s;&|]+))*?" |
There was a problem hiding this comment.
Impact: repeated installs still cause quadratic work in the synchronous security scan, leaving #1269 incompletely fixed.
Trigger: RULES['unpinned-install'].pattern.search('pip install -i -i= ' * n).
Evidence: this option/value branch consumes -i -i= together, letting each opener scan the entire remaining suffix without a target. Every -i= also satisfies the new PIP_STUCK anchor, retrying from the next opener; the suffix lengths sum to Θ(n²). Existing growth cases omit this paired-option shape.
Workaround: cap command-line length pending correction. Make restarts account for options consumed as values, or scan each segment once, and add this input to the growth tests.
Not shown by a run: The proof step did not check this finding.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1353 +/- ##
==========================================
- Coverage 96.19% 96.18% -0.01%
==========================================
Files 499 500 +1
Lines 66465 66476 +11
Branches 11542 11542
==========================================
+ Hits 63933 63941 +8
Misses 1563 1563
- Partials 969 972 +3
🚀 New features to boost your workflow:
|
Summary
Four open issues about the command rules of
dokimi repo security(dokimi/dokimi/repos/security.py): a line of one repeated keyword takes quadratic time, because each repeat starts a scan that reads the rest of the segment (or of the word). Measured as the CPU time at 16n over that at n (n=150,tests/growth_timing.py; linear is about 16, the limit of the tests 64):mainRULES['destructive-delete']on"rm -r " * n"rm -rf " * n(0.64 s for 3000 repeats)RULES['secret-read']on"a/" * n(one word)"~/" * n"/etc" * nRULES['history-rewrite']on"git " + "-C push " * n"git " + "-C reset " * n_PUSH_NO_VERIFYon"git " + "-C push " * n_COMMIT_NO_VERIFYon"git " + "-C commit " * nRULES['history-rewrite']on"git push -" + "f" * n + "1"RULES['pipe-to-shell']on"curl x " * n"wget x " * nRULES['unpinned-install']on"pip install " * nThe
git -cscan of history-rewrite and hook-bypass (16.2 and 16.0) and the secret-read scan of a line of repeateda/words (15.7), two parts of #688, were already linear onmain(#1270). This PR adds a growth test for each so they stay so.What each fix does, and the option taken where an issue lists several:
rm) and repo security: pipe-to-shell and unpinned-install are quadratic on a line of repeatedcurl,wgetorpip install#1269 (the first opener of a segment): a later opener of a segment finds its tail only where the first one does, so the scan starts at one place per segment. The place is the first opener that can start a match, not the firstrmorpipword:rm a rm -rf /andpip install-x pip install https://xmatch onmain, and a plain first-opener anchor would lose them. Forrmit is the firstrmwith a recursive flag, taken once; a flag word that holds a;,&or|reads on into the next segment, so a second branch reads those chains from eachrm. Forpip installthe scan also restarts after an option word-i=that no word can follow (pip install -i= pip install https://xmatches onmain).secret-read, a word of repeated path parts): the first/of a word reaches every/.namethat a later/of the word does, so the lazy scan starts at the word's start and takes the first/only (SECRET_WORD_START, insecurity_patterns.py). Of the two ways, this one and testing the start of the/.nameword in code, the pattern was kept: it stays one regex, and a table of words (a/.ssh,a/b/.aws/z,~/.ssh,$HOME/.aws,/.ssh,a//.env,.awsome,/a/.ssh.dand more) and a random-lines comparison with a copy of the old pattern show the same hits.git -C push,-C resetor-C commitis quadratic in history-rewrite, the push branch and the commit branch of hook-bypass #1267: of the two options, the closer is tried from the first end of the opener only. The first option, refusing the subcommand's name as the argument of-C, changes results:git -C push\npush -f(-Ctakespush, the secondpushis on the next line) stops matching in history-rewrite. The opener takes-Cas a word of its own before it takes the next word as its argument, so its first end is the firstpush, and a later end finds its closers only where the first does. A table pinsgit -C push push -f,git -C reset reset --hardandgit -C commit commit --no-verify.-fff...f1aftergit push#1268: of the two options,\s-(?=[a-z]*f)[a-z]++\b. A\bafter an earlier letter of the word never holds, so the possessive word gives the same hit as\s-[a-z]*f[a-z]*\bon-fff1,-ff-x,-f_and every other word of the table, and on random words of letters, digits,_,-and non-ASCII letters.Closes #688
Closes #1267
Closes #1268
Closes #1269
The same quadratic scans in other rules, found while searching for sites with these causes, are tracked in #1349 (history-rewrite and hook-bypass on
gitfollowed by a newline and a chain of-C push), #1350 (destructive-delete on a long flag word), #1351 (destructive-delete on a chain of words that end inrm) and #1352 (pipe-to-shell on a long path after the pipe).Changes
dokimi/dokimi/repos/security_patterns.py(new): the pattern ofdestructive-delete(RM_RECURSIVE), the anchors ofpipe-to-shell(DOWNLOAD) andunpinned-install(PIP_INSTALL,PIP_STUCK) and the start of a path inside a word forsecret-read(SECRET_WORD_START), with the constants they use (BLANKS,SYSTEM_DIRS,CLEANUP_PARTS,PIP_URL_OPTS) moved fromsecurity.py.security.pywas at 799 of its 800 lines and the growth ratchet only shrinks.security.py:pipe-to-shellstarts at the first download of each segment,unpinned-installat the firstpip installafter the start of its segment or after an option word-i=.security.py: the opener of history-rewrite and of the commit and push branches of hook-bypass is lazy, takes-Cas a word first, and is atomic, so it is cut at its first end (_first_in_segment,_COMMIT_OPT_LOCAL_WORD_FIRST,_GIT_PUSH). The push branch's opener is read on one line: an opener with a newline in its whitespace is the across-lines branch's.security.py: the force flag of history-rewrite (_PUSH_FORCE) takes the flag word possessively after a lookahead for thef.security.py:secret-readstarts its path scan at the first/of a word (SECRET_WORD_START).dokimi/tests/test_security_linear_scans.py(new): for each rule a table of lines and a random-lines comparison against a copy of the old pattern, and growth tests. It is a new file becausetests/test_linear_scan_sites.pywould pass the 1000-line cap; it importsCASESand_security_searchfrom there. No existing test changed.secret-readpart of repo security: keyword scans for rm -rf, git -c and a/ are quadratic in repeats on one line #688.Risk
None for what the rules match: each pattern finds the same lines as before, pinned by the tables and by random-lines comparisons with a copy of the old pattern. The scan only asks whether a pattern matches (
security.py:344), so the span of a match, which now starts at the segment (or at the word, forsecret-read), is not used. The patterns use atomic groups and possessive quantifiers, which need Python 3.11, the minimum the package requires.Testing
On Linux, at
53e9ace4(onmain1673824):make ci-local: 38 passed, 0 failed, 15 skipped (the docker and network catalog smoke, the pinned Claude Code fidelity run, jobs that run on a schedule or not on a pull request, the other CI matrix legs, conda-forge, the fuzz run, semgrep). 30341 tests passed, 23 skipped; changed-line coverage 100% (18 lines); mutation of changed lines 36 of 36 killed.main's code the first version of the new test file gave 19 failed, 169 passed: each growth test failed its limit (the first,curl xrepeated:assert 201.2 < 64); the tables and the random comparisons passed, as they compare a pattern with its own copy. Thesecret-readtests added since (40 selected) give 6 failed, 34 passed on the code before their commit: the six growth tests (assert 235.2 < 64and the like).Open questions
None.
Revert
A plain revert restores the quadratic scans and the old position of the patterns in
security.py. Nothing is stored.Checklist
pytestpassesruff,mypy, and schema checks passAGENTS.mdraise/exit and test names the doc, contract or issue it traces to.github/.licenserc.yaml)