Mac slice F task 6 fix round 1: drop inherited fds and env, Mac tests
- tool-proxy closes inherited descriptors above 2 (those without FD_CLOEXEC; the runtime's all carry it) and empties its environment before it dials; tested in a child with a token pipe and sentinels. - Path refusal test: a 103-byte socket serves, a real 104-byte socket (bound relatively, so it exists on a Mac too) is refused, and a dial seam shows every refusal precedes the dial. - Release guard builds linux/amd64 explicitly and checks the darwin release has the endpoint and no test-build code. - Transcript comparison is exact, trailing newline included.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+396
−52
cmd/equipo-runner/release_guard_test.go+21 −1cmd/equipo-runner/tool_proxy_isolation_test.go+184 −0cmd/equipo-runner/tool_proxy_mac.go+57 −0cmd/equipo-runner/tool_proxy_mac_test.go+41 −39internal/runner/thin/toolproxy_transcript_test.go+49 −4internal/runner/tool_proxy_mac.go+5 −1internal/runner/tool_proxy_mac_test.go+39 −7
Intent
diff --git a/cmd/equipo-runner/release_guard_test.go b/cmd/equipo-runner/release_guard_test.go| index 17ac431b..e92a9e4e 100644 |
| --- a/cmd/equipo-runner/release_guard_test.go |
| +++ b/cmd/equipo-runner/release_guard_test.go |
| @@ -35,8 +35,9 @@ func TestReleaseRunnerHasNoFakeWorker(t *testing.T) { |
| } |
| dir := t.TempDir() |
| bin := filepath.Join(dir, "equipo-runner") |
| + // The Linux release, on whatever machine runs this. |
| build := exec.Command("go", "build", "-trimpath", "-ldflags", "-s -w", "-o", bin, ".") |
| - build.Env = append(os.Environ(), "CGO_ENABLED=0") |
| + build.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=linux", "GOARCH=amd64") |
| testci.GoCommand(t, build) |
| testci.Files(t, "../../scripts/no-fake-worker.sh") |
| if out, err := build.CombinedOutput(); err != nil { |
| @@ -60,6 +61,25 @@ func TestReleaseRunnerHasNoFakeWorker(t *testing.T) { |
| if out, err := exec.Command("../../scripts/no-fake-worker.sh", bin).CombinedOutput(); err != nil { |
| t.Fatalf("the release check refused a clean binary: %s", out) |
| } |
| + // The Mac release (darwin/arm64, as make release-runner builds it) |
| + // has the Mac's endpoint and nothing of the test build. |
| + mac := filepath.Join(dir, "equipo-runner-darwin") |
| + build = exec.Command("go", "build", "-trimpath", "-ldflags", "-s -w", "-o", mac, ".") |
| + build.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=darwin", "GOARCH=arm64") |
| + testci.GoCommand(t, build) |
| + if out, err := build.CombinedOutput(); err != nil { |
| + t.Fatalf("Mac release build: %v %s", err, out) |
| + } |
| + raw, err = os.ReadFile(mac) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if !bytes.Contains(raw, []byte(darwinProxyMarker)) || bytes.Contains(raw, []byte(marker)) || bytes.Contains(raw, []byte(egressMarker)) { |
| + t.Fatal("the Mac release runner lacks its tool endpoint or holds the test build's code") |
| + } |
| + if out, err := exec.Command("../../scripts/no-fake-worker.sh", mac).CombinedOutput(); err != nil { |
| + t.Fatalf("the release check refused the Mac runner: %s", out) |
| + } |
| archive := filepath.Join(dir, "thin.a") |
| test := exec.Command("go", "build", "-tags", "testbuild", "-o", archive, "../../internal/runner/thin") |
| test.Env = append(os.Environ(), "CGO_ENABLED=0") |
diff --git a/cmd/equipo-runner/tool_proxy_isolation_test.go b/cmd/equipo-runner/tool_proxy_isolation_test.go| new file mode 100644 |
| index 00000000..27374d9c |
| --- /dev/null |
| +++ b/cmd/equipo-runner/tool_proxy_isolation_test.go |
| @@ -0,0 +1,184 @@ |
| +//go:build darwin || testbuild |
| + |
| +package main |
| + |
| +import ( |
| + "bufio" |
| + "context" |
| + "encoding/json" |
| + "net" |
| + "os" |
| + "path/filepath" |
| + "strconv" |
| + "testing" |
| + "time" |
| + |
| + "golang.org/x/sys/unix" |
| +) |
| + |
| +// childArgs starts this test binary as equipo-runner with those |
| +// arguments (a JSON array, toolProxyChild) instead of running its tests; |
| +// childReport names the file it reports to. |
| +const ( |
| + childArgs = "EQUIPO_TEST_TOOL_PROXY_ARGS" |
| + childReport = "EQUIPO_TEST_TOOL_PROXY_REPORT" |
| +) |
| + |
| +func init() { |
| + if raw := os.Getenv(childArgs); raw != "" { |
| + var args []string |
| + if json.Unmarshal([]byte(raw), &args) != nil { |
| + os.Exit(93) |
| + } |
| + os.Exit(toolProxyChild(args, os.Getenv(childReport))) |
| + } |
| +} |
| + |
| +// toolProxyReport is what the endpoint process holds once it has |
| +// served: its environment, the descriptors above 2 without |
| +// close-on-exec, the identity of every descriptor above 2, and any |
| +// variable run read through getenv. |
| +type toolProxyReport struct { |
| + Getenv []string `json:"getenv"` |
| + Env []string `json:"env"` |
| + Inherited []int `json:"inherited"` |
| + Open [][2]uint64 `json:"open"` |
| + Pipe bool `json:"pipe"` |
| +} |
| + |
| +// toolProxyChild runs equipo-runner as the CLI starts it, then reports |
| +// to the file at report. The Go runtime's poller is in use before the |
| +// endpoint starts (a pipe, as signal handling and the runner's own files |
| +// are): its descriptors must survive, which the pipe shows afterwards. |
| +func toolProxyChild(args []string, reportPath string) int { |
| + r, w, err := os.Pipe() |
| + if err != nil { |
| + return 90 |
| + } |
| + var read []string |
| + code := run(context.Background(), args, os.Stdin, os.Stdout, os.Stderr, func(k string) string { read = append(read, k); return "" }) |
| + report := toolProxyReport{Getenv: read, Env: os.Environ()} |
| + _ = r.SetReadDeadline(time.Now().Add(5 * time.Second)) |
| + if _, err := w.Write([]byte("x")); err == nil { |
| + b := make([]byte, 1) |
| + n, err := r.Read(b) |
| + report.Pipe = err == nil && n == 1 |
| + } |
| + entries, err := os.ReadDir("/dev/fd") |
| + if err != nil { |
| + return 91 |
| + } |
| + for _, e := range entries { |
| + fd, err := strconv.Atoi(e.Name()) |
| + if err != nil || fd <= 2 { |
| + continue |
| + } |
| + flags, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0) |
| + if err != nil { |
| + continue // the directory's own descriptor, closed by now |
| + } |
| + if flags&unix.FD_CLOEXEC == 0 { |
| + report.Inherited = append(report.Inherited, fd) |
| + } |
| + var st unix.Stat_t |
| + if unix.Fstat(fd, &st) == nil { |
| + report.Open = append(report.Open, [2]uint64{uint64(st.Dev), uint64(st.Ino)}) |
| + } |
| + } |
| + raw, err := json.Marshal(report) |
| + if err != nil || os.WriteFile(reportPath, raw, 0600) != nil { |
| + return 92 |
| + } |
| + return code |
| +} |
| + |
| +// The endpoint starts as the CLI's child, so it inherits whatever the CLI |
| +// leaves open and its whole environment: a token's descriptor |
| +// (CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR) and any token variable. It |
| +// keeps neither while it serves: every inherited descriptor above 2 is |
| +// closed and the environment is emptied, while the runtime's own |
| +// descriptors (its poller) keep working. |
| +func TestToolProxyDropsInheritedCredentials(t *testing.T) { |
| + socket := echoSocket(t) |
| + tokenR, tokenW, err := os.Pipe() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer tokenR.Close() |
| + defer tokenW.Close() |
| + if _, err := tokenW.WriteString("sk-ant-oat01-SENTINEL\n"); err != nil { |
| + t.Fatal(err) |
| + } |
| + var tokenID [2]uint64 |
| + var st unix.Stat_t |
| + if err := unix.Fstat(int(tokenR.Fd()), &st); err != nil { |
| + t.Fatal(err) |
| + } |
| + tokenID = [2]uint64{uint64(st.Dev), uint64(st.Ino)} |
| + // The token's read end as fd 3, its write end as fd 6. |
| + code, out, stderr, report := runToolProxy(t, []string{"tool-proxy", "--socket", socket}, `{"jsonrpc":"2.0","id":1,"method":"ping"}`+"\n", []*os.File{tokenR, nil, nil, tokenW}, |
| + "CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-SENTINEL", "CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR=3", "ANTHROPIC_API_KEY=sk-ant-api-SENTINEL", "HOME=/Users/sentinel", "PATH=/usr/bin:/bin") |
| + if code != 0 || stderr != "" || out != `{"jsonrpc":"2.0","id":1,"result":{}}`+"\n" { |
| + t.Fatalf("the endpoint did not serve: exit %d, out %q, stderr %q", code, out, stderr) |
| + } |
| + if len(report.Env) != 0 { |
| + t.Fatalf("the endpoint kept its environment: %q", report.Env) |
| + } |
| + if len(report.Inherited) != 0 { |
| + t.Fatalf("the endpoint kept inherited descriptors %v", report.Inherited) |
| + } |
| + for _, id := range report.Open { |
| + if tokenID[1] != 0 && id == tokenID { |
| + t.Fatalf("the endpoint still holds the token's pipe (%v)", id) |
| + } |
| + } |
| + if !report.Pipe { |
| + t.Fatal("the runtime's poller no longer works after the endpoint started") |
| + } |
| +} |
| + |
| +// echoSocket is a 0600 socket in a 0700 directory, without links, that |
| +// answers each request with an empty result. |
| +func echoSocket(t *testing.T) string { |
| + t.Helper() |
| + root, err := filepath.EvalSymlinks(os.TempDir()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + dir, err := os.MkdirTemp(root, "t") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { os.RemoveAll(dir) }) |
| + if err := os.Chmod(dir, 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + path := filepath.Join(dir, "s") |
| + l, err := net.ListenUnix("unix", &net.UnixAddr{Name: path, Net: "unix"}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { l.Close() }) |
| + if err := os.Chmod(path, 0600); err != nil { |
| + t.Fatal(err) |
| + } |
| + go func() { |
| + for { |
| + c, err := l.AcceptUnix() |
| + if err != nil { |
| + return |
| + } |
| + go func() { |
| + defer c.Close() |
| + r := bufio.NewReader(c) |
| + for { |
| + if _, err := r.ReadBytes('\n'); err != nil { |
| + return |
| + } |
| + c.Write([]byte(`{"jsonrpc":"2.0","id":1,"result":{}}` + "\n")) |
| + } |
| + }() |
| + } |
| + }() |
| + return path |
| +} |
diff --git a/cmd/equipo-runner/tool_proxy_mac.go b/cmd/equipo-runner/tool_proxy_mac.go| index 6093fa49..af191e2b 100644 |
| --- a/cmd/equipo-runner/tool_proxy_mac.go |
| +++ b/cmd/equipo-runner/tool_proxy_mac.go |
| @@ -4,7 +4,12 @@ package main |
| import ( |
| "context" |
| + "errors" |
| "io" |
| + "os" |
| + "strconv" |
| + |
| + "golang.org/x/sys/unix" |
| "amadan.net/oficina/equipo/internal/runner" |
| ) |
| @@ -19,8 +24,60 @@ func toolProxy(ctx context.Context, args []string, input io.Reader, output io.Wr |
| if len(args) != 2 || args[0] != "--socket" { |
| return 2, true |
| } |
| + if dropInherited() != nil { |
| + return 1, true |
| + } |
| if runner.ServeDarwinToolProxy(ctx, input, output, args[1]) != nil { |
| return 1, true |
| } |
| return 0, true |
| } |
| + |
| +// errInherited: the endpoint could not list or close what it inherited. |
| +var errInherited = errors.New("tool endpoint: inherited descriptors") |
| + |
| +// dropInherited lets go of what the endpoint inherited from the CLI that |
| +// started it: a token's descriptor (CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR) |
| +// and the CLI's whole environment. It serves with neither. |
| +// |
| +// A descriptor above 2 is inherited exactly when close-on-exec is clear: |
| +// one that had it set did not survive exec, and every descriptor the Go |
| +// runtime and standard library open carries it (the poller's kqueue or |
| +// epoll and eventfd, pipes, files, sockets; this binary has no cgo). So |
| +// the runtime's own descriptors are kept by what they are, not guessed |
| +// by number, whenever they were opened. The list comes from /dev/fd (on |
| +// Linux a link to /proc/self/fd). |
| +// |
| +// The environment is emptied: the endpoint needs none of it. That clears |
| +// the process's own copy; the original strings exec placed in memory |
| +// remain, which is why a token never travels in the environment (task 8 |
| +// passes it by descriptor). |
| +func dropInherited() error { |
| + entries, err := os.ReadDir("/dev/fd") |
| + if err != nil { |
| + return errInherited |
| + } |
| + for _, e := range entries { |
| + fd, err := strconv.Atoi(e.Name()) |
| + if err != nil { |
| + return errInherited |
| + } |
| + if fd <= 2 { |
| + continue |
| + } |
| + flags, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0) |
| + if errors.Is(err, unix.EBADF) { |
| + continue // the listing's own descriptor, closed already |
| + } |
| + if err != nil { |
| + return errInherited |
| + } |
| + if flags&unix.FD_CLOEXEC == 0 { |
| + if err := unix.Close(fd); err != nil { |
| + return errInherited |
| + } |
| + } |
| + } |
| + os.Clearenv() |
| + return nil |
| +} |
diff --git a/cmd/equipo-runner/tool_proxy_mac_test.go b/cmd/equipo-runner/tool_proxy_mac_test.go| index 25062334..d2d8f853 100644 |
| --- a/cmd/equipo-runner/tool_proxy_mac_test.go |
| +++ b/cmd/equipo-runner/tool_proxy_mac_test.go |
| @@ -3,55 +3,57 @@ |
| package main |
| import ( |
| - "bufio" |
| "bytes" |
| "context" |
| - "net" |
| + "encoding/json" |
| "os" |
| + "os/exec" |
| "path/filepath" |
| "strings" |
| "testing" |
| + "time" |
| ) |
| -// equipo-runner tool-proxy --socket <path> is a Mac's MCP tool endpoint. |
| -// Like the Linux tool proxy it takes exactly that, reads no environment |
| -// and no configuration, says nothing on stderr, and exits 2 for any other |
| -// arguments and 1 when it cannot serve. |
| -func TestToolProxyCommand(t *testing.T) { |
| - noEnv := func(k string) string { t.Fatalf("tool-proxy read %s", k); return "" } |
| - root, err := filepath.EvalSymlinks(os.TempDir()) |
| +// runToolProxy runs equipo-runner tool-proxy with args as the CLI starts |
| +// it, in a child process (toolProxyChild): the endpoint lets go of its |
| +// inherited descriptors and environment, which must not happen to the |
| +// test process. extra are descriptors from 3 on, env the child's |
| +// environment besides what starts it. |
| +func runToolProxy(t *testing.T, args []string, input string, extra []*os.File, env ...string) (code int, out, stderr string, report toolProxyReport) { |
| + t.Helper() |
| + rawArgs, err := json.Marshal(args) |
| if err != nil { |
| t.Fatal(err) |
| } |
| - dir, err := os.MkdirTemp(root, "t") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - defer os.RemoveAll(dir) |
| - if err := os.Chmod(dir, 0700); err != nil { |
| - t.Fatal(err) |
| + reportPath := filepath.Join(t.TempDir(), "report.json") |
| + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| + defer cancel() |
| + cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^$") |
| + cmd.Env = append([]string{childArgs + "=" + string(rawArgs), childReport + "=" + reportPath}, env...) |
| + cmd.ExtraFiles = extra |
| + cmd.Stdin = strings.NewReader(input) |
| + var o, e bytes.Buffer |
| + cmd.Stdout, cmd.Stderr = &o, &e |
| + err = cmd.Run() |
| + if exit, ok := err.(*exec.ExitError); ok { |
| + code = exit.ExitCode() |
| + } else if err != nil { |
| + t.Fatalf("%q: %v", args, err) |
| } |
| - path := filepath.Join(dir, "s") |
| - l, err := net.ListenUnix("unix", &net.UnixAddr{Name: path, Net: "unix"}) |
| - if err != nil { |
| - t.Fatal(err) |
| + raw, err := os.ReadFile(reportPath) |
| + if err != nil || json.Unmarshal(raw, &report) != nil { |
| + t.Fatalf("%q: no report (exit %d, stderr %q): %v", args, code, e.String(), err) |
| } |
| - defer l.Close() |
| - if err := os.Chmod(path, 0600); err != nil { |
| - t.Fatal(err) |
| - } |
| - go func() { |
| - for { |
| - c, err := l.AcceptUnix() |
| - if err != nil { |
| - return |
| - } |
| - if _, err := bufio.NewReader(c).ReadBytes('\n'); err == nil { |
| - c.Write([]byte(`{"jsonrpc":"2.0","id":1,"result":{}}` + "\n")) |
| - } |
| - c.Close() |
| - } |
| - }() |
| + return code, o.String(), e.String(), report |
| +} |
| + |
| +// equipo-runner tool-proxy --socket <path> is a Mac's MCP tool endpoint. |
| +// Like the Linux tool proxy it takes exactly that, reads no environment |
| +// and no configuration, says nothing on stderr, and exits 2 for any other |
| +// arguments and 1 when it cannot serve. |
| +func TestToolProxyCommand(t *testing.T) { |
| + path := echoSocket(t) |
| + dir := filepath.Dir(path) |
| ping := `{"jsonrpc":"2.0","id":1,"method":"ping"}` + "\n" |
| for _, tc := range []struct { |
| args []string |
| @@ -67,9 +69,9 @@ func TestToolProxyCommand(t *testing.T) { |
| {[]string{"tool-proxy", "--socket", "s"}, 1, ""}, |
| {[]string{"tool-proxy", "--socket", filepath.Join(dir, "missing")}, 1, ""}, |
| } { |
| - var out, log bytes.Buffer |
| - if code := run(context.Background(), tc.args, strings.NewReader(ping), &out, &log, noEnv); code != tc.code || out.String() != tc.out || log.Len() != 0 { |
| - t.Fatalf("%q: exit %d, out %q, stderr %q", tc.args, code, out.String(), log.String()) |
| + code, out, stderr, report := runToolProxy(t, tc.args, ping, nil, "EQUIPO_ORIGIN=https://equipo.example.com") |
| + if code != tc.code || out != tc.out || stderr != "" || len(report.Getenv) != 0 { |
| + t.Fatalf("%q: exit %d, out %q, stderr %q, read %q", tc.args, code, out, stderr, report.Getenv) |
| } |
| } |
| } |
diff --git a/internal/runner/thin/toolproxy_transcript_test.go b/internal/runner/thin/toolproxy_transcript_test.go| index ab356456..bfebd968 100644 |
| --- a/internal/runner/thin/toolproxy_transcript_test.go |
| +++ b/internal/runner/thin/toolproxy_transcript_test.go |
| @@ -3,6 +3,7 @@ package thin |
| import ( |
| "context" |
| "encoding/json" |
| + "fmt" |
| "io" |
| "os" |
| "path/filepath" |
| @@ -211,9 +212,20 @@ func readTranscript(t *testing.T) (header, body string) { |
| return header, text |
| } |
| -// sameTranscript fails t at the first line where got differs from want. |
| -func sameTranscript(t *testing.T, got, want string) { |
| +// fataler is what sameTranscript needs of a test, so its own test can |
| +// watch it fail. |
| +type fataler interface { |
| + Helper() |
| + Fatalf(format string, args ...any) |
| +} |
| + |
| +// sameTranscript fails t unless got is want byte for byte, naming the |
| +// first line that differs. |
| +func sameTranscript(t fataler, got, want string) { |
| t.Helper() |
| + if got == want { |
| + return |
| + } |
| g, w := strings.Split(got, "\n"), strings.Split(want, "\n") |
| for i := 0; i < len(g) || i < len(w); i++ { |
| var gl, wl string |
| @@ -223,8 +235,41 @@ func sameTranscript(t *testing.T, got, want string) { |
| if i < len(w) { |
| wl = w[i] |
| } |
| - if gl != wl { |
| - t.Fatalf("transcript line %d differs\n got %s\nwant %s", i+1, gl, wl) |
| + if gl != wl || i >= len(g) || i >= len(w) { |
| + t.Fatalf("transcript line %d differs\n got %q\nwant %q", i+1, gl, wl) |
| + return |
| } |
| } |
| + t.Fatalf("transcript differs (%d bytes, want %d)", len(got), len(want)) |
| +} |
| + |
| +// fatalRecorder records sameTranscript's verdict. |
| +type fatalRecorder struct{ failed []string } |
| + |
| +func (r *fatalRecorder) Helper() {} |
| +func (r *fatalRecorder) Fatalf(format string, args ...any) { |
| + r.failed = append(r.failed, fmt.Sprintf(format, args...)) |
| +} |
| + |
| +// A line-based MCP client reads a reply only at its newline, so a |
| +// transcript differing only in a final newline, or in an empty last |
| +// line, differs. |
| +func TestSameTranscriptIsExact(t *testing.T) { |
| + want := "== s\n> {\"id\":1}\n< {\"id\":1}\n" |
| + for name, got := range map[string]string{ |
| + "missing final newline": strings.TrimSuffix(want, "\n"), |
| + "extra empty line": want + "\n", |
| + "changed reply": strings.Replace(want, "< {\"id\":1}", "< {\"id\":2}", 1), |
| + "missing reply": "== s\n> {\"id\":1}\n", |
| + } { |
| + r := &fatalRecorder{} |
| + sameTranscript(r, got, want) |
| + if len(r.failed) == 0 { |
| + t.Fatalf("%s: %q passed as %q", name, got, want) |
| + } |
| + } |
| + r := &fatalRecorder{} |
| + if sameTranscript(r, want, want); len(r.failed) != 0 { |
| + t.Fatalf("an equal transcript failed: %v", r.failed) |
| + } |
| } |
diff --git a/internal/runner/tool_proxy_mac.go b/internal/runner/tool_proxy_mac.go| index a8aa5fef..551598f2 100644 |
| --- a/internal/runner/tool_proxy_mac.go |
| +++ b/internal/runner/tool_proxy_mac.go |
| @@ -34,6 +34,10 @@ const ( |
| macReplyFrame = 4 << 20 |
| ) |
| +// macDial dials the step socket once every check has passed. Tests |
| +// stand in to see that a refusal never reaches it. |
| +var macDial = (&net.Dialer{}).DialContext |
| + |
| // macListenerUID is the user that holds the step socket's other end |
| // (LOCAL_PEERCRED on a Mac). Tests stand in. |
| var macListenerUID = func(c *net.UnixConn) (uint32, error) { |
| @@ -71,7 +75,7 @@ func ServeDarwinToolProxy(ctx context.Context, input io.Reader, output io.Writer |
| // No /proc/self/fd here: the path is dialled as checked. Only this |
| // user can change what lies under a directory it alone may enter, |
| // and the listener's credential is checked before anything is sent. |
| - conn, err := (&net.Dialer{}).DialContext(ctx, "unix", path) |
| + conn, err := macDial(ctx, "unix", path) |
| if err != nil { |
| return err |
| } |
diff --git a/internal/runner/tool_proxy_mac_test.go b/internal/runner/tool_proxy_mac_test.go| index 9bfc80f3..db155d8e 100644 |
| --- a/internal/runner/tool_proxy_mac_test.go |
| +++ b/internal/runner/tool_proxy_mac_test.go |
| @@ -228,14 +228,42 @@ func TestDarwinToolProxyRefusesPaths(t *testing.T) { |
| if err := os.Chmod(loose, 0666); err != nil { |
| t.Fatal(err) |
| } |
| - // A real socket one byte past a Mac's sun_path (Linux takes it). |
| - longDir := dir + "/" + strings.Repeat("d", provider.MaxDarwinSocketPath-len(dir)-len("/equipo.sock")) |
| + // The longest path a Mac takes: a socket at exactly 103 bytes serves. |
| + edgeDir := dir + "/" + strings.Repeat("d", provider.MaxDarwinSocketPath-len(dir)-1-len("/equipo.sock")) |
| + if err := os.Mkdir(edgeDir, 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + edge, _ := echoServer(t, edgeDir, `{"jsonrpc":"2.0","id":1,"result":{}}`) |
| + if len(edge) != provider.MaxDarwinSocketPath { |
| + t.Fatalf("the edge socket is %d bytes", len(edge)) |
| + } |
| + out.Reset() |
| + if err := ServeDarwinToolProxy(context.Background(), strings.NewReader(ping), &out, edge); err != nil || out.Len() == 0 { |
| + t.Fatalf("a socket of %d bytes: %v %q", len(edge), err, out.String()) |
| + } |
| + // A real socket one byte longer. A Mac cannot bind that path by |
| + // name, so it is bound by a relative name from inside its directory |
| + // (either system takes that); the endpoint must refuse it by length. |
| + longDir := dir + "/" + strings.Repeat("e", provider.MaxDarwinSocketPath-len(dir)-len("/equipo.sock")) |
| if err := os.Mkdir(longDir, 0700); err != nil { |
| t.Fatal(err) |
| } |
| - long, longGot := echoServer(t, longDir, `{"jsonrpc":"2.0","id":1,"result":{}}`) |
| - if len(long) != provider.MaxDarwinSocketPath+1 { |
| - t.Fatalf("the long socket is %d bytes", len(long)) |
| + t.Chdir(longDir) |
| + ll, err := net.ListenUnix("unix", &net.UnixAddr{Name: "equipo.sock", Net: "unix"}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer ll.Close() |
| + long := filepath.Join(longDir, "equipo.sock") |
| + if err := os.Chmod(long, 0600); err != nil || len(long) != provider.MaxDarwinSocketPath+1 { |
| + t.Fatalf("the long socket %s (%d bytes): %v", long, len(long), err) |
| + } |
| + // Every refusal comes before a dial. |
| + dials := 0 |
| + defer func(f func(context.Context, string, string) (net.Conn, error)) { macDial = f }(macDial) |
| + macDial = func(ctx context.Context, network, address string) (net.Conn, error) { |
| + dials++ |
| + return (&net.Dialer{}).DialContext(ctx, network, address) |
| } |
| for name, p := range map[string]string{ |
| "relative": "t/equipo.sock", |
| @@ -254,10 +282,14 @@ func TestDarwinToolProxyRefusesPaths(t *testing.T) { |
| ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| err := ServeDarwinToolProxy(ctx, strings.NewReader(ping), &out, p) |
| cancel() |
| - if err == nil || out.Len() != 0 || got.Len() != before || openGot.Len() != 0 || looseGot.Len() != 0 || longGot.Len() != 0 { |
| - t.Fatalf("%s %s: %v, %q out", name, p, err, out.String()) |
| + if err == nil || out.Len() != 0 || got.Len() != before || openGot.Len() != 0 || looseGot.Len() != 0 || dials != 0 { |
| + t.Fatalf("%s %s: %v, %q out, %d dials", name, p, err, out.String(), dials) |
| } |
| } |
| + // The seam is the endpoint's dial: the private socket goes through it. |
| + if err := ServeDarwinToolProxy(context.Background(), strings.NewReader(ping), &out, path); err != nil || dials != 1 { |
| + t.Fatalf("the private socket: %v, %d dials", err, dials) |
| + } |
| } |
| // The endpoint checks who listens before it sends anything: a step |