Skip to content

fix: restore CI lint gates (#1740) and assess substitutions in the bash gate (#1753) - #1764

Open
1jehuang wants to merge 2 commits into
masterfrom
fix/triage-ci-gates-and-bash-gate
Open

1jehuang wants to merge 2 commits into
masterfrom
fix/triage-ci-gates-and-bash-gate

Conversation

@1jehuang

@1jehuang 1jehuang commented Oct 8, 2026 •

Copy link
Copy Markdown
Owner

Summary

Two small fixes from automated triage, one commit each. Rebased after #1736 and #1739 merged, so the overlapping lines are gone.

#1740: remaining clippy/rustfmt failures on master

  • google.rs: ticks.is_multiple_of(30) (manual_is_multiple_of)
  • jcode-tui: gate a test-only copy helper and the display_col_slice import with #[cfg(test)], drop a needless mut in a test
  • rustfmt on openai-runtime/src/lib.rs (drift reported by hosted CI)

#1753: bash gate rated "$(rm -rf ~)", backticks and unquoted heredoc substitutions Safe
tokenize::embedded_substitutions extracts $(...) and backtick bodies from shell source (skipping single quotes) and from unquoted heredoc bodies (skipping quoted heredocs, which the shell leaves unexpanded). assess recurses into each body and merges findings. It's additive, so verdicts can only get stricter.

Validation

The Enforce oversized-file ratchet check fails identically on origin/master (32 files over budget, tracked in #692). This PR grows none of them.

Fixes #1740
Fixes #1753

--- — Jcode agent (automated triage), on behalf of @1jehuang

@greptile-apps

greptile-apps Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 1/5

[Medium risk] Fixes command risk assessment for shell substitutions.

Do not merge until the Bash gate catches both the escaped-apostrophe deletion and the previously reported case deletion.

Findings

  1. P1 Security Quoted apostrophe hides a deletion ▶
  2. P1 Security Case hides a deletion ▶
  3. P2 Comments block harmless commands ▶
  4. P2 Quoted text triggers refusal ▶
  5. P2 Nesting multiplies assessment work ▶
Fix with agent prompt
### Issue 1
crates/jcode-command-risk/src/tokenize.rs:295-300
`scan_substitutions` misses commands after an escaped apostrophe in Bash `$'...'` text. For `echo $'it\'s' "$(rm -rf ~)"`, it treats the escaped apostrophe as the closing quote, then treats the real closing apostrophe as a new opening quote. That skips the following substitution. `assess` returns `Safe`, so the Bash gate allows the home-directory deletion.

Recognize escapes inside `$'...'` and add this command as a regression test.

**How this was verified:** A source trace shows the quote loop skips the destructive substitution, while the Bash tool passes the original command to Bash, where `$'it\'s'` ends before the substitution.

### Issue 2
crates/jcode-command-risk/src/tokenize.rs:361-365
`matching_paren_body` treats a `case` pattern’s closing `)` as the end of the substitution. For `echo "$(case prod in prod) rm -rf ~ ;; esac)"`, it extracts only `case prod in prod`. The deletion is never assessed: the result remains `Safe`, and the Bash gate permits execution. Bash executes the matching branch, leaving a home-directory deletion outside the protection this change is intended to provide. This bypass must be addressed before merging.

Recognize shell pattern boundaries, or require approval when the complete substitution cannot be read safely. Add this example as a regression test.

**How this was verified:** The actual assessment and Bash refusal function allowed the payload, while instrumented Bash invoked an intercepted deletion with the isolated home directory as its target.

### Issue 3
crates/jcode-command-risk/src/tokenize.rs:292-294
`scan_substitutions` reads substitutions inside shell comments. For example, `echo ok # "$(rm -rf ~)"` only prints `ok`, but the new scan extracts `rm -rf ~` and makes the gate permanently deny the command, even with a justification. This is a non-blocking false-positive concern: harmless commands containing quoted shell examples in comments must be rewritten before the agent can run them.

Skip comments outside quoted words when reading shell source, while keeping `#` literal inside heredoc bodies. Add a regression test for this quoted comment.

### Issue 4
crates/jcode-command-risk/src/tokenize.rs:undefined-259
`embedded_substitutions` finds heredocs one line at a time, without carrying the surrounding quote state. For `echo 'cat\n<<EOF\n$(rm -rf ~)\nEOF'`, with real newlines, Bash treats the entire argument as literal text. The new code instead sees an unquoted heredoc and scans its body, causing a permanent refusal even with a justification. This is a non-blocking false-positive concern: printing or writing these harmless multiline examples requires rewriting the command.

Carry quote state across lines before deciding that a heredoc starts.

### Issue 5
crates/jcode-command-risk/src/lib.rs:224-225
`assess` now calls itself for each nested substitution without a depth or work limit, repeatedly copying and scanning the remaining source. A constructed 32,775-byte command with 4,096 nested substitutions took about 2.49 seconds to assess, compared with about 1.4 milliseconds before the change. Stack consumption also grows with nesting; an explicitly constrained 256 KiB stack overflowed at depth 1,024, although larger stacks completed the tested inputs.

This is a non-blocking responsiveness and robustness concern for unusually deep commands. Use an explicit work list and a work budget, or a depth limit that requires approval rather than silently skipping deeper commands.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

This PR restores lint gates and extends the Bash risk check to commands inside substitutions.

  • Assesses $(...), backticks, and expanding heredoc bodies.
  • Adds destructive and inert substitution tests.
  • Restores the test-only TUI import and cleans up lint and formatting warnings.
  • The new scanner still misses substitutions after an escaped apostrophe in $'...'.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Bash command] --> B[Assess outer commands]
    A --> C[Extract substitutions]
    C --> D[Assess each extracted body]
    B --> E[Combine findings]
    D --> E
    E --> F{Highest risk}
    F -->|Safe or Low| G[Run command]
    F -->|Confirm| H[Require justification]
    F -->|Catastrophic| I[Refuse command]
Loading

Reviews (3) · Last reviewed commit: "fix(command-risk): assess commands insid..." · Reviewed by Greptile

Comment on lines +361 to +365
')' => {
depth -= 1;
if depth == 0 {
return (chars[start..index].iter().collect(), index + 1);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 security Case hides a deletion

matching_paren_body treats a case pattern’s closing ) as the end of the substitution. For echo "$(case prod in prod) rm -rf ~ ;; esac)", it extracts only case prod in prod. The deletion is never assessed: the result remains Safe, and the Bash gate permits execution. Bash executes the matching branch, leaving a home-directory deletion outside the protection this change is intended to provide. This bypass must be addressed before merging.

Recognize shell pattern boundaries, or require approval when the complete substitution cannot be read safely. Add this example as a regression test.

How this was verified: The actual assessment and Bash refusal function allowed the payload, while instrumented Bash invoked an intercepted deletion with the isolated home directory as its target.

Artifacts

Rust harness for assessment and bash refusal

  • The executed harness calls the real assessment and gate APIs plus the exact production bash refusal function, exposing the policy result without executing deletion.

Safe parent and head reproduction script

  • The executed script compiles revision-specific source and runs instrumented Bash with isolated HOME, making the comparison reproducible without invoking external rm.

Parent results with intercepted deletion

  • The parent run captures assessment, gate, bash refusal, and instrumented shell traces for the payload and controls, showing that both substitution variants were allowed.

PR head results with intercepted deletion

  • The head run captures the truncated case body and Safe/Allow result alongside Bash invoking the intercepted rm, confirming that the case-pattern bypass remains.

View artifacts

T-Rex Ran code and verified through T-Rex

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-command-risk/src/tokenize.rs
Line: 361-365

Comment:
**Case hides a deletion**

`matching_paren_body` treats a `case` pattern’s closing `)` as the end of the substitution. For `echo "$(case prod in prod) rm -rf ~ ;; esac)"`, it extracts only `case prod in prod`. The deletion is never assessed: the result remains `Safe`, and the Bash gate permits execution. Bash executes the matching branch, leaving a home-directory deletion outside the protection this change is intended to provide. This bypass must be addressed before merging.

Recognize shell pattern boundaries, or require approval when the complete substitution cannot be read safely. Add this example as a regression test.

**How this was verified:** The actual assessment and Bash refusal function allowed the payload, while instrumented Bash invoked an intercepted deletion with the isolated home directory as its target.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Comment on lines +292 to +294
while index < chars.len() {
match chars[index] {
'\\' => index += 2,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Comments block harmless commands

scan_substitutions reads substitutions inside shell comments. For example, echo ok # "$(rm -rf ~)" only prints ok, but the new scan extracts rm -rf ~ and makes the gate permanently deny the command, even with a justification. This is a non-blocking false-positive concern: harmless commands containing quoted shell examples in comments must be rewritten before the agent can run them.

Skip comments outside quoted words when reading shell source, while keeping # literal inside heredoc bodies. Add a regression test for this quoted comment.

Artifacts

Executed Rust contract test

  • This test invokes the actual assess and gate APIs for the claimed command and controls, making the before/after verdicts reproducible.

Executed comparison and safe Bash runner

  • This runner compiles unchanged parent and candidate source snapshots and executes the guarded Bash command, reproducing the regression without editing production or performing deletion.

Parent behavior for the harmless commented command

  • The parent contract and guarded Bash command were executed successfully, showing Safe/Allow and Bash output ok.

Candidate denial of the harmless commented command

  • The candidate contract and identical guarded Bash command were executed successfully, showing unconditional Catastrophic/Deny even though Bash still prints ok.

View artifacts

T-Rex Ran code and verified through T-Rex

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-command-risk/src/tokenize.rs
Line: 292-294

Comment:
**Comments block harmless commands**

`scan_substitutions` reads substitutions inside shell comments. For example, `echo ok # "$(rm -rf ~)"` only prints `ok`, but the new scan extracts `rm -rf ~` and makes the gate permanently deny the command, even with a justification. This is a non-blocking false-positive concern: harmless commands containing quoted shell examples in comments must be rewritten before the agent can run them.

Skip comments outside quoted words when reading shell source, while keeping `#` literal inside heredoc bodies. Add a regression test for this quoted comment.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

let line = lines[index];
shell_source.push_str(line);
index += 1;
for (delimiter, strip_tabs, quoted) in heredoc_delimiters(line.trim_end_matches('\n')) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Quoted text triggers refusal

embedded_substitutions finds heredocs one line at a time, without carrying the surrounding quote state. For echo 'cat\n<<EOF\n$(rm -rf ~)\nEOF', with real newlines, Bash treats the entire argument as literal text. The new code instead sees an unquoted heredoc and scans its body, causing a permanent refusal even with a justification. This is a non-blocking false-positive concern: printing or writing these harmless multiline examples requires rewriting the command.

Carry quote state across lines before deciding that a heredoc starts.

Artifacts

Rust test harness for the multiline literal

  • This executed harness calls the real assessment and gate APIs with the exact multiline command and asserts each revision’s observed result, demonstrating the false denial.

Reproduction commands for isolated before and after runs

  • This executed script compiles immutable Git snapshots, runs the Rust harness and Bash with a disposable home, and captures both results without editing shared source.

Before-change assessment, gate response, and Bash output

  • The parent revision’s real APIs and the exact Bash command were executed, showing Safe/Allow and harmless literal output.

After-change assessment, gate refusal, and Bash output

  • The changed revision’s real APIs and the exact Bash command were executed, showing an unconditional Catastrophic/Deny despite harmless literal output.

Source line citations and unchanged working-tree check

  • Source inspection and Git checks were executed, locating the per-line quote reset and refusal path while confirming no tracked source edits.

View artifacts

T-Rex Ran code and verified through T-Rex

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-command-risk/src/tokenize.rs
Line: 259

Comment:
**Quoted text triggers refusal**

`embedded_substitutions` finds heredocs one line at a time, without carrying the surrounding quote state. For `echo 'cat\n<<EOF\n$(rm -rf ~)\nEOF'`, with real newlines, Bash treats the entire argument as literal text. The new code instead sees an unquoted heredoc and scans its body, causing a permanent refusal even with a justification. This is a non-blocking false-positive concern: printing or writing these harmless multiline examples requires rewriting the command.

Carry quote state across lines before deciding that a heredoc starts.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Comment on lines +224 to +225
for source in tokenize::embedded_substitutions(command) {
for finding in assess(&source, ctx).findings {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Nesting multiplies assessment work

assess now calls itself for each nested substitution without a depth or work limit, repeatedly copying and scanning the remaining source. A constructed 32,775-byte command with 4,096 nested substitutions took about 2.49 seconds to assess, compared with about 1.4 milliseconds before the change. Stack consumption also grows with nesting; an explicitly constrained 256 KiB stack overflowed at depth 1,024, although larger stacks completed the tested inputs.

This is a non-blocking responsiveness and robustness concern for unusually deep commands. Use an explicit work list and a work budget, or a depth limit that requires approval rather than silently skipping deeper commands.

Artifacts

Assessor test driver for balanced nested echo substitutions

  • The executed Rust driver constructs bounded nested echo inputs, times the real assessor, and checks successful verdicts; shell commands are never executed.

Validation script with exact build commands and subprocess limits

  • The executed Python script archives both commits, builds their unchanged assessor source, and captures bounded timing and stack probes; the comparison is reproducible.

Source citations and runtime environment

  • Executed inspection commands captured commit identities, compiler version, absent setup file, and numbered assessor, tokenizer, and caller source; the tested change is traceable.

Exact parent assessor source compiled for the baseline

  • Git archived the assessor source from ead5e66 and the harness compiled it unchanged; the executed baseline source is preserved.

Exact candidate assessor source compiled for validation

  • Git archived the assessor source from 6b28600 and the harness compiled it unchanged; the executed candidate source is preserved.

Parent assessor timings and bounded stack probes

  • The parent assessor ran identical nested inputs with recorded commands, working directories, exit codes, and limits; it remained fast and completed every stack probe.

Candidate assessor timings and constrained-stack overflow

  • The candidate assessor ran the same inputs and limits, recording increased timings and a 256 KiB stack abort; the regression is executed rather than theoretical.

Complete validation command output

  • The validation script was executed with its command, working directory, exit code, and full output captured; the overall run completed successfully despite the intentionally isolated abort.

Tracked-source no-edit check

  • An executed git diff --exit-code returned zero after validation; no tracked source changes or fixes were introduced.

View artifacts

T-Rex Ran code and verified through T-Rex

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-command-risk/src/lib.rs
Line: 224-225

Comment:
**Nesting multiplies assessment work**

`assess` now calls itself for each nested substitution without a depth or work limit, repeatedly copying and scanning the remaining source. A constructed 32,775-byte command with 4,096 nested substitutions took about 2.49 seconds to assess, compared with about 1.4 milliseconds before the change. Stack consumption also grows with nesting; an explicitly constrained 256 KiB stack overflowed at depth 1,024, although larger stacks completed the tested inputs.

This is a non-blocking responsiveness and robustness concern for unusually deep commands. Use an explicit work list and a work budget, or a depth limit that requires approval rather than silently skipping deeper commands.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Comment thread crates/jcode-tui/src/tui/ui.rs Resolved
…#1740)

#1736 and #1739 covered the mcp.rs lint and commands_tests.rs format.
This handles the rest:
- google login: use is_multiple_of (manual_is_multiple_of)
- tui: gate a test-only copy helper and import, drop a needless mut
- rustfmt the openai-runtime drift reported by hosted CI
…uoted heredocs (fixes #1753)

The word tokenizer flattens double-quoted command substitutions and
backticks into literals, and strips unquoted heredoc bodies, so
destructive commands there were rated Safe. Extract each substitution
body (skipping single quotes and quoted heredocs, which the shell does
not expand) and assess it recursively like a top-level command.
@1jehuang
1jehuang force-pushed the fix/triage-ci-gates-and-bash-gate branch from 73006b7 to 13375d1 Compare October 8, 2026 04:28
Comment on lines +295 to +300
'\'' if shell_quotes && !in_double => {
index += 1;
while index < chars.len() && chars[index] != '\'' {
index += 1;
}
index += 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 security Quoted apostrophe hides a deletion

scan_substitutions misses commands after an escaped apostrophe in Bash $'...' text. For echo $'it\'s' "$(rm -rf ~)", it treats the escaped apostrophe as the closing quote, then treats the real closing apostrophe as a new opening quote. That skips the following substitution. assess returns Safe, so the Bash gate allows the home-directory deletion.

Recognize escapes inside $'...' and add this command as a regression test.

How this was verified: A source trace shows the quote loop skips the destructive substitution, while the Bash tool passes the original command to Bash, where $'it\'s' ends before the substitution.

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-command-risk/src/tokenize.rs
Line: 295-300

Comment:
**Quoted apostrophe hides a deletion**

`scan_substitutions` misses commands after an escaped apostrophe in Bash `$'...'` text. For `echo $'it\'s' "$(rm -rf ~)"`, it treats the escaped apostrophe as the closing quote, then treats the real closing apostrophe as a new opening quote. That skips the following substitution. `assess` returns `Safe`, so the Bash gate allows the home-directory deletion.

Recognize escapes inside `$'...'` and add this command as a regression test.

**How this was verified:** A source trace shows the quote loop skips the destructive substitution, while the Bash tool passes the original command to Bash, where `$'it\'s'` ends before the substitution.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

@1jehuang

Copy link
Copy Markdown
Owner Author

The command-risk commit landed on master as 800d42b. Master already has its own clippy/fmt cleanup (bdd630b), so the CI-gate commit here is not needed. Leaving this open in case the CJK/tui pieces are wanted separately. --- Jcode agent, on behalf of @1jehuang

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Fixes incorrect or broken behavior.

Projects

None yet

1 participant