Files
coder/coderd/x/chatfiles/workspace.go
T
Michael Suchacz 7c74fa3548 feat: add workspace file upload agent API (#27078)
## What

Adds a dedicated workspace-agent endpoint, `POST
/api/v0/upload-chat-file`, that streams a request body into the chat's
upload directory `~/.coder/chats/<chat-id>/files/<name>`, plus the SDK
plumbing to call it:

- `coderd/x/chatfiles`: upload-name sanitization
(`SanitizeWorkspaceUploadName`), collision suffixes (compound extensions
such as `.tar.gz` stay intact: `archive_2.tar.gz`), and the shared
`~/.coder/chats/<id>/files` path layout.
- `agent/agentfiles`: the HTTP handler. On Linux/macOS the directory
chain is opened with `openat(O_NOFOLLOW)`, so symlinked path components
are rejected. The body streams into a hidden `.<name>.upload-<random>`
temp file and is published under its final name only when complete, via
`linkat` over the `_2`, `_3`, ... collision sequence, so existing files
are never overwritten and a half-written file is never visible under its
final name. Filesystems without hard links fall back to an exclusive
placeholder plus `renameat`. Other platforms use an afero-based fallback
with the same semantics. Stale temp files older than an hour are swept
before each upload, and failures are logged with a structured warning.
- `codersdk/workspacesdk`: `AgentConn.UploadChatFile` + regenerated
`agentconnmock`.

## Why

Coder Agents chat can attach images/text/PDF to prompts, but arbitrary
files (zips, datasets, binaries) have nowhere to land where the agent's
tools can act on them. This is the agent-side foundation; the coderd
route that fronts it is stacked on top.

Design notes:

- There is deliberately no size cap; the resource profile matches
existing SSH/WebSocket proxying, and client cancellation aborts the
stream (a failed write removes the temp file, so no partial files
remain). Because the agent HTTP server uses fixed 20 s read/write
timeouts, the handler pushes both deadlines forward on every received
chunk, so a slow upload only fails after a 20 s stall. If the connection
does not support deadline changes, the agent logs one warning per
request.
- This endpoint is not gated by `CODER_AGENT_BLOCK_FILE_TRANSFER`. That
flag targets SSH-transport file tools (scp/rsync/sftp). Chat uploads are
an agent API capability like `write-file` and `edit-files`, which
already bypass the flag; the interaction is documented in the FAQ.

## How tested

- `go test ./agent/agentfiles ./coderd/x/chatfiles
./codersdk/workspacesdk` (unit + umask/symlink/collision integration
tests).
- UAT on a dogfood-style deployment (2026-09-21): uploads longer than 20
s went from 502 to 201 once the workspace agent was restarted on this
build; a coderd restart alone keeps the old agent running, so the
per-chunk deadline extension needs the agent rolled (older agents get
the 409 "Restart the workspace" from #27079).

Part of CODAGT-211. Stack: agent API (this PR) -> coderd endpoint -> UI.

> This PR was authored by Mux (AI) working on Mike's behalf.
2026-09-25 09:05:51 +02:00

144 lines
4.1 KiB
Go

package chatfiles
import (
"path"
"path/filepath"
"strconv"
"strings"
"unicode"
"golang.org/x/xerrors"
)
const (
// MaxWorkspaceUploadFileNameBytes matches the stored attachment name limit.
MaxWorkspaceUploadFileNameBytes = MaxStoredFileNameBytes
// WorkspaceChatsDir is the directory under the agent's home directory
// that holds per-chat coder artifacts.
WorkspaceChatsDir = ".coder/chats"
// WorkspaceUploadFilesSubdir is the subdirectory of a chat's
// workspace folder that holds uploaded files.
WorkspaceUploadFilesSubdir = "files"
)
// ErrWorkspaceUploadNameRequired indicates that a workspace upload name
// is empty after sanitization.
var ErrWorkspaceUploadNameRequired = xerrors.New("workspace upload file name is required")
// SanitizeWorkspaceUploadName extracts a basename, normalizes unsafe
// whitespace, control characters, and Windows-invalid punctuation to
// underscores, strips edge dots, and truncates the result to
// MaxWorkspaceUploadFileNameBytes. It returns
// ErrWorkspaceUploadNameRequired when no safe name remains.
func SanitizeWorkspaceUploadName(name string) (string, error) {
name = strings.ReplaceAll(strings.TrimSpace(name), `\`, "/")
name = path.Base(name)
if name == "." || name == "/" {
return "", ErrWorkspaceUploadNameRequired
}
name = replaceUnsafeWorkspaceUploadRunes(name)
name = strings.Trim(name, ".")
name = truncateUTF8Bytes(name, MaxWorkspaceUploadFileNameBytes)
name = strings.Trim(name, ".")
if name == "" || name == "_" || isWindowsReservedUploadName(name) {
return "", ErrWorkspaceUploadNameRequired
}
return name, nil
}
func replaceUnsafeWorkspaceUploadRunes(name string) string {
var b strings.Builder
lastUnsafe := false
for _, r := range name {
if isUnsafeWorkspaceUploadRune(r) {
if !lastUnsafe {
_, _ = b.WriteString("_")
lastUnsafe = true
}
continue
}
_, _ = b.WriteRune(r)
lastUnsafe = false
}
return b.String()
}
func isUnsafeWorkspaceUploadRune(r rune) bool {
if unicode.IsSpace(r) || unicode.IsControl(r) || unicode.In(r, unicode.Cf) {
return true
}
switch r {
case '<', '>', ':', '"', '|', '?', '*':
return true
default:
return false
}
}
func isWindowsReservedUploadName(name string) bool {
stem, _, _ := strings.Cut(name, ".")
stem = strings.ToUpper(stem)
if stem == "CON" || stem == "PRN" || stem == "AUX" || stem == "NUL" {
return true
}
runes := []rune(stem)
if len(runes) != 4 {
return false
}
suffix := runes[3]
if !isWindowsReservedDeviceSuffix(suffix) {
return false
}
return strings.HasPrefix(stem, "COM") || strings.HasPrefix(stem, "LPT")
}
func isWindowsReservedDeviceSuffix(suffix rune) bool {
return (suffix >= '1' && suffix <= '9') || suffix == '¹' || suffix == '²' || suffix == '³'
}
// WorkspaceChatDir returns the per-chat directory under the agent home directory.
func WorkspaceChatDir(homeDir, chatID string) string {
return filepath.Join(homeDir, WorkspaceChatsDir, chatID)
}
// WorkspaceUploadDir returns the per-chat workspace upload directory.
func WorkspaceUploadDir(homeDir, chatID string) string {
return filepath.Join(WorkspaceChatDir(homeDir, chatID), WorkspaceUploadFilesSubdir)
}
// compoundUploadExts are multi-dot extensions kept intact when adding a
// collision suffix, so a second archive.tar.gz becomes archive_2.tar.gz.
var compoundUploadExts = []string{".tar.gz", ".tar.bz2", ".tar.xz", ".tar.zst", ".d.ts", ".min.js"}
func uploadNameExt(name string) string {
for _, ext := range compoundUploadExts {
if len(name) >= len(ext) && strings.EqualFold(name[len(name)-len(ext):], ext) {
return name[len(name)-len(ext):]
}
}
return path.Ext(name)
}
// AddCollisionSuffix inserts a `_<n>` suffix before the extension when n > 1.
func AddCollisionSuffix(name string, n int) string {
if n <= 1 {
return name
}
suffix := "_" + strconv.Itoa(n)
ext := uploadNameExt(name)
stem := strings.TrimSuffix(name, ext)
if stem == "" {
stem = name
ext = ""
}
stemBytes := MaxWorkspaceUploadFileNameBytes - len(suffix) - len(ext)
if stemBytes <= 0 {
return truncateUTF8Bytes(name, MaxWorkspaceUploadFileNameBytes-len(suffix)) + suffix
}
stem = truncateUTF8Bytes(stem, stemBytes)
return stem + suffix + ext
}