Files
Michael Suchacz 70b1e4716e fix(coderd/x/chatd/chattool)!: fail closed when the plan path cannot be resolved (#30983)
Removes the 404 bypass from the plan-turn symlink guard in `write_file`
and `edit_files`. `ensurePlanPathResolvesToItself` treated a 404 from
the agent's `resolve-path` endpoint as success, so a plan turn on an
agent without that endpoint wrote the plan file with no symlink check.
Now every `ResolvePath` error fails the tool call. The PR deletes the
status predicate and its `net/http` import, the two tests that expected
a write after a 404, and the test-only `statusError` file. The PR adds
77 lines and removes 147.

**Behavior change, approved by Mike:** an agent too old to serve the
`resolve-path` endpoint can no longer write plan files during plan
turns. The tool returns `resolve plan path: ...` instead. The endpoint
arrived with plan mode in #24236. Coder does not support mixed coderd
and agent versions, and upgrades happen in maintenance windows.

**Why it is safe:** the agent's `resolve-path` handler never answers
404. It returns 400 for a relative path, 403 for a permission error, and
500 for other errors, and it resolves a path whose last components do
not exist yet, such as a new plan file. So a 404 means that the route is
missing.

- In each tool's tests, the symlink case is now a table with three more
rows: a 404 and a 500 built with `codersdk.NewError`, and a connection
error without a status code. No row expects a write, and the strict
gomock controller fails on any `WriteFile` or `EditFiles` call.
- With the old resolver (`go test -overlay`), the 404 row fails for both
tools.
- A mutant that ignores every `ResolvePath` error fails all three error
rows for both tools. On `main`, the whole `chattool` package passes with
that mutant.
- A mutant that ignores only errors without a status code, such as a
dropped connection, fails the connection error row for both tools.
Without that row, both tools' tests pass with it.
- The success, shared plan path, and mixed-path tests do not change and
pass.

**Review focus:** the behavior change. Only `write_file` and
`edit_files` call the guard. `propose_plan` does not call `ResolvePath`,
and this PR does not change it.

Validation: `go test ./coderd/x/chatd/chattool -count=1` passes, `go vet
./coderd/x/chatd/chattool` is clean, and the pre-commit hook passed.

> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
2026-10-27 16:16:30 +01:00

435 lines
14 KiB
Go

package chattool_test
import (
"context"
"io"
"net/http"
"strings"
"testing"
"charm.land/fantasy"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/workspacesdk"
"github.com/coder/coder/v2/codersdk/workspacesdk/agentconnmock"
)
func TestWriteFile(t *testing.T) {
t.Parallel()
t.Run("PlanTurnRejectsNonPlanPath", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockConn := agentconnmock.NewMockAgentConn(ctrl)
planPath := "/home/coder/.coder/plans/PLAN-test-uuid.md"
getWorkspaceConnCalled := false
tool := chattool.WriteFile(chattool.WriteFileOptions{
GetWorkspaceConn: func(context.Context) (workspacesdk.AgentConn, error) {
getWorkspaceConnCalled = true
return mockConn, nil
},
ResolvePlanPath: func(context.Context) (string, string, error) {
return planPath, "/home/coder", nil
},
IsPlanTurn: true,
})
resp, err := tool.Run(context.Background(), fantasy.ToolCall{
ID: "call-1",
Name: "write_file",
Input: `{"path":"/home/coder/README.md","content":"# Plan"}`,
})
require.NoError(t, err)
assert.True(t, resp.IsError)
assert.Equal(t, "during plan turns, write_file is restricted to "+planPath, resp.Content)
assert.False(t, getWorkspaceConnCalled)
})
t.Run("PlanTurnAllowsResolvedPlanPath", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockConn := agentconnmock.NewMockAgentConn(ctrl)
planPath := "/home/coder/.coder/plans/PLAN-test-uuid.md"
resolvePlanPathCalls := 0
mockConn.EXPECT().ResolvePath(gomock.Any(), planPath).Return(planPath, nil)
mockConn.EXPECT().
WriteFile(gomock.Any(), planPath, gomock.Any()).
DoAndReturn(func(_ context.Context, path string, reader io.Reader) error {
data, err := io.ReadAll(reader)
require.NoError(t, err)
require.Equal(t, planPath, path)
require.Equal(t, "# Plan", string(data))
return nil
})
tool := chattool.WriteFile(chattool.WriteFileOptions{
GetWorkspaceConn: func(context.Context) (workspacesdk.AgentConn, error) {
return mockConn, nil
},
ResolvePlanPath: func(context.Context) (string, string, error) {
resolvePlanPathCalls++
return planPath, "/home/coder", nil
},
IsPlanTurn: true,
})
resp, err := tool.Run(context.Background(), fantasy.ToolCall{
ID: "call-1",
Name: "write_file",
Input: `{"path":"` + planPath + `","content":"# Plan"}`,
})
require.NoError(t, err)
assert.False(t, resp.IsError)
assert.Equal(t, 1, resolvePlanPathCalls)
assert.Equal(t, `{"ok":true}`, strings.TrimSpace(resp.Content))
})
t.Run("PlanTurnRejectsPlanPathNotResolvingToItself", func(t *testing.T) {
t.Parallel()
planPath := "/home/coder/.coder/plans/PLAN-test-uuid.md"
notFound := codersdk.NewError(http.StatusNotFound, codersdk.Response{Message: "Route not found."})
internal := codersdk.NewError(http.StatusInternalServerError, codersdk.Response{Message: "Internal error."})
tests := []struct {
name string
resolved string
err error
want string
}{
{name: "Symlinked", resolved: "/home/coder/README.md", want: "the chat-specific plan path /home/coder/.coder/plans/PLAN-test-uuid.md resolves to /home/coder/README.md; symlinked plan paths are not allowed during plan turns"},
{name: "ResolvePathNotFound", err: notFound, want: "resolve plan path: " + notFound.Error()},
{name: "ResolvePathError", err: internal, want: "resolve plan path: " + internal.Error()},
{name: "ResolvePathTransportError", err: xerrors.New("do request: connection refused"), want: "resolve plan path: do request: connection refused"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockConn := agentconnmock.NewMockAgentConn(ctrl)
mockConn.EXPECT().ResolvePath(gomock.Any(), planPath).Return(tc.resolved, tc.err)
tool := chattool.WriteFile(chattool.WriteFileOptions{
GetWorkspaceConn: func(context.Context) (workspacesdk.AgentConn, error) {
return mockConn, nil
},
ResolvePlanPath: func(context.Context) (string, string, error) {
return planPath, "/home/coder", nil
},
IsPlanTurn: true,
})
resp, err := tool.Run(context.Background(), fantasy.ToolCall{
ID: "call-1",
Name: "write_file",
Input: `{"path":"` + planPath + `","content":"# Plan"}`,
})
require.NoError(t, err)
assert.True(t, resp.IsError)
assert.Equal(t, tc.want, resp.Content)
})
}
})
t.Run("RejectsHomeRootPlanVariantsWhenResolvePlanPathIsConfigured", func(t *testing.T) {
t.Parallel()
tests := []struct {
name string
requested string
home string
}{
{
name: "ExactLegacyPath",
requested: chattool.LegacySharedPlanPath,
home: "/home/coder",
},
{
name: "LowercasePlanAtHomeRoot",
requested: "/home/coder/plan.md",
home: "/home/coder",
},
{
name: "MixedCasePlanAtHomeRoot",
requested: "/home/coder/Plan.md",
home: "/home/coder",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockConn := agentconnmock.NewMockAgentConn(ctrl)
tool := chattool.WriteFile(chattool.WriteFileOptions{
GetWorkspaceConn: func(context.Context) (workspacesdk.AgentConn, error) {
return mockConn, nil
},
ResolvePlanPath: func(context.Context) (string, string, error) {
return "/home/coder/.coder/plans/PLAN-chat.md", testCase.home, nil
},
})
resp, err := tool.Run(context.Background(), fantasy.ToolCall{
ID: "call-1",
Name: "write_file",
Input: `{"path":"` + testCase.requested + `","content":"# Plan"}`,
})
require.NoError(t, err)
assert.True(t, resp.IsError)
assert.Equal(
t,
sharedPlanPathResolvedMessage(
testCase.requested,
"/home/coder/.coder/plans/PLAN-chat.md",
),
resp.Content,
)
})
}
})
t.Run("RejectsRelativePlanPathsWhenResolvePlanPathIsConfigured", func(t *testing.T) {
t.Parallel()
tests := []struct {
name string
requested string
}{
{
name: "PlainRelativePath",
requested: "plan.md",
},
{
name: "DotSlashRelativePath",
requested: "./plan.md",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockConn := agentconnmock.NewMockAgentConn(ctrl)
resolvePlanPathCalled := false
tool := chattool.WriteFile(chattool.WriteFileOptions{
GetWorkspaceConn: func(context.Context) (workspacesdk.AgentConn, error) {
return mockConn, nil
},
ResolvePlanPath: func(context.Context) (string, string, error) {
resolvePlanPathCalled = true
return "/home/coder/.coder/plans/PLAN-chat.md", "/home/coder", nil
},
})
resp, err := tool.Run(context.Background(), fantasy.ToolCall{
ID: "call-1",
Name: "write_file",
Input: `{"path":"` + testCase.requested + `","content":"# Plan"}`,
})
require.NoError(t, err)
assert.True(t, resp.IsError)
assert.False(t, resolvePlanPathCalled)
assert.Equal(t, relativePlanPathMessage(), resp.Content)
})
}
})
t.Run("RejectsSharedPlanPathWhenResolverFails", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockConn := agentconnmock.NewMockAgentConn(ctrl)
tool := chattool.WriteFile(chattool.WriteFileOptions{
GetWorkspaceConn: func(context.Context) (workspacesdk.AgentConn, error) {
return mockConn, nil
},
ResolvePlanPath: func(context.Context) (string, string, error) {
return "", "", xerrors.New("workspace unavailable")
},
})
resp, err := tool.Run(context.Background(), fantasy.ToolCall{
ID: "call-1",
Name: "write_file",
Input: `{"path":"/home/coder/plan.md","content":"# Plan"}`,
})
require.NoError(t, err)
assert.True(t, resp.IsError)
assert.Equal(t, planPathVerificationMessage("/home/coder/plan.md"), resp.Content)
})
t.Run("PerChatPlanPathIsAllowed", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockConn := agentconnmock.NewMockAgentConn(ctrl)
chatPlanPath := "/home/coder/.coder/plans/PLAN-123e4567-e89b-12d3-a456-426614174000.md"
mockConn.EXPECT().
WriteFile(gomock.Any(), chatPlanPath, gomock.Any()).
DoAndReturn(func(_ context.Context, path string, reader io.Reader) error {
data, err := io.ReadAll(reader)
require.NoError(t, err)
require.Equal(t, chatPlanPath, path)
require.Equal(t, "# Plan", string(data))
return nil
})
resolvePlanPathCalled := false
tool := chattool.WriteFile(chattool.WriteFileOptions{
GetWorkspaceConn: func(context.Context) (workspacesdk.AgentConn, error) {
return mockConn, nil
},
ResolvePlanPath: func(context.Context) (string, string, error) {
resolvePlanPathCalled = true
return chatPlanPath, "/home/coder", nil
},
})
resp, err := tool.Run(context.Background(), fantasy.ToolCall{
ID: "call-1",
Name: "write_file",
Input: `{"path":"` + chatPlanPath + `","content":"# Plan"}`,
})
require.NoError(t, err)
assert.False(t, resp.IsError)
assert.False(t, resolvePlanPathCalled)
assert.Equal(t, `{"ok":true}`, strings.TrimSpace(resp.Content))
})
t.Run("NestedPlanPathAllowedWhenResolverFails", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockConn := agentconnmock.NewMockAgentConn(ctrl)
mockConn.EXPECT().
WriteFile(gomock.Any(), "/home/coder/myproject/plan.md", gomock.Any()).
DoAndReturn(func(_ context.Context, path string, reader io.Reader) error {
data, err := io.ReadAll(reader)
require.NoError(t, err)
require.Equal(t, "/home/coder/myproject/plan.md", path)
require.Equal(t, "# Plan", string(data))
return nil
})
tool := chattool.WriteFile(chattool.WriteFileOptions{
GetWorkspaceConn: func(context.Context) (workspacesdk.AgentConn, error) {
return mockConn, nil
},
ResolvePlanPath: func(context.Context) (string, string, error) {
return "", "", xerrors.New("workspace unavailable")
},
})
resp, err := tool.Run(context.Background(), fantasy.ToolCall{
ID: "call-1",
Name: "write_file",
Input: `{"path":"/home/coder/myproject/plan.md","content":"# Plan"}`,
})
require.NoError(t, err)
assert.False(t, resp.IsError)
assert.Equal(t, `{"ok":true}`, strings.TrimSpace(resp.Content))
})
t.Run("NestedPlanPathUnderHomeIsAllowed", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockConn := agentconnmock.NewMockAgentConn(ctrl)
mockConn.EXPECT().
WriteFile(gomock.Any(), "/home/coder/myproject/plan.md", gomock.Any()).
DoAndReturn(func(_ context.Context, path string, reader io.Reader) error {
data, err := io.ReadAll(reader)
require.NoError(t, err)
require.Equal(t, "/home/coder/myproject/plan.md", path)
require.Equal(t, "# Plan", string(data))
return nil
})
planPathCalled := false
tool := chattool.WriteFile(chattool.WriteFileOptions{
GetWorkspaceConn: func(context.Context) (workspacesdk.AgentConn, error) {
return mockConn, nil
},
ResolvePlanPath: func(context.Context) (string, string, error) {
planPathCalled = true
return "/home/coder/.coder/plans/PLAN-chat.md", "/home/coder", nil
},
})
resp, err := tool.Run(context.Background(), fantasy.ToolCall{
ID: "call-1",
Name: "write_file",
Input: `{"path":"/home/coder/myproject/plan.md","content":"# Plan"}`,
})
require.NoError(t, err)
assert.False(t, resp.IsError)
assert.True(t, planPathCalled)
assert.Equal(t, `{"ok":true}`, strings.TrimSpace(resp.Content))
})
t.Run("AllowsNonSharedPath", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockConn := agentconnmock.NewMockAgentConn(ctrl)
mockConn.EXPECT().
WriteFile(gomock.Any(), "/home/dev/my-plan.md", gomock.Any()).
DoAndReturn(func(_ context.Context, path string, reader io.Reader) error {
data, err := io.ReadAll(reader)
require.NoError(t, err)
require.Equal(t, "/home/dev/my-plan.md", path)
require.Equal(t, "# Plan", string(data))
return nil
})
resolvePlanPathCalled := false
tool := chattool.WriteFile(chattool.WriteFileOptions{
GetWorkspaceConn: func(context.Context) (workspacesdk.AgentConn, error) {
return mockConn, nil
},
ResolvePlanPath: func(context.Context) (string, string, error) {
resolvePlanPathCalled = true
return "", "", xerrors.New("should not be called")
},
})
resp, err := tool.Run(context.Background(), fantasy.ToolCall{
ID: "call-1",
Name: "write_file",
Input: `{"path":"/home/dev/my-plan.md","content":"# Plan"}`,
})
require.NoError(t, err)
assert.False(t, resp.IsError)
assert.False(t, resolvePlanPathCalled)
assert.Equal(t, `{"ok":true}`, strings.TrimSpace(resp.Content))
})
t.Run("AllowsSharedPlanPathWhenResolvePlanPathIsNil", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockConn := agentconnmock.NewMockAgentConn(ctrl)
mockConn.EXPECT().
WriteFile(gomock.Any(), chattool.LegacySharedPlanPath, gomock.Any()).
DoAndReturn(func(_ context.Context, _ string, reader io.Reader) error {
data, err := io.ReadAll(reader)
require.NoError(t, err)
require.Equal(t, "# Plan", string(data))
return nil
})
tool := chattool.WriteFile(chattool.WriteFileOptions{
GetWorkspaceConn: func(context.Context) (workspacesdk.AgentConn, error) {
return mockConn, nil
},
})
resp, err := tool.Run(context.Background(), fantasy.ToolCall{
ID: "call-1",
Name: "write_file",
Input: `{"path":"` + chattool.LegacySharedPlanPath + `","content":"# Plan"}`,
})
require.NoError(t, err)
assert.False(t, resp.IsError)
})
}