Skip to content

fix(policy): scan path-qualified interpreters at command positions - #403

Open
sahilkhedkar wants to merge 6 commits into
solongate:mainfrom
sahilkhedkar:fix/281-interpreter-paths
Open

sahilkhedkar wants to merge 6 commits into
solongate:mainfrom
sahilkhedkar:fix/281-interpreter-paths

Conversation

@sahilkhedkar

@sahilkhedkar sahilkhedkar commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Path-qualified interpreters such as /bin/bash deploy.sh and ./node run.js did not match the anchored interpreter regex. Their referenced scripts were omitted from BuildPolicyInput, allowing content-dependent command and path deny rules to miss those scripts while equivalent bare-interpreter invocations were blocked.

Interpreter matching now uses the executable basename and discovers invocations at shell command positions. Ordinary arguments, comments, quoted command-like text, redirection targets, and literal heredoc contents cannot consume the three-file scan budget. For example, echo /bin/bash a.sh ./node b.sh /usr/bin/python3 c.sh; /bin/bash deploy.sh now scans deploy.sh rather than the three decoy files.

Common execution wrappers (env, sudo, command, exec, and nohup) are recognized with known option arities, so an option value cannot masquerade as an interpreter. Static quoted and escaped paths are resolved without evaluating substitutions or accessing the agent's environment. Existing file-count and file-size limits remain in place, and missing or oversized files are skipped without consuming the successful-read budget. README documents the scope and limitations of this static inspection.

Dependency justification: this adds mvdan.cc/sh/v3/syntax v3.14.1 to the policy decision path. A shell syntax parser is needed to distinguish command positions from arguments and shell data, including quoted separators, comments, redirections, and heredocs; splitting tokens or delimiters with regexes would retain ways to exhaust the scan budget. Only syntax parsing is used; no shell interpreter is executed. The guard's module metadata is updated for the transitive dependency.

Regression coverage includes:

  • Absolute and relative interpreter paths, flags, source, and ..
  • Command and path deny enforcement, including the three-decoy argument attack, while safe scripts remain allowed.
  • Quoted text, comments, redirections, heredocs, and wrapper option values that must not exhaust the scan limit.
  • Pipelines, conditionals, subshells, newlines, command substitutions, quoted/escaped filenames, and common execution wrappers.
  • Missing files, files exceeding 64 KiB, and the exact 64 KiB boundary after invalid candidates.

Validation:

  • The original path-qualified regression and the review's scan-budget regression failed before their respective fixes and passed afterward.
  • cd packages/policy && go vet ./... passed.
  • cd packages/policy && go test -count=1 -race ./... passed.
  • cd packages/guard && go vet ./... passed.
  • cd packages/guard && go test -count=1 ./... passed outside the sandbox, since the shield tests require a local HTTP listener.
  • Changed Go files are formatted; git diff --check passed.
  • The repository-wide ./scripts/sg-unit-check.sh previously stopped at pre-existing formatting issues in unchanged packages/guard source files.

Verification exercised Go policy APIs with temporary scripts that were read, never executed. No live agent-client or sandbox end-to-end test was performed. This remains bounded static inspection, with the documented limitations around dynamic words, unsupported wrapper options, and unparseable shell syntax.

Closes #281

Signed-off-by: Sahil <sahilkhedkar11@gmail.com>
Signed-off-by: Sahil <sahilkhedkar11@gmail.com>
Signed-off-by: Sahil <sahilkhedkar11@gmail.com>
Signed-off-by: Sahil <sahilkhedkar11@gmail.com>
Signed-off-by: Sahil <sahilkhedkar11@gmail.com>
@codacy-production

codacy-production Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 71 complexity · 4 duplication

Metric Results
Complexity 71
Duplication 4

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

⚠️ Resolve issues before merging

A high-severity logic issue remains in interpreter detection: arbitrary command arguments can be mistaken for interpreter paths, exhaust the three-file scan limit, and cause an actual referenced script to bypass policy scanning. This should be fixed before merging. Regression coverage should also include missing and oversized referenced files.

Test suggestions

  • Referenced scripts are discovered when the interpreter is an absolute or relative path.
  • Interpreter flags are skipped before locating the referenced script.
  • The source and dot interpreter forms discover their referenced scripts.
  • Command deny rules block content found in scripts invoked through path-qualified interpreters.
  • Path deny rules block paths found in scripts invoked through path-qualified interpreters.
  • Safe referenced scripts remain allowed.
  • Missing or oversized referenced files are ignored.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Missing or oversized referenced files are ignored.

TIP Add review instructions in .codacy/instructions/review.md, or an AGENTS.md, so the reviewer knows your conventions.
TIP How was this review? Give us feedback

Comment thread packages/policy/extract_paths.go Outdated
}
for i := 0; i < len(toks)-1; i++ {
if !reInterpreter.MatchString(toks[i]) {
if !reInterpreter.MatchString(filepath.Base(toks[i])) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 HIGH RISK

Restrict interpreter detection to command positions rather than arbitrary arguments. Otherwise, path arguments can consume the three-file scan limit and allow a later referenced script to bypass policy scanning. Add a regression test demonstrating that non-interpreter path arguments do not exhaust the limit.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

Signed-off-by: Sahil <sahilkhedkar11@gmail.com>
@sahilkhedkar sahilkhedkar changed the title fix(policy): enforce script scanning for path-qualified interpreters fix(policy): scan path-qualified interpreters at command positions Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

policy: ReadReferencedFiles' interpreter match misses path-qualified binaries

1 participant