pub fn check_native_write_target(
project_root: &Path,
path: &Path,
) -> Result<PathBuf, WriteTargetError>Expand description
Refuse any write target that is not genuinely beneath <project>/.trusty-code/.
Why: “native project writes stay beneath <project>/.trusty-code/” is only a
property if something enforces it, and a lexical starts_with does not: a
symlink at .trusty-code/agents -> ../../elsewhere satisfies it while every
write lands outside the tree.
Resolving the target alone is not enough either. The first version of this
guard resolved the target AND the config root the same way and compared them
to each other, so a .trusty-code that was ITSELF a symlink out of the
project moved the goalposts with the target and approved every write
(code-critic BLOCK, PR #6980). The root is therefore anchored to the project
FIRST, and only then does the target get compared against it.
What: three checks, in order.
- Lexical —
pathmust start withnative_config_dir, elseWriteTargetError::CrossProduct. - Root anchoring — when
<project>/.trusty-coderesolves, it must canonicalise to somewhere inside the canonicalisedproject_root, elseWriteTargetError::SymlinkEscape. Aproject_rootthat cannot itself be canonicalised fails CLOSED here: containment cannot be established, so the write is refused rather than assumed safe. Checks 1 and 2 live in [anchor_native_root], shared with the pinning path. - Target containment — the target’s nearest EXISTING ancestor (the target itself is usually absent, since we are about to create it) must resolve inside that same anchored root.
With nothing on the path existing yet, no symlink can be redirecting it and check 1 is the whole answer.
This decides MEMBERSHIP, and it is not how a write is authorised. Its
answer describes the tree as it was when it ran, so a caller that then writes
through the same path string can be overtaken by a rename (#7779). Writers do
not call this at all: write_dir::NativeWriteDir::open goes to
[anchor_native_root] directly and pins the very path it returned, so there
is no second resolution to be raced. Call this function only to classify a
path without writing to it — the import PLAN does.
Test: paths::tests::write_to_native_dir_is_allowed,
paths::tests::write_to_claude_dir_is_refused,
paths::tests::write_outside_project_is_refused,
paths::tests::symlinked_native_subdir_escape_is_refused,
paths::tests::symlinked_native_root_escape_is_refused.