Repository navigation
Commit ec8e836
fix(sandbox): scope exclusive Add File creation to UnixLocal (#4893)
* fix(sandbox): reject apply_patch create_file on an existing file
Add File is documented to the model as creating a new file, and the
delete and update operations both enforce their existing-file
precondition. create_file enforced nothing, so an Add File operation
aimed at a path that already existed overwrote it and reported
"Created <path>", losing the previous contents with no error.
Check that the destination is absent before writing, mirroring the
existing _ensure_exists precondition used by delete_file.
* fix(sandbox): keep the create precondition probe out of failed read spans
The absence check reached SandboxSession.read(), which records a failed
sandbox.read child span when the file is missing. Missing is the success
case for a create, so every successful Add File looked like it contained
a failed sandbox operation.
Use the existing _read_with_expected_span_errors helper, the same path
the skills capability already uses for an existence probe.
* test(sandbox): skip the create span test on Windows
The span assertion needs FilesystemTestSandboxSession, which is typed to
UnixLocalSandboxSessionState, and importing agents.sandbox.sandboxes.unix_local
raises ImportError on Windows by design. tests/conftest.py already
collect-ignores the other files that depend on it, but test_apply_patch.py
must stay collectible because its remaining tests are platform independent.
The unix-only imports stay inside the test body so collection does not
touch unix_local on Windows.
* fix(sandbox): claim the create_file name at the backend write boundary
The previous read-then-write check left a window: a concurrent sandbox
command could create the file after the probe, and the unconditional
write then destroyed that content. Reading also did not establish that a
dangling symlink entry was absent, because the unix_local path policy
resolves symlinks and the write landed on the link target.
Add BaseSandboxSession.write_new_file(), which claims the target name
before the payload is written and raises FileExistsError when the name is
already taken. The shared implementation uses a shell noclobber
redirection, so the redirect itself is the O_EXCL attempt; UnixLocal
overrides it with os.open(O_CREAT|O_EXCL) for its direct path. Both
validate the parent through the normal policy, preserving grants and the
bound user, and leave the final component unresolved so a symlink at that
name is rejected rather than followed.
apply_patch create_file now uses it and drops the probe, so the tracing
workaround for the probe read is no longer needed. Existing files still
have to go through update_file.
* fix(sandbox): link a completed payload into place for create_file
Three problems with the previous commit.
SandboxSession did not forward write_new_file, so a session built by
SandboxClient fell back to the shared implementation and never reached
the UnixLocal os.open override. Forward it.
The shared implementation created an empty file and then wrote the
payload in a separate step, so a concurrent writer could be overwritten
and a failed upload left an empty file holding the name. Write the
payload under a staging name first, then claim the target with ln, which
fails when the name is taken. The content is complete before the name
exists, and a failed create leaves only the staging entry, which is
removed.
The script used ':' for the noclobber redirection. ':' is a POSIX special
builtin, so on dash a redirection failure ended the shell before the exit
mapping ran and a collision surfaced as a generic write error instead of
FileExistsError. ln is a regular command, and a test now runs the script
through sh, dash and bash so this cannot regress silently.
Also create local files with 0o666 so the process umask decides the final
mode, matching Path.open("wb") on the ordinary write path.
* fix(sandbox): claim the requested name, not its resolved target
WorkspaceEditor normalizes the destination before dispatching, and
UnixLocal resolves leaf symlinks, so create_file handed the primitive the
link target. Add File on a dangling link.txt created missing.txt and
reported success. Pass the unresolved path for create.
Stage the payload locally too, then os.link it into place. os.link fails
with EEXIST for a file, a directory or a dangling symlink, and a write
that fails partway now leaves only the staging entry instead of a file
holding the name.
Reject a name held by a directory in the shared script. Bare ln treats an
existing directory as a target directory and would have linked the
staging file inside it while reporting the directory as created.
Move the staging write inside the cleanup scope so a failed upload cannot
leak the staging entry, and create the parent as the bound user so a
fresh nested path is owned the way the previous write path owned it.
The new tests drive session.apply_patch() rather than the primitive, which
is the path that was actually broken.
* fix(sandbox): drop the login shell and narrow the create collision
Use sh -c instead of sh -lc for the exclusive create. This path runs for a
filesystem-only capability set, so it must not source shell startup files
that live in the workspace it is editing.
A parent that is a regular file makes mkdir raise FileExistsError, and the
broad handler reported that as a collision on the requested name, telling
the model to use update_file for a target that does not exist. Only the
os.link call can report a collision now; parent and staging failures are
wrapped as write errors.
* fix(sandbox): use a fixed-length staging basename
The staging name was derived from the destination, so it was always
longer than the destination itself. A filename that fits the filesystem's
component limit, and that the ordinary write path accepts, could then
fail to stage: a 254 character name raised WorkspaceArchiveWriteError
where a plain write succeeded before this branch.
The staging basename is now constant at 52 characters regardless of the
destination.
Reported by fscfede-beep in #4930.
* fix(sandbox): classify a visible collision before staging the payload
The staging write ran before the link script could classify the target, so
a target inside an executable but non-writable parent failed on the
staging write and the caller saw WorkspaceArchiveWriteError instead of the
ApplyPatchDiffError that points it at update_file. It also meant an
Add File onto an occupied name uploaded a payload that was then discarded.
Probe the target first in both paths, then keep the atomic claim to decide
real races. A creator that wins between the probe and the link still loses
the name, and its staging entry is still cleaned up.
Reported by Codex and by fscfede-beep in #4930.
* fix(sandbox): build exclusive create on the descriptor-relative file ops
#4931 gave UnixLocal a dir_fd plus O_NOFOLLOW file-ops layer and routed the
bound-user path through a Python worker running that same module. That is
the primitive this change was approximating, so use it instead.
write_new is the existing write with O_TRUNC replaced by O_EXCL, which
claims the name in the same syscall that creates the file and fails with
EEXIST when the name is taken by a file, a directory, or a dangling
symlink. The worker exits with a distinct status for an existing target so
the caller can report FileExistsError rather than a generic write error.
This deletes the machinery the previous approach needed: the shell script,
the staging file, the hard link, and the staging cleanup. With no hard link
there is nothing to fail on writable object-storage mounts or across owners
under fs.protected_hardlinks, and with no staging entry there is nothing for
concurrent work to replace. The shared base now documents what it actually
guarantees: it rejects an occupied target but is not atomic, and a backend
that can claim a name should override it.
test_parent_swap_after_validation_cannot_access_outside[patch] injected at
session.normalize_path, which the create path no longer calls, so its swap
stopped firing. Re-pointed the patch case at the file-ops authorize
boundary the create path does traverse, keeping the coverage rather than
letting it pass vacuously. Verified separately that a parent symlinked
outside the workspace is still refused, with the outside file untouched.
Co-Authored-By: Claude Opus 5 <[email protected]>
* fix(sandbox): keep supported parent symlinks and stop reading the target
Two problems with the previous commit.
Passing the whole path into the file ops unresolved made a supported
internal symlink parent fail. The ordinary write path resolves those safe
aliases, and test_safe_symlinks_grants_and_listing_paths_remain_supported
establishes the support, but a create through "internal -> real" opened
"internal" with O_NOFOLLOW | O_DIRECTORY and failed. Resolve the parent the
way write does and keep only the leaf name unresolved, so a dangling
symlink at the target name is still rejected without its target being
created.
The shared default probed the target with read(), which eagerly fetches the
whole payload on the remote backends that inherit it. An Add File aimed at
an existing large file would download it before reporting the collision,
and an existing file the bound user cannot read reported a read failure
instead of the collision. List the parent instead, which needs no payload
and no read permission on the target.
Both regressions have a test that fails on the unfixed source, including
one asserting the rejected create issues no read at all.
Co-Authored-By: Claude Opus 5 <[email protected]>
* fix(sandbox): probe the target directly instead of listing its parent
Listing the parent to decide a collision failed when the parent did not
exist. BaseSandboxSession.ls() raises ExecNonZeroError in that case, which
the probe did not catch, so a nested create such as newdir/file.txt aborted
before write() on every backend inheriting the default, while succeeding on
UnixLocal. Those backends' write paths create missing parents.
Use a bare existence test instead. It needs only execute permission on the
parent, never reads the target, and reports absent when the parent is
missing so the create still reaches write(). Only a positive result rejects;
any other probe outcome falls through rather than blocking a valid create.
The test is also more accurate than the listing: it sees a file inside an
executable but unreadable parent, which listing could not, so an existing
file there is no longer silently overwritten.
Validated the exact shipped script under sh, bash and dash for an existing
file, a dangling symlink, an absent name, a missing parent, and a file in a
0311 parent.
Co-Authored-By: Claude Opus 5 <[email protected]>
* fix(sandbox): fail closed on a failed probe and keep scripted creates working
Two problems with the previous commit.
The probe treated any unexpected status as "absent" so a valid create would
not be blocked. That was wrong: exit 0 already means absent, including when
the parent does not exist, so the permissive fallback only covered the case
where the probe itself did not run. On provider sessions whose write() uses
a separate upload API, the write then succeeds and overwrites an existing
target precisely when the precondition could not be checked. Only 0 may
proceed now; 13 stays the collision and every other status is a write error.
scripted_sandbox_session() broke. The inherited default reaches for exec,
and a script that configures only file steps hides exec, so a previously
valid mkdir-and-write script failed with AttributeError. Give the scripted
session an implementation that consumes the same mkdir and write pair, and
restore the explicit parent creation in the default so the observable call
sequence matches what the create path did before this branch.
Both have a test that fails on the unfixed source.
Co-Authored-By: Claude Opus 5 <[email protected]>
* fix(sandbox): keep scripted create calls on their normalized paths
The create path hands write_new_file an unresolved path on purpose, so a
symlink at the target name is rejected rather than followed. The scripted
session forwarded that path straight into its compatibility mkdir and write
pair, so a script using the documented match callback saw "." and
"notes.txt" where it previously saw the workspace paths, and failed with
SandboxCallMatcherError.
Normalize before emitting the pair. Verified against the previous commit:
with a custom manifest root the calls were "sub" and "sub/notes.txt" and
are now "/custom/root/sub" and "/custom/root/sub/notes.txt", matching what
this surface recorded before this branch.
The earlier test only asserted method names, which is why it could not catch
this. It now asserts the recorded paths too and fails on the unfixed source.
* fix(sandbox): narrow exclusive creates and clarify partial-write recovery
---------
Co-authored-by: Claude Opus 5 <[email protected]>
Co-authored-by: Justin Beckwith <[email protected]>1 parent 6492137 commit ec8e836
12 files changed
Lines changed: 675 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
130 | | - | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
131 | 135 | | |
132 | 136 | | |
133 | 137 | | |
| |||
186 | 190 | | |
187 | 191 | | |
188 | 192 | | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
189 | 211 | | |
190 | 212 | | |
191 | 213 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
506 | 506 | | |
507 | 507 | | |
508 | 508 | | |
| 509 | + | |
509 | 510 | | |
510 | 511 | | |
511 | | - | |
| 512 | + | |
512 | 513 | | |
513 | 514 | | |
514 | 515 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
29 | 37 | | |
30 | 38 | | |
31 | 39 | | |
| |||
118 | 126 | | |
119 | 127 | | |
120 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
121 | 157 | | |
122 | 158 | | |
123 | 159 | | |
| |||
209 | 245 | | |
210 | 246 | | |
211 | 247 | | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
212 | 256 | | |
213 | 257 | | |
214 | 258 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
113 | 119 | | |
114 | 120 | | |
115 | 121 | | |
| |||
1065 | 1071 | | |
1066 | 1072 | | |
1067 | 1073 | | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
1068 | 1141 | | |
1069 | 1142 | | |
1070 | 1143 | | |
| |||
1094 | 1167 | | |
1095 | 1168 | | |
1096 | 1169 | | |
1097 | | - | |
| 1170 | + | |
1098 | 1171 | | |
1099 | 1172 | | |
1100 | 1173 | | |
1101 | 1174 | | |
1102 | 1175 | | |
1103 | 1176 | | |
1104 | | - | |
| 1177 | + | |
1105 | 1178 | | |
1106 | 1179 | | |
1107 | 1180 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
997 | 997 | | |
998 | 998 | | |
999 | 999 | | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
1000 | 1017 | | |
1001 | 1018 | | |
1002 | 1019 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
684 | 684 | | |
685 | 685 | | |
686 | 686 | | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
687 | 700 | | |
688 | 701 | | |
689 | 702 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
59 | 73 | | |
60 | 74 | | |
61 | 75 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| 15 | + | |
14 | 16 | | |
| 17 | + | |
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
| |||
411 | 414 | | |
412 | 415 | | |
413 | 416 | | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
0 commit comments