-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathintegration_shellguard_test.go
More file actions
232 lines (202 loc) · 5.69 KB
/
integration_shellguard_test.go
File metadata and controls
232 lines (202 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package shellguard_test
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"
"github.com/fawdyinc/shellguard/manifest"
"github.com/fawdyinc/shellguard/parser"
"github.com/fawdyinc/shellguard/server"
"github.com/fawdyinc/shellguard/ssh"
"github.com/fawdyinc/shellguard/toolkit"
"github.com/fawdyinc/shellguard/validator"
)
func validateCommand(t *testing.T, registry map[string]*manifest.Manifest, command string) error {
t.Helper()
pipeline, err := parser.Parse(command)
if err != nil {
return err
}
return validator.ValidatePipeline(pipeline, registry)
}
func TestIntegrationSecurity_AllowedReadOnlyCommands(t *testing.T) {
registry, err := manifest.LoadEmbedded()
if err != nil {
t.Fatalf("LoadEmbedded() error = %v", err)
}
allowed := []string{
"ls /tmp",
"find /var/log -name '*.log' | head -20",
"psql -c 'SELECT 1'",
"docker ps",
"aws ec2 describe-instances",
}
for _, cmd := range allowed {
if err := validateCommand(t, registry, cmd); err != nil {
t.Fatalf("expected allowed command %q, got error: %v", cmd, err)
}
}
}
func TestIntegrationSecurity_RejectsMutationAndEscapes(t *testing.T) {
registry, err := manifest.LoadEmbedded()
if err != nil {
t.Fatalf("LoadEmbedded() error = %v", err)
}
rejected := []string{
"rm /tmp/file",
"systemctl start nginx",
"docker run alpine",
"psql -c 'DELETE FROM users'",
"python -c 'print(1)'",
"apt update",
"vim /tmp/file",
"env bash -lc id",
"curl -X POST https://example.com",
"unzip archive.zip",
"tar -xf archive.tar",
}
for _, cmd := range rejected {
if err := validateCommand(t, registry, cmd); err == nil {
t.Fatalf("expected rejected command %q to fail validation", cmd)
}
}
}
func TestIntegrationSecurity_RejectsParserEscapes(t *testing.T) {
rejectedByParser := []string{
"echo $(id)",
"cat /etc/passwd > /tmp/x",
"sleep 1 &",
"ls /tmp; rm -rf /",
}
for _, cmd := range rejectedByParser {
if _, err := parser.Parse(cmd); err == nil {
t.Fatalf("expected parser to reject %q", cmd)
}
}
}
func TestIntegrationProbeCommandContract(t *testing.T) {
cmd := toolkit.BuildProbeCommand()
if cmd != "PATH=$HOME/.shellguard/bin:$PATH command -v rg jq yq 2>/dev/null; echo '---'; uname -m" {
t.Fatalf("unexpected probe command: %q", cmd)
}
registry, err := manifest.LoadEmbedded()
if err != nil {
t.Fatalf("LoadEmbedded() error = %v", err)
}
runner := &integrationRunner{
executeRawResults: map[string]ssh.ExecResult{
cmd: {Stdout: "/usr/bin/jq\n---\nx86_64\n"},
},
}
core := server.NewCore(registry, runner, nil)
if _, err := core.Connect(context.Background(), server.ConnectInput{Host: "host1"}); err != nil {
t.Fatalf("Connect() error = %v", err)
}
if len(runner.executeRawCalls) == 0 || runner.executeRawCalls[0] != cmd {
t.Fatalf("expected ExecuteRaw probe call %q, got %#v", cmd, runner.executeRawCalls)
}
}
func TestIntegrationToolkitPathReconstructionCorrect(t *testing.T) {
pipeline, err := parser.Parse("ls /tmp")
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
got := ssh.ReconstructCommand(pipeline, false, true)
if !strings.HasPrefix(got, "PATH=$HOME/.shellguard/bin:$PATH ") {
t.Fatalf("expected toolkit PATH prefix, got %q", got)
}
}
func TestIntegrationToolNames(t *testing.T) {
registry, err := manifest.LoadEmbedded()
if err != nil {
t.Fatalf("LoadEmbedded() error = %v", err)
}
for _, tool := range toolkit.ToolkitTools {
m := registry[tool]
if m == nil {
t.Fatalf("missing manifest for toolkit tool %q", tool)
return
}
if m.Deny {
t.Fatalf("tool %q must be allowed, but manifest is deny=true", tool)
}
}
}
func TestIntegration_StdioServerStartsAndExitsOnEOF(t *testing.T) {
bin := integrationBinary(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, bin)
cmd.Stdin = strings.NewReader("")
cmd.Stdout = io.Discard
cmd.Stderr = io.Discard
if err := cmd.Run(); err != nil {
t.Fatalf("run shellguard stdio server: %v", err)
}
}
type integrationRunner struct {
executeRawResults map[string]ssh.ExecResult
executeRawCalls []string
}
func (r *integrationRunner) Connect(_ context.Context, _ ssh.ConnectionParams) error {
return nil
}
func (r *integrationRunner) Execute(_ context.Context, _, _ string, _ time.Duration) (ssh.ExecResult, error) {
return ssh.ExecResult{Stdout: "ok", ExitCode: 0}, nil
}
func (r *integrationRunner) ExecuteRaw(_ context.Context, _, command string, _ time.Duration) (ssh.ExecResult, error) {
r.executeRawCalls = append(r.executeRawCalls, command)
if res, ok := r.executeRawResults[command]; ok {
return res, nil
}
return ssh.ExecResult{}, nil
}
func (r *integrationRunner) SFTPSession(_ string) (ssh.SFTPClient, error) {
return nil, errors.New("not implemented")
}
func (r *integrationRunner) Disconnect(_ context.Context, _ string) error {
return nil
}
var (
binaryOnce sync.Once
binaryPath string
binaryErr error
)
func integrationBinary(t *testing.T) string {
t.Helper()
binaryOnce.Do(func() {
root := moduleRoot(t)
dir, err := os.MkdirTemp("", "shellguard-integration-bin-*")
if err != nil {
binaryErr = err
return
}
binaryPath = filepath.Join(dir, "shellguard")
cmd := exec.Command("go", "build", "-o", binaryPath, "./cmd/shellguard")
cmd.Dir = root
out, err := cmd.CombinedOutput()
if err != nil {
binaryErr = fmt.Errorf("go build failed: %w: %s", err, string(out))
}
})
if binaryErr != nil {
t.Fatalf("build integration binary: %v", binaryErr)
}
return binaryPath
}
func moduleRoot(t *testing.T) string {
t.Helper()
_, file, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller failed")
}
return filepath.Dir(file)
}