|
| 1 | +package scanner |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "go.uber.org/zap" |
| 8 | +) |
| 9 | + |
| 10 | +func TestDockerImageFromCommand(t *testing.T) { |
| 11 | + cases := []struct { |
| 12 | + name string |
| 13 | + info ServerInfo |
| 14 | + want string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "simple docker run", |
| 18 | + info: ServerInfo{Command: "docker", Args: []string{"run", "-i", "--rm", "mcp/fetch"}}, |
| 19 | + want: "mcp/fetch", |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "value flags before image, command after", |
| 23 | + info: ServerInfo{Command: "docker", Args: []string{"run", "--rm", "-i", "-e", "FOO=bar", "ghcr.io/x/y:tag", "serve"}}, |
| 24 | + want: "ghcr.io/x/y:tag", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "podman with volume", |
| 28 | + info: ServerInfo{Command: "podman", Args: []string{"run", "-v", "/tmp:/data", "mcp/time"}}, |
| 29 | + want: "mcp/time", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "name and attached long flag", |
| 33 | + info: ServerInfo{Command: "docker", Args: []string{"run", "--name", "x", "--network=host", "mcp/git"}}, |
| 34 | + want: "mcp/git", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "absolute docker path", |
| 38 | + info: ServerInfo{Command: "/usr/local/bin/docker", Args: []string{"run", "alpine"}}, |
| 39 | + want: "alpine", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "attached short env flag with equals", |
| 43 | + info: ServerInfo{Command: "docker", Args: []string{"run", "-e", "A=1", "-eB=2", "mcp/x"}}, |
| 44 | + want: "mcp/x", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "combined boolean short flags", |
| 48 | + info: ServerInfo{Command: "docker", Args: []string{"run", "-it", "mcp/x"}}, |
| 49 | + want: "mcp/x", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "docker container run subcommand", |
| 53 | + info: ServerInfo{Command: "docker", Args: []string{"container", "run", "mcp/x"}}, |
| 54 | + want: "mcp/x", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "not a docker command", |
| 58 | + info: ServerInfo{Command: "uvx", Args: []string{"mcp-server"}}, |
| 59 | + want: "", |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "docker but not run", |
| 63 | + info: ServerInfo{Command: "docker", Args: []string{"ps"}}, |
| 64 | + want: "", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "no image present", |
| 68 | + info: ServerInfo{Command: "docker", Args: []string{"run", "--rm"}}, |
| 69 | + want: "", |
| 70 | + }, |
| 71 | + } |
| 72 | + for _, tc := range cases { |
| 73 | + t.Run(tc.name, func(t *testing.T) { |
| 74 | + if got := dockerImageFromCommand(tc.info); got != tc.want { |
| 75 | + t.Errorf("dockerImageFromCommand() = %q, want %q", got, tc.want) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestResolveDockerRunImage(t *testing.T) { |
| 82 | + r := NewSourceResolver(zap.NewNop()) |
| 83 | + resolved, err := r.Resolve(context.Background(), ServerInfo{ |
| 84 | + Name: "fetch", |
| 85 | + Protocol: "stdio", |
| 86 | + Command: "docker", |
| 87 | + Args: []string{"run", "-i", "--rm", "mcp/fetch"}, |
| 88 | + }) |
| 89 | + if err != nil { |
| 90 | + t.Fatalf("Resolve: %v", err) |
| 91 | + } |
| 92 | + defer resolved.Cleanup() |
| 93 | + if resolved.Method != "container_image" { |
| 94 | + t.Errorf("Method = %q, want container_image", resolved.Method) |
| 95 | + } |
| 96 | + if resolved.ContainerImage != "mcp/fetch" { |
| 97 | + t.Errorf("ContainerImage = %q, want mcp/fetch", resolved.ContainerImage) |
| 98 | + } |
| 99 | + if resolved.SourceDir != "" { |
| 100 | + t.Errorf("SourceDir = %q, want empty", resolved.SourceDir) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestScannerCommandForImage(t *testing.T) { |
| 105 | + trivy := &ScannerPlugin{ |
| 106 | + ID: "trivy-mcp", |
| 107 | + Inputs: []string{"source", "container_image"}, |
| 108 | + Command: []string{"fs", "--format", "sarif", "/scan/source"}, |
| 109 | + ImageCommand: []string{"image", "--format", "sarif", "{{IMAGE}}"}, |
| 110 | + } |
| 111 | + |
| 112 | + // Image present + scanner supports it → image-mode command with substitution. |
| 113 | + got := effectiveScannerCommand(trivy, ScanRequest{ContainerImage: "mcp/fetch"}) |
| 114 | + want := []string{"image", "--format", "sarif", "mcp/fetch"} |
| 115 | + if !equalSlice(got, want) { |
| 116 | + t.Errorf("image-mode command = %v, want %v", got, want) |
| 117 | + } |
| 118 | + |
| 119 | + // No image → fall back to default source-mode command. |
| 120 | + got = effectiveScannerCommand(trivy, ScanRequest{}) |
| 121 | + if !equalSlice(got, trivy.Command) { |
| 122 | + t.Errorf("no-image command = %v, want %v", got, trivy.Command) |
| 123 | + } |
| 124 | + |
| 125 | + // Scanner without container_image support → keep its command even if an image is present. |
| 126 | + semgrep := &ScannerPlugin{ |
| 127 | + ID: "semgrep-mcp", |
| 128 | + Inputs: []string{"source"}, |
| 129 | + Command: []string{"semgrep", "scan", "/scan/source"}, |
| 130 | + } |
| 131 | + got = effectiveScannerCommand(semgrep, ScanRequest{ContainerImage: "mcp/fetch"}) |
| 132 | + if !equalSlice(got, semgrep.Command) { |
| 133 | + t.Errorf("non-image scanner command = %v, want %v", got, semgrep.Command) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func equalSlice(a, b []string) bool { |
| 138 | + if len(a) != len(b) { |
| 139 | + return false |
| 140 | + } |
| 141 | + for i := range a { |
| 142 | + if a[i] != b[i] { |
| 143 | + return false |
| 144 | + } |
| 145 | + } |
| 146 | + return true |
| 147 | +} |
0 commit comments