Files
Michael SuchaczandEthan Dickson f10eabbb0c fix: confine read_skill_file reads to the skill directory (#31462)
> Xum opened this PR for Mike.

`read_skill_file` checked the requested path lexically, then read
`<skill dir>/<path>` through the agent's general file reader, which
follows symlinks. A supporting file that is a symlink to a file outside
the skill directory returned that file's contents to the model.

The agent's `read-file` endpoint now accepts an optional `root`
parameter. With it, the agent opens `root` with Go's `os.OpenRoot`,
refuses it if `root` itself is a symlink, and opens the file with
`Root.Open`, which follows symlinks only while they stay beneath `root`
and refuses absolute symlinks. `os.Root` opens one path component at a
time relative to the directory it already holds and resolves each
symlink itself, so a file swapped for a symlink during the walk cannot
slip through. `codersdk` adds `AgentConn.ReadFileInRoot`, and
`read_skill_file` reads supporting files through it. Reads without
`root` are unchanged.

- Relative symlinks that never leave the skill directory still work.
Absolute symlinks, and links that climb out of the directory, are
refused. Refusal messages never name the link target.
- The agent refuses a `root` that is itself a symlink, because
`os.OpenRoot` follows it: a skill directory replaced with a symlink to
`/etc` would otherwise become `/etc`. It compares the opened directory
with an `Lstat` of `root`, so a swap between the two calls is also
refused. Symlinks in the parent directories of `root` still work.
- Agents older than this change reject the `root` parameter, so
`read_skill_file` fails closed in a workspace until it restarts with the
new agent.
- Not in this PR: the agent's `resolve-path` endpoint joins a relative
link target to the parent path before resolving the parent's own
symlinks, so it can return the wrong file. Plan path checks still use
it; skill reads no longer do.

Fixes CODAGT-1496

## Testing

- New tests: `TestReadFileInRoot` (handler, real symlinks),
`TestReadFileInRootSwap` (swaps `root` for a symlink while reads run),
`TestReadFileInRootRegularDirectory` (no symlinks, so it also runs on
Windows), `TestAgentConnReadFileInRoot` (SDK over tailnet against the
real handler), `TestReadSkillFileTool/AgentRefusalIsToolError`. The
refusal tests fail when the protection is removed (plain open instead of
`os.Root`, no check that `root` is not a symlink, SDK joining root and
path, or the tool dropping the agent error). Without the `root` check,
`TestReadFileInRootSwap` returned outside bytes in 45 of 500 reads.
- Remote dogfood UAT passed on 132c7bed66 with a real model: allowed
reads (including an in-skill directory symlink followed by `..`),
refused escapes with no outside bytes or target paths in errors, a
30-call symlink swap race with no outside bytes, and an old agent
failing closed. The merge base leaked `/etc/passwd` in the same setup.
Later commits change tests and docs, rewrite agent errors for the model,
and add the `root` symlink check. Unit tests cover those changes; the
UAT did not.
- Cleanup passes stayed inside the implementation diff: polish touched
only `skill_test.go`, and the test audit added only `agentconn_test.go`.
No new dependencies.

---------

Co-authored-by: Ethan Dickson <ethan@coder.com>
2026-10-29 10:18:54 +01:00
..