Repository navigation
fix(policy): scan path-qualified interpreters at command positions - #403
sahilkhedkar wants to merge 6 commits into
Conversation
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>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 71 |
| Duplication | 4 |
AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
Pull Request Overview
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
| } | ||
| for i := 0; i < len(toks)-1; i++ { | ||
| if !reInterpreter.MatchString(toks[i]) { | ||
| if !reInterpreter.MatchString(filepath.Base(toks[i])) { |
There was a problem hiding this comment.
🔴 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>
Path-qualified interpreters such as
/bin/bash deploy.shand./node run.jsdid not match the anchored interpreter regex. Their referenced scripts were omitted fromBuildPolicyInput, 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.shnow scansdeploy.shrather than the three decoy files.Common execution wrappers (
env,sudo,command,exec, andnohup) 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/syntaxv3.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:
source, and..Validation:
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.git diff --checkpassed../scripts/sg-unit-check.shpreviously stopped at pre-existing formatting issues in unchangedpackages/guardsource 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