Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ and the agent says so rather than quietly working around it.
| **Protected paths** | A path the OS itself refuses the agent, rather than a rule matched against the words in a tool call. |
| **Sandbox** | The agent's whole root is a new, minimal filesystem holding only the project; everything else is not refused, it is not there. |

For execution tools, policy inspection also reads statically named scripts invoked
through recognized interpreters, including absolute and relative interpreter paths,
`source`, and `.`. Shell syntax identifies command positions so ordinary arguments,
comments, and quoted command-like text do not consume the scan budget. Common
execution wrappers such as `env`, `sudo`, and `command` are recognized with known
options. Inspection reads at most three regular files of up to 64 KiB each; missing
or oversized files are skipped. Dynamic words, unsupported wrapper options, and
unparseable shell syntax are outside this static referenced-file inspection.

## The CLI

```
Expand Down Expand Up @@ -199,4 +208,4 @@ have made it this far.

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome.
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome.
3 changes: 2 additions & 1 deletion packages/guard/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
modernc.org/libc v1.79.0 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.12.1 // indirect
mvdan.cc/sh/v3 v3.14.1 // indirect
)

require (
Expand Down Expand Up @@ -50,4 +51,4 @@ replace github.com/solongate/agent-security/packages/shared => ../shared

replace github.com/solongate/agent-security/packages/policy => ../policy

replace github.com/solongate/agent-security/packages/crypto => ../crypto
replace github.com/solongate/agent-security/packages/crypto => ../crypto
2 changes: 2 additions & 0 deletions packages/guard/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,5 @@ modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
mvdan.cc/sh/v3 v3.14.1 h1:bXkhQWNHCs0KZEChF8hYS6FC+T2N9mUZLbQv9blditI=
mvdan.cc/sh/v3 v3.14.1/go.mod h1:syYCoFET8w9tvevxiXUtY8/ICrU+l26jHmhJDra3Vwo=
149 changes: 130 additions & 19 deletions packages/policy/extract_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"strings"

"github.com/solongate/agent-security/packages/shared"
"mvdan.cc/sh/v3/syntax"
)

var (
GlobTokenSplit = regexp.MustCompile("[\\s'\"|<>;&()]+")
reRefTokSplit = regexp.MustCompile(`[\s'"();|&<>]+`)

GlobChars = regexp.MustCompile(`[*?\[]`)

Expand Down Expand Up @@ -95,25 +95,43 @@ func ReadReferencedFiles(args map[string]interface{}, cwd string) []refFile {
if !ok {
continue
}
var toks []string
for _, t := range reRefTokSplit.Split(v, -1) {
if t != "" {
toks = append(toks, t)
}
}
for i := 0; i < len(toks)-1; i++ {
if !reInterpreter.MatchString(toks[i]) {
continue
}

j := i + 1
for j < len(toks) && strings.HasPrefix(toks[j], "-") {
j++
}
if j < len(toks) && !seen[toks[j]] {
seen[toks[j]] = true
cands = append(cands, toks[j])
// Parse command positions: ordinary arguments, comments, and quoted
// command-like text must not consume the bounded file-reading budget.
// Keep complete statements before a syntax error, without falling back
// to treating arbitrary words in malformed input as commands.
for stmt, err := range syntax.NewParser().StmtsSeq(strings.NewReader(v)) {
if err != nil {
break
}
syntax.Walk(stmt, func(node syntax.Node) bool {
call, ok := node.(*syntax.CallExpr)
if !ok || len(call.Args) < 2 {
return true
}
words := referencedInvocation(call.Args)
if len(words) < 2 {
return true
}
name, ok := referencedLiteral(words[0].Parts, false)
if !ok || !reInterpreter.MatchString(filepath.Base(name)) {
return true
}
for _, word := range words[1:] {
target, ok := referencedLiteral(word.Parts, false)
if !ok {
break
}
if strings.HasPrefix(target, "-") {
continue
}
if target != "" && !seen[target] {
seen[target] = true
cands = append(cands, target)
}
break
}
return true
})
}
}
base := cwd
Expand Down Expand Up @@ -148,6 +166,99 @@ func ReadReferencedFiles(args map[string]interface{}, cwd string) []refFile {
return out
}

// Known execution wrappers have their own command operand. Only skip options
// with known arity, so an option's value cannot masquerade as an interpreter.
var referencedWrappers = map[string]map[string]bool{
"command": {"-p": false},
"exec": {"-c": false, "-l": false, "-a": true},
"nohup": {},
"env": {"-i": false, "--ignore-environment": false, "-u": true, "--unset": true},
"sudo": {
"-E": false, "-H": false, "-n": false, "-S": false, "-k": false,
"--non-interactive": false, "--preserve-env": false,
"-u": true, "--user": true, "-g": true, "--group": true,
},
}

func referencedInvocation(words []*syntax.Word) []*syntax.Word {
for len(words) > 0 {
name, ok := referencedLiteral(words[0].Parts, false)
options, wrapper := referencedWrappers[filepath.Base(name)]
if !ok || !wrapper {
return words
}
i, flags := 1, true
for i < len(words) {
arg, ok := referencedLiteral(words[i].Parts, false)
if !ok {
return nil
}
if key, _, assigned := strings.Cut(arg, "="); filepath.Base(name) == "env" && assigned && syntax.ValidName(key) {
i++
continue
}
if !flags || !strings.HasPrefix(arg, "-") {
break
}
i++
if arg == "--" {
flags = false
continue
}
option, _, inline := strings.Cut(arg, "=")
value, known := options[option]
if !known || (inline && !strings.HasPrefix(option, "--")) {
return nil
}
if value && !inline {
i++
}
}
if i >= len(words) {
return nil
}
words = words[i:]
}
return nil
}

// referencedLiteral removes shell quoting without evaluating substitutions or
// reading the agent's environment. Dynamic words cannot identify a static file.
func referencedLiteral(parts []syntax.WordPart, doubleQuoted bool) (string, bool) {
var out strings.Builder
for _, part := range parts {
switch p := part.(type) {
case *syntax.Lit:
for i := 0; i < len(p.Value); i++ {
if p.Value[i] == '\\' && i+1 < len(p.Value) {
next := p.Value[i+1]
if !doubleQuoted || strings.ContainsRune("$`\"\\\n", rune(next)) {
i++
if next == '\n' {
continue
}
}
}
out.WriteByte(p.Value[i])
}
case *syntax.SglQuoted:
if p.Dollar {
return "", false
}
out.WriteString(p.Value)
case *syntax.DblQuoted:
value, ok := referencedLiteral(p.Parts, true)
if !ok || p.Dollar {
return "", false
}
out.WriteString(value)
default:
return "", false
}
}
return out.String(), true
}

const (
searchWalkMaxFiles = 2000
searchWalkMaxDepth = 8
Expand Down
107 changes: 98 additions & 9 deletions packages/policy/extract_paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,20 @@ func TestReadReferencedFilesReadsInterpreterTarget(t *testing.T) {
if err := os.WriteFile(script, []byte("echo hello"), 0o644); err != nil {
t.Fatal(err)
}
got := ReadReferencedFiles(args("command", "bash deploy.sh"), dir)
if len(got) != 1 || got[0].Name != "deploy.sh" {
t.Fatalf("expected deploy.sh to be read, got %+v", got)
}
if got[0].Content != "echo hello" {
t.Errorf("unexpected content: %q", got[0].Content)
for _, interpreter := range []string{
"bash", "/bin/bash", "./bash", "../bin/bash",
"node", "/usr/bin/node", "./node", "/usr/bin/python3 -u",
"source", ".",
} {
t.Run(interpreter, func(t *testing.T) {
got := ReadReferencedFiles(args("command", interpreter+" deploy.sh"), dir)
if len(got) != 1 || got[0].Name != "deploy.sh" {
t.Fatalf("expected deploy.sh to be read, got %+v", got)
}
if got[0].Content != "echo hello" {
t.Errorf("unexpected content: %q", got[0].Content)
}
})
}
}

Expand All @@ -94,9 +102,90 @@ func TestReadReferencedFilesSkipsFlagsAfterInterpreter(t *testing.T) {

func TestReadReferencedFilesIgnoresMissingOrOversizedFiles(t *testing.T) {
dir := t.TempDir()
got := ReadReferencedFiles(args("command", "bash does-not-exist.sh"), dir)
if len(got) != 0 {
t.Errorf("a nonexistent referenced file was reported as read: %+v", got)
if err := os.WriteFile(filepath.Join(dir, "oversized.sh"), make([]byte, 65537), 0o644); err != nil {
t.Fatal(err)
}
for _, name := range []string{"does-not-exist.sh", "oversized.sh"} {
t.Run(name, func(t *testing.T) {
if got := ReadReferencedFiles(args("command", "/bin/bash "+name), dir); len(got) != 0 {
t.Errorf("an unreadable referenced file was reported as read: %+v", got)
}
})
}
}

func TestReadReferencedFilesArgumentsDoNotExhaustLimit(t *testing.T) {
dir := t.TempDir()
for _, name := range []string{"a.sh", "b.sh", "c.sh", "deploy.sh"} {
if err := os.WriteFile(filepath.Join(dir, name), []byte("echo hello"), 0o644); err != nil {
t.Fatal(err)
}
}
for _, command := range []string{
"echo /bin/bash a.sh ./node b.sh /usr/bin/python3 c.sh; /bin/bash deploy.sh",
"echo bash a.sh node b.sh python3 c.sh && ./bash deploy.sh",
"echo '; /bin/bash a.sh; ./node b.sh; python3 c.sh'; /bin/bash deploy.sh",
"echo \"; /bin/bash a.sh; ./node b.sh; python3 c.sh\"; /bin/bash deploy.sh",
"echo ignored # /bin/bash a.sh; ./node b.sh; python3 c.sh\n/bin/bash deploy.sh",
"echo ignored > /bin/bash a.sh > ./node b.sh > /usr/bin/python3 c.sh; /bin/bash deploy.sh",
"cat <<'EOF'\n/bin/bash a.sh; ./node b.sh; python3 c.sh\nEOF\n/bin/bash deploy.sh",
"sudo -u /bin/bash a.sh; sudo -u ./node b.sh; sudo -u python3 c.sh; /bin/bash deploy.sh",
} {
t.Run(command, func(t *testing.T) {
got := ReadReferencedFiles(args("command", command), dir)
if len(got) != 1 || got[0].Name != "deploy.sh" {
t.Fatalf("expected only the actual script to be read, got %+v", got)
}
})
}
}

func TestReadReferencedFilesCommandPositions(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "deploy script.sh"), []byte("echo hello"), 0o644); err != nil {
t.Fatal(err)
}
for _, command := range []string{
`'/bin/bash' 'deploy script.sh'`,
`"/bin/bash" "deploy script.sh"`,
`/bin/ba\sh deploy\ script.sh`,
`MODE=test /bin/bash 'deploy script.sh'`,
`env -i MODE=test /bin/bash 'deploy script.sh'`,
`sudo -u root /bin/bash 'deploy script.sh'`,
`sudo --user=root /bin/bash 'deploy script.sh'`,
`command -p /bin/bash 'deploy script.sh'`,
`exec -a shell /bin/bash 'deploy script.sh'`,
`nohup /bin/bash 'deploy script.sh'`,
`true && /bin/bash 'deploy script.sh'`,
`false || /bin/bash 'deploy script.sh'`,
`echo hello | /bin/bash 'deploy script.sh'`,
`(/bin/bash 'deploy script.sh')`,
`if true; then /bin/bash 'deploy script.sh'; fi`,
`>/dev/null /bin/bash 'deploy script.sh'`,
`echo "$(/bin/bash 'deploy script.sh')"`,
"true\n/bin/bash 'deploy script.sh'",
} {
t.Run(command, func(t *testing.T) {
got := ReadReferencedFiles(args("command", command), dir)
if len(got) != 1 || got[0].Name != "deploy script.sh" {
t.Fatalf("expected the script at the command position to be read, got %+v", got)
}
})
}
}

func TestReadReferencedFilesInvalidFilesDoNotExhaustLimit(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "oversized.sh"), make([]byte, 65537), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "limit.sh"), make([]byte, 65536), 0o644); err != nil {
t.Fatal(err)
}
command := "/bin/bash missing.sh; /bin/bash oversized.sh; /bin/bash missing-too.sh; /bin/bash limit.sh"
got := ReadReferencedFiles(args("command", command), dir)
if len(got) != 1 || got[0].Name != "limit.sh" || len(got[0].Content) != 65536 {
t.Fatalf("expected the size-limit script after invalid files to be read, got %d files", len(got))
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/policy/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.26.11
require (
github.com/open-policy-agent/opa v1.22.0
github.com/solongate/agent-security/packages/shared v0.0.0
mvdan.cc/sh/v3 v3.14.1
)

require (
Expand Down Expand Up @@ -36,4 +37,4 @@ require (
google.golang.org/protobuf v1.36.12 // indirect
)

replace github.com/solongate/agent-security/packages/shared => ../shared
replace github.com/solongate/agent-security/packages/shared => ../shared
4 changes: 4 additions & 0 deletions packages/policy/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8=
github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-quicktest/qt v1.102.0 h1:HSQxCeh5YZH3EL3W39ixjtyaEhcWSXQHtHnMBzSs474=
github.com/go-quicktest/qt v1.102.0/go.mod h1:p4lGIVX+8Wa6ZPNDvqcxq36XpUDLh42FLetFU7odllI=
github.com/gobwas/glob v1.0.0 h1:p+FKbLEIsK1yZ39/OINwFvqNb5oyPY4H8xcy6uYu8dg=
github.com/gobwas/glob v1.0.0/go.mod h1:oWCdo522i2P1n/hMXGNWs7yoV4wy/ciZuUIbvKj5rkc=
github.com/goccy/go-json v0.10.6 h1:p8HrPJzOakx/mn/bQtjgNjdTcN+/S6FcG2CTtQOrHVU=
Expand Down Expand Up @@ -120,3 +122,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
mvdan.cc/sh/v3 v3.14.1 h1:bXkhQWNHCs0KZEChF8hYS6FC+T2N9mUZLbQv9blditI=
mvdan.cc/sh/v3 v3.14.1/go.mod h1:syYCoFET8w9tvevxiXUtY8/ICrU+l26jHmhJDra3Vwo=
Loading
Loading