Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(execd): abort on sync failure and keep upload metadata
JasonBuildAIaa73aaf81e24
diff --git a/components/execd/pkg/web/controller/filesystem_upload.go b/components/execd/pkg/web/controller/filesystem_upload.goindex 946c6bc17..1cedcbc33 100644--- a/components/execd/pkg/web/controller/filesystem_upload.go+++ b/components/execd/pkg/web/controller/filesystem_upload.go@@ -233,6 +233,12 @@ func writeUploadStream(target string, r io.Reader) *uploadError {tmp, err := createUploadTemp(filepath.Dir(resolvedTarget))if err != nil {+ // A directory that is only traversable (0555, or a 0666 file in it) was+ // writable in place before staging existed. Keep that upload working+ // through the legacy path; its tradeoff is documented on the route.+ if statErr == nil && errors.Is(err, fs.ErrPermission) {+ return writeUploadInPlace(resolvedTarget, r)+ }return newUploadError(http.StatusInternalServerError,model.ErrorCodeRuntimeError,@@ -249,23 +255,23 @@ func writeUploadStream(target string, r io.Reader) *uploadError {_ = os.Remove(tmpName)}()+ if _, err := io.Copy(tmp, r); err != nil {+ return uploadWriteError(resolvedTarget, err)+ }+if statErr == nil {- // An in-place write kept the metadata of the file it overwrote: carry- // the mode over, and the owner/group where the platform exposes them,- // so replacing the file does not change who can read or write it.- if err := tmp.Chmod(existing.Mode().Perm()); err != nil {- return uploadWriteError(resolvedTarget, err)- }+ // Apply metadata after the content: writing and chown both clear+ // setuid/setgid, so doing this before io.Copy silently dropped them.if err := copyFileOwnership(tmp, existing); err != nil {log.Warn("upload: preserve owner/group of %s: %v", resolvedTarget, err)}+ if err := tmp.Chmod(uploadDestinationMode(existing.Mode())); err != nil {+ return uploadWriteError(resolvedTarget, err)+ }}- if _, err := io.Copy(tmp, r); err != nil {- return uploadWriteError(resolvedTarget, err)- }if err := tmp.Sync(); err != nil {- log.Error("upload: sync target file: %v", err)+ return uploadWriteError(resolvedTarget, err)}if err := tmp.Close(); err != nil {return uploadWriteError(resolvedTarget, err)@@ -288,6 +294,35 @@ func writeUploadStream(target string, r io.Reader) *uploadError {return nil}+// uploadDestinationMode keeps the permission bits an in-place write preserved,+// including setuid/setgid/sticky: a rename replaces the inode, so the staged+// file has to carry them explicitly.+func uploadDestinationMode(mode os.FileMode) os.FileMode {+ return mode & (os.ModePerm | os.ModeSetuid | os.ModeSetgid | os.ModeSticky)+}++// writeUploadInPlace is the legacy path for a destination whose directory is+// not writable but whose file is. Unlike the staged path it truncates first, so+// a copy that fails partway loses the previous content.+func writeUploadInPlace(target string, r io.Reader) *uploadError {+ file, err := os.OpenFile(target, os.O_WRONLY|os.O_TRUNC, 0)+ if err != nil {+ return newUploadError(+ http.StatusInternalServerError,+ model.ErrorCodeRuntimeError,+ fmt.Sprintf("error opening destination file %s. %v", target, err),+ )+ }+ defer file.Close()+ if _, err := io.Copy(file, r); err != nil {+ return uploadWriteError(target, err)+ }+ if err := file.Sync(); err != nil {+ return uploadWriteError(target, err)+ }+ return nil+}+func uploadWriteError(path string, err error) *uploadError {return newUploadError(http.StatusInternalServerError,diff --git a/components/execd/pkg/web/controller/filesystem_upload_atomic_test.go b/components/execd/pkg/web/controller/filesystem_upload_atomic_test.goindex 758b8c023..3b375b294 100644--- a/components/execd/pkg/web/controller/filesystem_upload_atomic_test.go+++ b/components/execd/pkg/web/controller/filesystem_upload_atomic_test.go@@ -195,3 +195,65 @@ func TestUploadFileWritesThroughMultipart(t *testing.T) {}requireNoUploadStaging(t, dir)}++// A rename replaces the inode, so a setuid/setgid bit the old in-place write+// kept would silently disappear unless the staged file carries it too.+func TestUploadDestinationModeKeepsSpecialBits(t *testing.T) {+ mode := os.FileMode(0o750) | os.ModeSetuid | os.ModeSetgid | os.ModeSticky+ require.Equal(t, mode, uploadDestinationMode(mode))+ require.Equal(t, os.FileMode(0o640), uploadDestinationMode(0o640))+}++func TestWriteUploadStreamPreservesSpecialModeBits(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("Windows only models the read-only bit")+ }+ dir := t.TempDir()+ target := filepath.Join(dir, "helper")+ require.NoError(t, os.WriteFile(target, []byte("old"), 0o750))+ require.NoError(t, os.Chmod(target, 0o750|os.ModeSetuid))+ before, err := os.Stat(target)+ require.NoError(t, err)+ if before.Mode()&os.ModeSetuid == 0 {+ t.Skip("filesystem does not keep setuid bits")+ }++ uerr := writeUploadStream(target, bytes.NewReader([]byte("new")))++ require.Nil(t, uerr)+ info, err := os.Stat(target)+ require.NoError(t, err)+ require.Equal(t, os.FileMode(0o750), info.Mode().Perm())+ require.NotZero(t, info.Mode()&os.ModeSetuid, "the replacement must keep setuid")+}++// A destination the caller can write but whose directory it cannot must keep+// working: the staged path cannot create its sibling, so it falls back to the+// legacy in-place write.+func TestWriteUploadStreamFallsBackWithoutDirectoryWrite(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("Windows does not model directory write permission this way")+ }+ dir := t.TempDir()+ target := filepath.Join(dir, "notes.md")+ require.NoError(t, os.WriteFile(target, []byte("old"), 0o666))+ require.NoError(t, os.Chmod(dir, 0o555))+ t.Cleanup(func() { _ = os.Chmod(dir, 0o755) })++ // root and some ACL setups still allow the staging file; then there is no+ // fallback to exercise.+ probe, probeErr := os.CreateTemp(dir, ".probe-")+ if probeErr == nil {+ _ = probe.Close()+ _ = os.Remove(probe.Name())+ t.Skip("directory stays writable for this process")+ }++ uerr := writeUploadStream(target, bytes.NewReader([]byte("new")))++ require.Nil(t, uerr)+ got, err := os.ReadFile(target)+ require.NoError(t, err)+ require.Equal(t, "new", string(got))+ requireNoUploadStaging(t, dir)+}diff --git a/docs/architecture/data-plane/execd.md b/docs/architecture/data-plane/execd.mdindex f002411c0..419a7e0e2 100644--- a/docs/architecture/data-plane/execd.md+++ b/docs/architecture/data-plane/execd.md@@ -100,7 +100,7 @@ This is a sandbox-wide credential, not a restricted terminal ticket. Obtain it tOne API covers the whole filesystem tree: stream files in and out, list and search directories, inspect and change permissions, replace file contents, move and remove paths. The file APIs in every sandbox SDK are a thin wrapper over these endpoints.-An upload is staged next to its destination and renamed into place, so a copy that fails partway leaves the file it was replacing untouched instead of truncating it. The replacement keeps the destination's mode and, where the platform allows it, its owner and group.+Uploads through the filesystem route are staged next to their destination and renamed into place, so a copy that fails partway leaves the file it was replacing untouched instead of truncating it. This requires write permission on the containing directory. When only the destination file is writable (for example a `0666` file in a `0555` directory), execd falls back to an in-place write: the upload still succeeds, but a partial failure can truncate the old content. The replacement keeps the destination's mode, including setuid/setgid/sticky bits, and, where the platform allows it, its owner and group. The isolated-session upload route (`/v1/isolated/session/:sessionId/files/upload`) is not staged and keeps its in-place behavior.### Filesystem execution identity