Skip to content

fix(terminal): prevent safety filter false positives on keywords inside quoted strings#20066

Open
kshitijk4poor wants to merge 1 commit into
NousResearch:mainfrom
kshitijk4poor:fix/terminal-safety-filter-false-positives
Open

fix(terminal): prevent safety filter false positives on keywords inside quoted strings#20066
kshitijk4poor wants to merge 1 commit into
NousResearch:mainfrom
kshitijk4poor:fix/terminal-safety-filter-false-positives

Conversation

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Problem

The terminal tool's safety filter blocks legitimate commands when keywords like nohup, disown, or setsid appear inside quoted strings — e.g., in commit messages, Python -c code, gh pr create --body text, or grep patterns.

Examples that get incorrectly blocked:

python3 -c "x = 'preexec_fn=os.setsid'"
git commit -m "fix: replace preexec_fn=os.setsid with process_group=0"
gh pr create --body "We removed os.setsid..."
grep -r 'nohup' tests/

Root Cause

_foreground_background_guidance() in tools/terminal_tool.py uses:

_SHELL_LEVEL_BACKGROUND_RE = re.compile(r"\b(?:nohup|disown|setsid)\b", re.IGNORECASE)

This matches the keyword anywhere in the full command text, including inside quoted strings.

Fix

Two-layer approach:

  1. _strip_quotes() helper — Removes single-quoted, double-quoted, and backtick-quoted content before pattern matching, so keywords inside strings are invisible to the regexes.

  2. Position-aware regex — Tightened to only match keywords at shell command positions (after ^, ;, &&, ||, $() rather than at any word boundary.

Both layers are needed: quote stripping handles the common case (keywords in string literals), and the position-aware regex handles unquoted edge cases.

Testing

  • All 14 test_terminal_tool.py tests pass
  • All 45 test_terminal_compound_background.py + test_terminal_foreground_timeout_cap.py tests pass
  • Verified 7 false-positive cases now pass (quoted keywords)
  • Verified 5 true-positive cases still blocked (actual background wrapper usage)

Fixes #20064

…de quoted strings

The _foreground_background_guidance() function matched background-wrapper
keywords (nohup/disown/setsid) anywhere in the command text, including
inside quoted strings, Python -c code, commit messages, and PR body text.

Two-layer fix:
1. Strip single-quoted, double-quoted, and backtick-quoted content before
   pattern matching via _strip_quotes() helper.
2. Tighten the regex to only match keywords at command-start positions
   (after ^, ;, &, &&, ||, or $() — not mid-argument.

Both layers are needed: quote stripping handles the common case of keywords
in string literals, and the position-aware regex handles unquoted cases
like 'export FOO=setsid' (word boundary match, wrong position).

Fixes NousResearch#20064
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists tool/terminal Terminal execution and process management labels May 5, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Fixes #20064 — same root cause (naive regex in _foreground_background_guidance()).

1 similar comment
@alt-glitch

Copy link
Copy Markdown
Collaborator

Fixes #20064 — same root cause (naive regex in _foreground_background_guidance()).

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

Labels

P2 Medium — degraded but workaround exists tool/terminal Terminal execution and process management type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(terminal): safety filter false-positives on background wrapper keywords inside quoted strings

2 participants