mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 17:30:56 +08:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02a67bf9c5 |
+4
-4
@@ -15,12 +15,12 @@ import (
|
|||||||
func TestArgvAllowKeepsDenyAndTargetGate(t *testing.T) {
|
func TestArgvAllowKeepsDenyAndTargetGate(t *testing.T) {
|
||||||
ic := intercept.New(nil)
|
ic := intercept.New(nil)
|
||||||
ic.SetRulesForTest([]db.InterceptRule{
|
ic.SetRulesForTest([]db.InterceptRule{
|
||||||
{ID: 1, Name: "no force push", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "string",
|
{ID: 1, Name: "no force flag", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "string",
|
||||||
Pattern: "--force", Action: "deny", Message: "force push is forbidden"},
|
Pattern: "--force", Action: "deny", Message: "force push is forbidden"},
|
||||||
{ID: 2, Name: "argv allow curl", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
|
{ID: 2, Name: "argv allow curl", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
|
||||||
Pattern: `["curl"]`, Action: "allow"},
|
Pattern: `["curl"]`, Action: "allow"},
|
||||||
{ID: 3, Name: "argv allow git push", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
|
{ID: 3, Name: "argv allow nmap", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
|
||||||
Pattern: `["git","push"]`, Action: "allow"},
|
Pattern: `["nmap"]`, Action: "allow"},
|
||||||
})
|
})
|
||||||
g := NewWithInterceptor(ic)
|
g := NewWithInterceptor(ic)
|
||||||
gate := &fakeGate{t: t, check: unpinnedOnce, request: func(int, []intercept.TargetRequest, *intercept.Decision) intercept.TargetResult {
|
gate := &fakeGate{t: t, check: unpinnedOnce, request: func(int, []intercept.TargetRequest, *intercept.Decision) intercept.TargetResult {
|
||||||
@@ -29,7 +29,7 @@ func TestArgvAllowKeepsDenyAndTargetGate(t *testing.T) {
|
|||||||
g.gate = gate
|
g.gate = gate
|
||||||
ctx := gateOn(context.Background(), "7", time.Minute)
|
ctx := gateOn(context.Background(), "7", time.Minute)
|
||||||
|
|
||||||
if block, msg := pre(g, ctx, "Bash", `{"command":"git push --force origin"}`); !block || !strings.Contains(msg, "force push is forbidden") {
|
if block, msg := pre(g, ctx, "Bash", `{"command":"nmap --force 10.0.0.1"}`); !block || !strings.Contains(msg, "force push is forbidden") {
|
||||||
t.Fatalf("deny over argv allow: block=%v msg=%q", block, msg)
|
t.Fatalf("deny over argv allow: block=%v msg=%q", block, msg)
|
||||||
}
|
}
|
||||||
if gate.checks != 0 {
|
if gate.checks != 0 {
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ var alwaysAllowDenylist = func() map[string]bool {
|
|||||||
// package managers and build tools: run package scripts and hooks
|
// package managers and build tools: run package scripts and hooks
|
||||||
"npm npx pnpm yarn bunx corepack pip pipx uv uvx poetry conda composer gem bundle bundler rake cargo go " +
|
"npm npx pnpm yarn bunx corepack pip pipx uv uvx poetry conda composer gem bundle bundler rake cargo go " +
|
||||||
"mvn gradle dotnet apt apt-get dpkg dnf yum rpm pacman apk brew snap flatpak",
|
"mvn gradle dotnet apt apt-get dpkg dnf yum rpm pacman apk brew snap flatpak",
|
||||||
|
// version control and build generators: run hooks, aliases and script files
|
||||||
|
"git cmake ctest cpack",
|
||||||
// orchestration: run images, playbooks and plans
|
// orchestration: run images, playbooks and plans
|
||||||
"docker docker-compose podman nerdctl kubectl helm ansible ansible-playbook terraform pulumi vagrant",
|
"docker docker-compose podman nerdctl kubectl helm ansible ansible-playbook terraform pulumi vagrant",
|
||||||
// destructive
|
// destructive
|
||||||
|
|||||||
@@ -13,12 +13,13 @@ func TestArgvAllowDenylist(t *testing.T) {
|
|||||||
"python", "python3", "python3.11", "/usr/bin/python3.11", "php8.2", "ruby3.2", "perl5", "pip3",
|
"python", "python3", "python3.11", "/usr/bin/python3.11", "php8.2", "ruby3.2", "perl5", "pip3",
|
||||||
"env", "/usr/bin/env", "bash", "/bin/sh", "find", "rm", "mkfs", "mkfs.ext4", "/sbin/mkfs.xfs",
|
"env", "/usr/bin/env", "bash", "/bin/sh", "find", "rm", "mkfs", "mkfs.ext4", "/sbin/mkfs.xfs",
|
||||||
"docker", "kubectl", "npm", "sudo", "xargs", "Rscript", ".",
|
"docker", "kubectl", "npm", "sudo", "xargs", "Rscript", ".",
|
||||||
|
"git", "/usr/bin/git", "cmake", "cmake3", "/usr/bin/cmake", "ctest", "cpack",
|
||||||
} {
|
} {
|
||||||
if !ArgvAllowDenied(prog) {
|
if !ArgvAllowDenied(prog) {
|
||||||
t.Errorf("%q is not denied", prog)
|
t.Errorf("%q is not denied", prog)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, prog := range []string{"nmap", "git", "/usr/bin/git", "curl", "ls", "gh", "x", "7z", "pythonic"} {
|
for _, prog := range []string{"nmap", "curl", "ls", "gh", "x", "7z", "pythonic"} {
|
||||||
if ArgvAllowDenied(prog) {
|
if ArgvAllowDenied(prog) {
|
||||||
t.Errorf("%q is denied, want allowable", prog)
|
t.Errorf("%q is denied, want allowable", prog)
|
||||||
}
|
}
|
||||||
@@ -30,8 +31,8 @@ func TestAlwaysAllowPrefix(t *testing.T) {
|
|||||||
b, _ := json.Marshal(map[string]string{"command": cmd})
|
b, _ := json.Marshal(map[string]string{"command": cmd})
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
if got, err := AlwaysAllowPrefix("Bash", bash("git push --force origin main")); err != nil || !slices.Equal(got, []string{"git", "push"}) {
|
if got, err := AlwaysAllowPrefix("Bash", bash("gh pr list --limit 5")); err != nil || !slices.Equal(got, []string{"gh", "pr", "list"}) {
|
||||||
t.Fatalf("git push: %q %v", got, err)
|
t.Fatalf("gh pr list: %q %v", got, err)
|
||||||
}
|
}
|
||||||
if got, err := AlwaysAllowPrefix("Bash", bash("nmap -p 80 10.0.0.1")); err != nil || !slices.Equal(got, []string{"nmap"}) {
|
if got, err := AlwaysAllowPrefix("Bash", bash("nmap -p 80 10.0.0.1")); err != nil || !slices.Equal(got, []string{"nmap"}) {
|
||||||
t.Fatalf("nmap: %q %v", got, err)
|
t.Fatalf("nmap: %q %v", got, err)
|
||||||
@@ -40,10 +41,10 @@ func TestAlwaysAllowPrefix(t *testing.T) {
|
|||||||
tool string
|
tool string
|
||||||
input []byte
|
input []byte
|
||||||
}{
|
}{
|
||||||
"not Bash": {"shell_open", bash("git push")},
|
"not Bash": {"shell_open", bash("gh pr list")},
|
||||||
"compound": {"Bash", bash("git push; id")},
|
"compound": {"Bash", bash("gh pr list; id")},
|
||||||
"substitution": {"Bash", bash("git push $(id)")},
|
"substitution": {"Bash", bash("gh pr list $(id)")},
|
||||||
"redirection": {"Bash", bash("git push > out")},
|
"redirection": {"Bash", bash("gh pr list > out")},
|
||||||
"empty": {"Bash", bash("")},
|
"empty": {"Bash", bash("")},
|
||||||
"no command": {"Bash", []byte(`{}`)},
|
"no command": {"Bash", []byte(`{}`)},
|
||||||
"not json": {"Bash", []byte(`not json`)},
|
"not json": {"Bash", []byte(`not json`)},
|
||||||
@@ -52,6 +53,11 @@ func TestAlwaysAllowPrefix(t *testing.T) {
|
|||||||
"find": {"Bash", bash("find . -exec sh -c id ;")},
|
"find": {"Bash", bash("find . -exec sh -c id ;")},
|
||||||
"rm": {"Bash", bash("rm -rf /")},
|
"rm": {"Bash", bash("rm -rf /")},
|
||||||
"docker": {"Bash", bash("docker run --privileged x")},
|
"docker": {"Bash", bash("docker run --privileged x")},
|
||||||
|
"git": {"Bash", bash("git status")},
|
||||||
|
"path to git": {"Bash", bash("/usr/bin/git status")},
|
||||||
|
"git config": {"Bash", bash("git config user.name x")},
|
||||||
|
"cmake": {"Bash", bash("cmake --version")},
|
||||||
|
"ctest": {"Bash", bash("ctest -N")},
|
||||||
} {
|
} {
|
||||||
if got, err := AlwaysAllowPrefix(c.tool, c.input); err == nil {
|
if got, err := AlwaysAllowPrefix(c.tool, c.input); err == nil {
|
||||||
t.Errorf("%s: prefix %q, want a refusal", name, got)
|
t.Errorf("%s: prefix %q, want a refusal", name, got)
|
||||||
@@ -59,6 +65,20 @@ func TestAlwaysAllowPrefix(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The arity table is keyed by program name, so a path-qualified binary derives
|
||||||
|
// the same prefix length as the bare name.
|
||||||
|
func TestArgvPrefixLenUsesBasename(t *testing.T) {
|
||||||
|
if got := argvPrefixLen([]string{"/usr/bin/gh", "pr", "list"}); got != 3 {
|
||||||
|
t.Errorf("path-qualified gh prefix length = %d, want 3", got)
|
||||||
|
}
|
||||||
|
if got := argvPrefixLen([]string{"/usr/bin/git", "status"}); got != 2 {
|
||||||
|
t.Errorf("path-qualified git prefix length = %d, want 2", got)
|
||||||
|
}
|
||||||
|
if got, err := ArgvPrefix("/usr/bin/gh pr list"); err != nil || !slices.Equal(got, []string{"/usr/bin/gh", "pr", "list"}) {
|
||||||
|
t.Errorf("path-qualified gh prefix = %q, %v", got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The denylist neutralises argv allow rules at matching time (a rule created
|
// The denylist neutralises argv allow rules at matching time (a rule created
|
||||||
// before the denylist, or written straight into the database), and only them:
|
// before the denylist, or written straight into the database), and only them:
|
||||||
// an argv deny or ask rule for the same program still matches.
|
// an argv deny or ask rule for the same program still matches.
|
||||||
|
|||||||
+9
-1
@@ -25,9 +25,17 @@ var argvArity = map[string]int{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// argvPrefixLen returns the number of leading tokens that form the prefix.
|
// argvPrefixLen returns the number of leading tokens that form the prefix.
|
||||||
|
// The arity table is keyed by program name, so a path-qualified binary
|
||||||
|
// (/usr/bin/git) uses the same arity as the bare name.
|
||||||
func argvPrefixLen(tokens []string) int {
|
func argvPrefixLen(tokens []string) int {
|
||||||
|
if len(tokens) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
key := make([]string, len(tokens))
|
||||||
|
copy(key, tokens)
|
||||||
|
key[0] = argvProgramName(tokens[0])
|
||||||
for n := len(tokens); n > 0; n-- {
|
for n := len(tokens); n > 0; n-- {
|
||||||
if a, ok := argvArity[strings.Join(tokens[:n], " ")]; ok {
|
if a, ok := argvArity[strings.Join(key[:n], " ")]; ok {
|
||||||
return min(a, len(tokens))
|
return min(a, len(tokens))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -214,20 +214,20 @@ func TestParseArgvPattern(t *testing.T) {
|
|||||||
func TestArgvAllowBelowDeny(t *testing.T) {
|
func TestArgvAllowBelowDeny(t *testing.T) {
|
||||||
ic := New(nil)
|
ic := New(nil)
|
||||||
ic.SetRulesForTest([]db.InterceptRule{
|
ic.SetRulesForTest([]db.InterceptRule{
|
||||||
{ID: 1, Name: "no force push", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "regex",
|
{ID: 1, Name: "no force flag", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "regex",
|
||||||
Pattern: `--force`, Action: "deny"},
|
Pattern: `--force`, Action: "deny"},
|
||||||
{ID: 2, Name: "argv allow git push", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
|
{ID: 2, Name: "argv allow nmap", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
|
||||||
Pattern: `["git","push"]`, Action: "allow"},
|
Pattern: `["nmap"]`, Action: "allow"},
|
||||||
})
|
})
|
||||||
dec, ok := ic.Match("Bash", []byte(`{"command":"git push --force"}`))
|
dec, ok := ic.Match("Bash", []byte(`{"command":"nmap --force 10.0.0.1"}`))
|
||||||
if !ok || dec.Action != "deny" || dec.RuleID != 1 {
|
if !ok || dec.Action != "deny" || dec.RuleID != 1 {
|
||||||
t.Fatalf("force push: %+v %v, want the deny rule", dec, ok)
|
t.Fatalf("force flag: %+v %v, want the deny rule", dec, ok)
|
||||||
}
|
}
|
||||||
dec, ok = ic.Match("Bash", []byte(`{"command":"git push origin"}`))
|
dec, ok = ic.Match("Bash", []byte(`{"command":"nmap 10.0.0.1"}`))
|
||||||
if !ok || dec.Action != "allow" || dec.RuleID != 2 {
|
if !ok || dec.Action != "allow" || dec.RuleID != 2 {
|
||||||
t.Fatalf("push: %+v %v, want the argv allow", dec, ok)
|
t.Fatalf("nmap: %+v %v, want the argv allow", dec, ok)
|
||||||
}
|
}
|
||||||
if _, ok := ic.Match("Bash", []byte(`{"command":"git push; id"}`)); ok {
|
if _, ok := ic.Match("Bash", []byte(`{"command":"nmap 10.0.0.1; id"}`)); ok {
|
||||||
t.Fatal("a compound command matched the argv rule")
|
t.Fatal("a compound command matched the argv rule")
|
||||||
}
|
}
|
||||||
if _, ok := compileRule(db.InterceptRule{MatchType: "argv", Pattern: `["-x"]`}); ok {
|
if _, ok := compileRule(db.InterceptRule{MatchType: "argv", Pattern: `["-x"]`}); ok {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestValidateArgvRuleReq(t *testing.T) {
|
func TestValidateArgvRuleReq(t *testing.T) {
|
||||||
base := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: `["git","push"]`, Action: "allow"}
|
base := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: `["nmap"]`, Action: "allow"}
|
||||||
if err := validateInterceptRuleReq(base); err != nil {
|
if err := validateInterceptRuleReq(base); err != nil {
|
||||||
t.Fatalf("valid argv rule refused: %v", err)
|
t.Fatalf("valid argv rule refused: %v", err)
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ func TestValidateArgvRuleReq(t *testing.T) {
|
|||||||
"tool_name target": func(r *interceptRuleReq) { r.MatchTarget = "tool_name" },
|
"tool_name target": func(r *interceptRuleReq) { r.MatchTarget = "tool_name" },
|
||||||
"empty pattern": func(r *interceptRuleReq) { r.Pattern = "" },
|
"empty pattern": func(r *interceptRuleReq) { r.Pattern = "" },
|
||||||
"empty token": func(r *interceptRuleReq) { r.Pattern = `["git",""]` },
|
"empty token": func(r *interceptRuleReq) { r.Pattern = `["git",""]` },
|
||||||
"flag token": func(r *interceptRuleReq) { r.Pattern = `["git","-C"]` },
|
"flag token": func(r *interceptRuleReq) { r.Pattern = `["nmap","-C"]` },
|
||||||
"metacharacter": func(r *interceptRuleReq) { r.Pattern = `["nmap;id"]` },
|
"metacharacter": func(r *interceptRuleReq) { r.Pattern = `["nmap;id"]` },
|
||||||
"nine tokens": func(r *interceptRuleReq) { r.Pattern = `["a","b","c","d","e","f","g","h","i"]` },
|
"nine tokens": func(r *interceptRuleReq) { r.Pattern = `["a","b","c","d","e","f","g","h","i"]` },
|
||||||
"not an array": func(r *interceptRuleReq) { r.Pattern = `git push` },
|
"not an array": func(r *interceptRuleReq) { r.Pattern = `git push` },
|
||||||
@@ -31,7 +31,7 @@ func TestValidateArgvRuleReq(t *testing.T) {
|
|||||||
// The manual rule form refuses an argv allow rule for a denylisted program,
|
// The manual rule form refuses an argv allow rule for a denylisted program,
|
||||||
// with the message of the "always allow" endpoint; deny and ask stay creatable.
|
// with the message of the "always allow" endpoint; deny and ask stay creatable.
|
||||||
func TestValidateArgvAllowDenylist(t *testing.T) {
|
func TestValidateArgvAllowDenylist(t *testing.T) {
|
||||||
for _, pattern := range []string{`["python"]`, `["python3.11"]`, `["/usr/bin/env"]`, `["find"]`, `["docker","run"]`} {
|
for _, pattern := range []string{`["python"]`, `["python3.11"]`, `["/usr/bin/env"]`, `["find"]`, `["docker","run"]`, `["git","push"]`, `["cmake"]`} {
|
||||||
req := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: pattern, Action: "allow"}
|
req := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: pattern, Action: "allow"}
|
||||||
tokens, _ := intercept.ParseArgvPattern(pattern)
|
tokens, _ := intercept.ParseArgvPattern(pattern)
|
||||||
want := intercept.CheckArgvAllow(tokens)
|
want := intercept.CheckArgvAllow(tokens)
|
||||||
@@ -45,7 +45,7 @@ func TestValidateArgvAllowDenylist(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, pattern := range []string{`["nmap"]`, `["git","push"]`} {
|
for _, pattern := range []string{`["nmap"]`, `["curl"]`} {
|
||||||
req := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: pattern, Action: "allow"}
|
req := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: pattern, Action: "allow"}
|
||||||
if err := validateInterceptRuleReq(req); err != nil {
|
if err := validateInterceptRuleReq(req); err != nil {
|
||||||
t.Errorf("allow %s refused: %v", pattern, err)
|
t.Errorf("allow %s refused: %v", pattern, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user