| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "encoding/base64" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "net" |
| 11 | "net/http" |
| 12 | "net/http/httptest" |
| 13 | "net/url" |
| 14 | "os" |
| 15 | "os/exec" |
| 16 | "runtime" |
| 17 | "strings" |
| 18 | "sync/atomic" |
| 19 | "testing" |
| 20 | "time" |
| 21 | |
| 22 | "github.com/sylvinus/agent-vm/internal/cage" |
| 23 | ) |
| 24 | |
| 25 | func TestMaintenanceRequiresAuthenticationAndSoftwareSource(t *testing.T) { |
| 26 | var calls atomic.Int32 |
| 27 | upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 28 | calls.Add(1) |
| 29 | if r.Header.Get("Proxy-Authorization") != "" { |
| 30 | t.Error("installer credential reached software origin") |
| 31 | } |
| 32 | _, _ = io.WriteString(w, "package bytes") |
| 33 | })) |
| 34 | defer upstream.Close() |
| 35 | destination, _ := url.Parse(upstream.URL) |
| 36 | dial := func(ctx context.Context, network, address string) (net.Conn, error) { |
| 37 | if address != "deb.debian.org:80" && address != "deb.nodesource.com:80" && address != "archive.ubuntu.com:80" && address != "security.ubuntu.com:80" && address != "ports.ubuntu.com:80" { |
| 38 | return nil, errors.New("unexpected destination") |
| 39 | } |
| 40 | return (&net.Dialer{}).DialContext(ctx, network, destination.Host) |
| 41 | } |
| 42 | proxy := &maintenanceProxy{token: "fixture-only", ctx: context.Background(), dial: dial, connections: map[net.Conn]bool{}, transport: &http.Transport{Proxy: nil, DialContext: dial}} |
| 43 | server := httptest.NewServer(proxy) |
| 44 | defer server.Close() |
| 45 | defer proxy.close() |
| 46 | gate, err := cage.NewEgressGate(cage.DefaultConfig()) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | for _, test := range []struct { |
| 51 | target, method string |
| 52 | auth bool |
| 53 | status int |
| 54 | }{ |
| 55 | {"http://deb.debian.org/debian/pool/file.deb", "GET", false, 407}, |
| 56 | {"http://example.com/private", "GET", true, 403}, |
| 57 | {"http://127.0.0.1/private", "GET", true, 403}, |
| 58 | {"http://deb.debian.org:8080/private", "GET", true, 403}, |
| 59 | {"http://deb.debian.org/debian/pool/file.deb", "POST", true, 405}, |
| 60 | {"http://deb.debian.org/debian/pool/file.deb", "GET", true, 200}, |
| 61 | {"http://deb.nodesource.com/node_24.x/dists/nodistro/InRelease", "GET", true, 200}, |
| 62 | {"http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease", "GET", true, 200}, |
| 63 | {"http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease", "GET", true, 200}, |
| 64 | {"http://ports.ubuntu.com/ubuntu-ports/dists/jammy/InRelease", "GET", true, 200}, |
| 65 | } { |
| 66 | proxyURL, _ := url.Parse(server.URL) |
| 67 | if test.auth { |
| 68 | proxyURL.User = url.UserPassword("cage-install", "fixture-only") |
| 69 | } |
| 70 | transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)} |
| 71 | client := &http.Client{Transport: transport, Timeout: time.Second} |
| 72 | request, _ := http.NewRequest(test.method, test.target, nil) |
| 73 | response, err := client.Do(request) |
| 74 | if err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | body, _ := io.ReadAll(response.Body) |
| 78 | response.Body.Close() |
| 79 | transport.CloseIdleConnections() |
| 80 | if response.StatusCode != test.status { |
| 81 | t.Fatalf("%s: %d", test.target, response.StatusCode) |
| 82 | } |
| 83 | if test.status == 200 && string(body) != "package bytes" { |
| 84 | t.Fatal("download bytes changed") |
| 85 | } |
| 86 | } |
| 87 | if calls.Load() != 5 { |
| 88 | t.Fatal("rejected request reached origin") |
| 89 | } |
| 90 | if gate.Status().Mode != cage.GateClosed { |
| 91 | t.Fatal("maintenance changed business gate") |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestMaintenanceClosesTLSConnectionsAndRefusesBusinessHosts(t *testing.T) { |
| 96 | proxy := &maintenanceProxy{token: "fixture", ctx: context.Background(), connections: map[net.Conn]bool{}, transport: &http.Transport{}} |
| 97 | proxy.dial = func(context.Context, string, string) (net.Conn, error) { |
| 98 | a, b := net.Pipe() |
| 99 | go func() { defer b.Close(); _, _ = io.Copy(b, b) }() |
| 100 | return a, nil |
| 101 | } |
| 102 | server := httptest.NewServer(proxy) |
| 103 | defer server.Close() |
| 104 | defer proxy.close() |
| 105 | address, _ := url.Parse(server.URL) |
| 106 | connection, err := net.Dial("tcp", address.Host) |
| 107 | if err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | defer connection.Close() |
| 111 | _ = connection.SetDeadline(time.Now().Add(3 * time.Second)) |
| 112 | auth := base64.StdEncoding.EncodeToString([]byte("cage-install:fixture")) |
| 113 | _, _ = fmt.Fprintf(connection, "CONNECT deb.debian.org:443 HTTP/1.1\r\nHost: deb.debian.org:443\r\nProxy-Authorization: Basic %s\r\n\r\n", auth) |
| 114 | reader := bufio.NewReader(connection) |
| 115 | response, err := http.ReadResponse(reader, nil) |
| 116 | if err != nil || response.StatusCode != 200 { |
| 117 | t.Fatal(response, err) |
| 118 | } |
| 119 | _, _ = connection.Write([]byte("abc")) |
| 120 | bytes := make([]byte, 3) |
| 121 | if _, err = io.ReadFull(reader, bytes); err != nil || string(bytes) != "abc" { |
| 122 | t.Fatal("tunnel failed", err) |
| 123 | } |
| 124 | proxy.close() |
| 125 | if _, err = reader.ReadByte(); err == nil { |
| 126 | t.Fatal("maintenance left a live tunnel") |
| 127 | } |
| 128 | for _, address := range []string{"claude.ai:443", "api.anthropic.com:443", "deb.debian.org:22", "127.0.0.1:443", "github.com.evil.test:443"} { |
| 129 | if maintenanceDestination(address, true) { |
| 130 | t.Fatal("business or non-HTTPS destination accepted", address) |
| 131 | } |
| 132 | } |
| 133 | for _, host := range []string{"mirrors.nju.edu.cn", "mirrors.ustc.edu.cn", "cdn.npmmirror.com", "storage.googleapis.com", "codeload.github.com"} { |
| 134 | if !maintenanceDestination(host+":443", true) || maintenanceDestination(host+":80", false) || maintenanceDestination(host+".evil.test:443", true) { |
| 135 | t.Fatalf("unexpected mirror policy for %s", host) |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func TestMaintenanceScriptUsesPrivateTemporaryConfiguration(t *testing.T) { |
| 141 | if runtime.GOOS == "windows" { |
| 142 | t.Skip("the installer Bash script runs in the Linux guest, not the Windows host") |
| 143 | } |
| 144 | shell, err := exec.LookPath("bash") |
| 145 | if err != nil { |
| 146 | t.Skip("bash not available") |
| 147 | } |
| 148 | command := exec.Command(shell, "-se") |
| 149 | command.Stdin = strings.NewReader(maintenanceScript(`test -s "$CAGE_APT_CONFIG" |
| 150 | test "$http_proxy" = "$CAGE_INSTALL_PROXY" |
| 151 | test "$(umask)" = 0022 |
| 152 | printf '%s' "$CAGE_APT_CONFIG" |
| 153 | `, "fixture-token")) |
| 154 | output, err := command.Output() |
| 155 | if err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | if _, err = os.Stat(string(output)); !os.IsNotExist(err) { |
| 159 | t.Fatal("maintenance credential was retained") |
| 160 | } |
| 161 | } |