| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/ed25519" |
| 6 | "crypto/rand" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "net" |
| 10 | "net/http" |
| 11 | "net/http/httptest" |
| 12 | "os" |
| 13 | "path/filepath" |
| 14 | "sync/atomic" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "golang.org/x/crypto/ssh" |
| 19 | ) |
| 20 | |
| 21 | func TestBrowserUsesInstanceSSHWithHostPortOccupied(t *testing.T) { |
| 22 | var leaked atomic.Bool |
| 23 | unrelated := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 24 | leaked.Store(true) |
| 25 | http.Error(w, "unrelated application", http.StatusForbidden) |
| 26 | })) |
| 27 | defer unrelated.Close() |
| 28 | streamClosed := make(chan struct{}) |
| 29 | guest := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 30 | if r.Header.Get("Authorization") != "Bearer isolated-test-token" { |
| 31 | t.Error("guest request lost authentication") |
| 32 | } |
| 33 | if r.URL.Path == "/events" { |
| 34 | w.(http.Flusher).Flush() |
| 35 | <-r.Context().Done() |
| 36 | close(streamClosed) |
| 37 | return |
| 38 | } |
| 39 | _, _ = io.WriteString(w, `{"ok":true}`) |
| 40 | })) |
| 41 | defer guest.Close() |
| 42 | listener, err := net.Listen("tcp", "127.0.0.1:0") |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | defer listener.Close() |
| 47 | _, key, _ := ed25519.GenerateKey(rand.Reader) |
| 48 | signer, _ := ssh.NewSignerFromKey(key) |
| 49 | config := &ssh.ServerConfig{NoClientAuth: true} |
| 50 | config.AddHostKey(signer) |
| 51 | var connections atomic.Int32 |
| 52 | go func() { |
| 53 | for { |
| 54 | conn, err := listener.Accept() |
| 55 | if err != nil { |
| 56 | return |
| 57 | } |
| 58 | go func() { |
| 59 | server, channels, requests, err := ssh.NewServerConn(conn, config) |
| 60 | if err != nil { |
| 61 | conn.Close() |
| 62 | return |
| 63 | } |
| 64 | defer server.Close() |
| 65 | connections.Add(1) |
| 66 | go ssh.DiscardRequests(requests) |
| 67 | for incoming := range channels { |
| 68 | var target struct { |
| 69 | Host string |
| 70 | Port uint32 |
| 71 | Origin string |
| 72 | OriginPort uint32 |
| 73 | } |
| 74 | if incoming.ChannelType() != "direct-tcpip" || ssh.Unmarshal(incoming.ExtraData(), &target) != nil || target.Host != "127.0.0.1" || target.Port != 18500 { |
| 75 | _ = incoming.Reject(ssh.Prohibited, "unexpected destination") |
| 76 | continue |
| 77 | } |
| 78 | channel, requests, err := incoming.Accept() |
| 79 | if err != nil { |
| 80 | return |
| 81 | } |
| 82 | go ssh.DiscardRequests(requests) |
| 83 | remote, err := net.Dial("tcp", guest.Listener.Addr().String()) |
| 84 | if err != nil { |
| 85 | channel.Close() |
| 86 | return |
| 87 | } |
| 88 | go func() { _, _ = io.Copy(remote, channel); remote.Close(); channel.Close() }() |
| 89 | go func() { _, _ = io.Copy(channel, remote); remote.Close(); channel.Close() }() |
| 90 | } |
| 91 | }() |
| 92 | } |
| 93 | }() |
| 94 | s := testServer(t) |
| 95 | dir := s.instanceDir() |
| 96 | if err := os.MkdirAll(dir, 0700); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | sshConfig := fmt.Sprintf("Host *\n HostName 127.0.0.1\n Port %d\n User cage\n StrictHostKeyChecking no\n UserKnownHostsFile %s\n", listener.Addr().(*net.TCPAddr).Port, os.DevNull) |
| 100 | if err := os.WriteFile(filepath.Join(dir, "ssh.config"), []byte(sshConfig), 0600); err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | client := s.browserClient() |
| 104 | defer client.close() |
| 105 | |
| 106 | |
| 107 | |
| 108 | transport := client.http.Transport.(*http.Transport) |
| 109 | dial := transport.DialContext |
| 110 | transport.DialContext = func(ctx context.Context, network, _ string) (net.Conn, error) { |
| 111 | return dial(ctx, network, unrelated.Listener.Addr().String()) |
| 112 | } |
| 113 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 114 | defer cancel() |
| 115 | data, err := client.request(ctx, "isolated-test-token", http.MethodGet, "/tabs", nil) |
| 116 | if err != nil || string(data) != `{"ok":true}` || connections.Load() == 0 || leaked.Load() { |
| 117 | t.Fatalf("browser did not use instance SSH: data=%q err=%v SSH=%d host-port-contacted=%t", data, err, connections.Load(), leaked.Load()) |
| 118 | } |
| 119 | body, err := client.events(ctx, "isolated-test-token") |
| 120 | if err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | client.close() |
| 124 | body.Close() |
| 125 | select { |
| 126 | case <-streamClosed: |
| 127 | case <-time.After(3 * time.Second): |
| 128 | t.Fatal("SSH browser subscription survived client closure") |
| 129 | } |
| 130 | } |