Files
coder/coderd/x/chatd/chattool/planpathresolve.go
T
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

43 lines
917 B
Go

package chattool
import (
"context"
"path"
"path/filepath"
"strings"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/codersdk/workspacesdk"
)
func ensurePlanPathResolvesToItself(
ctx context.Context,
conn workspacesdk.AgentConn,
planPath string,
) error {
if conn == nil {
return xerrors.New("workspace connection is required")
}
normalizedPlanPath := normalizeWorkspacePath(planPath)
resolvedPath, err := conn.ResolvePath(ctx, planPath)
if err != nil {
return xerrors.Errorf("resolve plan path: %w", err)
}
resolvedPath = normalizeWorkspacePath(resolvedPath)
if resolvedPath != normalizedPlanPath {
return xerrors.New(symlinkedPlanPathMessage(normalizedPlanPath, resolvedPath))
}
return nil
}
func normalizeWorkspacePath(pathString string) string {
pathString = strings.TrimSpace(pathString)
if pathString == "" {
return ""
}
return path.Clean(filepath.ToSlash(pathString))
}