ollama - ✅(Solved) Fix Bash tool falsely blocked by denyPatterns when using --experimental [1 pull requests, 1 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
ollama/ollama#14766Fetched 2026-04-08 00:31:52
View on GitHub
Comments
1
Participants
1
Timeline
5
Reactions
0
Participants
Timeline (top)
cross-referenced ×2commented ×1labeled ×1referenced ×1

Fix Action

Fixed

PR fix notes

PR #14781: x/agent: fix false positive deny pattern matching in heredocs

Description (problem / solution / changelog)

The IsDenied function uses naive substring matching against the full command string, which causes false positives when heredoc bodies contain substrings matching deny patterns (e.g. su matching inside words like "result" or "ensure").

Two changes to fix this:

  • Strip heredoc bodies before checking deny patterns
  • Require command-like patterns (su, nc, sudo, etc.) to appear at a shell command position rather than anywhere in the string

Fixes #14766

Changed files

  • x/agent/approval.go (modified, +130/-6)
  • x/agent/approval_test.go (modified, +54/-1)

Code Example

blocked: Bash: cat > foobar.py << 'EOF'
#!/usr/bin/env python3
\"\"\"A Foo Bar python code\"\"\"
import ...
matches dangerous pattern: su
RAW_BUFFERClick to expand / collapse

What is the issue?

When Ollama is used with the --experimental flag, it seems that when the bash tool is called and a long piece of code is written, if the code contains any stuffs or pattern that includes one of the strings in the denyPatterns, the command gets blocked like this:

blocked: Bash: cat > foobar.py << 'EOF'
#!/usr/bin/env python3
\"\"\"A Foo Bar python code\"\"\"
import ...
matches dangerous pattern: su

The problem is located in func IsDenied located in file x/agent/approval.go

In this case, the substring su is detected as a dangerous pattern, even though it is not being used for privilege escalation or related purposes, but only appears as part of another word or variable. This leads to false positives that incorrectly block legitimate commands.

extent analysis

Fix Plan

To address the issue of false positives in the IsDenied function, we need to modify the pattern matching logic to consider the context of the matched string.

Step-by-Step Solution

  1. Modify the IsDenied function in x/agent/approval.go to use a more sophisticated pattern matching approach, such as using regular expressions with word boundaries.
  2. Update the denyPatterns to include word boundaries, for example, change "su" to "\bsu\b" to match only whole words.
  3. Implement a whitelist for common false positives, such as variable names or function names that contain the denied patterns.

Example Code

import (
	regexp
)

// ...

func IsDenied(code string, denyPatterns []string) bool {
	for _, pattern := range denyPatterns {
		// Use word boundaries to match whole words only
		re := regexp.MustCompile(`\b` + regexp.QuoteMeta(pattern) + `\b`)
		if re.FindString(code) != "" {
			return true
		}
	}
	return false
}

// ...

denyPatterns := []string{"\bsu\b", "\bsudo\b"} // updated patterns with word boundaries

Verification

To verify the fix, test the IsDenied function with various code snippets that previously triggered false positives, and ensure that only legitimate matches are blocked.

Extra Tips

  • Consider using a more advanced pattern matching library or framework to improve the accuracy of the IsDenied function.
  • Regularly review and update the denyPatterns list to ensure it remains effective and minimizes false positives.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

ollama - ✅(Solved) Fix Bash tool falsely blocked by denyPatterns when using --experimental [1 pull requests, 1 comments, 1 participants]