跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

portforward_test.go

56 行 · 1.7 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package hostagent
5
6import (
7 "context"
8 "testing"
9
10 "google.golang.org/grpc"
11 "google.golang.org/grpc/test/bufconn"
12 "google.golang.org/protobuf/types/known/emptypb"
13 "gotest.tools/v3/assert"
14
15 "github.com/lima-vm/lima/v2/pkg/guestagent/api"
16 guestagentclient "github.com/lima-vm/lima/v2/pkg/guestagent/api/client"
17)
18
19// invalidIPGuestService streams one port event whose IP is not an IP address.
20// A compromised guest can report an arbitrary string there; this one carries
21// ANSI escapes that would rewrite the operator's terminal if it reached
22// PortForwardEvent.GuestAddr.
23type invalidIPGuestService struct {
24 api.UnimplementedGuestServiceServer
25}
26
27func (*invalidIPGuestService) GetInfo(context.Context, *emptypb.Empty) (*api.Info, error) {
28 return &api.Info{}, nil
29}
30
31func (*invalidIPGuestService) GetEvents(_ *emptypb.Empty, stream api.GuestService_GetEventsServer) error {
32 return stream.Send(&api.Event{
33 AddedLocalPorts: []*api.IPPort{{Ip: "1.2.3.4\x1b[2Kspoofed\x07", Port: 8080, Protocol: "tcp"}},
34 })
35}
36
37func TestProcessGuestAgentEventsRejectsInvalidIP(t *testing.T) {
38 listener := bufconn.Listen(1024 * 1024)
39 server := grpc.NewServer(grpc.Creds(guestagentclient.NewCredentials()))
40 api.RegisterGuestServiceServer(server, &invalidIPGuestService{})
41 go func() {
42 _ = server.Serve(listener)
43 }()
44 t.Cleanup(func() {
45 server.Stop()
46 _ = listener.Close()
47 })
48
49 client, err := guestagentclient.NewGuestAgentClient(listener.DialContext)
50 assert.NilError(t, err)
51 t.Cleanup(func() { _ = client.Close() })
52
53 a := &HostAgent{guestAgentAliveCh: make(chan struct{})}
54 err = a.processGuestAgentEvents(t.Context(), client)
55 assert.ErrorContains(t, err, "invalid IP")
56}