Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(execd): address review feedback on the staged upload replace

Jason Cat6eb25d8219f0
diff --git a/components/execd/pkg/web/controller/file_owner_other.go b/components/execd/pkg/web/controller/file_owner_other.go
index e73bd0636..d5e87c2f3 100644
--- a/components/execd/pkg/web/controller/file_owner_other.go
+++ b/components/execd/pkg/web/controller/file_owner_other.go
@@ -20,6 +20,6 @@ import "os"
// copyFileOwnership is a no-op where the platform exposes no numeric owner and
// group on a FileInfo.
-func copyFileOwnership(string, os.FileInfo) error {
+func copyFileOwnership(*os.File, os.FileInfo) error {
return nil
}
diff --git a/components/execd/pkg/web/controller/file_owner_unix.go b/components/execd/pkg/web/controller/file_owner_unix.go
index 9a2d1ffaf..0a652f108 100644
--- a/components/execd/pkg/web/controller/file_owner_unix.go
+++ b/components/execd/pkg/web/controller/file_owner_unix.go
@@ -22,16 +22,18 @@ import (
"syscall"
)
-// copyFileOwnership records the owner and group of info on path, so staging a
-// file and renaming it over an existing one keeps the ownership an in-place
-// write preserved.
-func copyFileOwnership(path string, info os.FileInfo) error {
+// copyFileOwnership records the owner and group of info on file, so staging an
+// upload and renaming it over an existing one keeps the ownership an in-place
+// write preserved. The open descriptor is used rather than the staging path:
+// the staging directory is writable, so the name could be swapped between
+// creation and this call.
+func copyFileOwnership(file *os.File, info os.FileInfo) error {
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return nil
}
- if err := os.Chown(path, int(stat.Uid), int(stat.Gid)); err != nil {
- return fmt.Errorf("chown %s to %d:%d: %w", path, stat.Uid, stat.Gid, err)
+ if err := file.Chown(int(stat.Uid), int(stat.Gid)); err != nil {
+ return fmt.Errorf("chown to %d:%d: %w", stat.Uid, stat.Gid, err)
}
return nil
}
diff --git a/components/execd/pkg/web/controller/filesystem_upload.go b/components/execd/pkg/web/controller/filesystem_upload.go
index 7acbc9d00..946c6bc17 100644
--- a/components/execd/pkg/web/controller/filesystem_upload.go
+++ b/components/execd/pkg/web/controller/filesystem_upload.go
@@ -204,8 +204,8 @@ func writeUploadStream(target string, r io.Reader) *uploadError {
existing, statErr := os.Stat(resolvedTarget)
switch {
case statErr == nil && !existing.Mode().IsRegular():
- // A device or a FIFO cannot be staged and renamed over without being
- // replaced by a regular file, so keep refusing them as before.
+ // A device or a FIFO cannot be staged and renamed over without replacing
+ // the node with a regular file, so refuse them.
return newUploadError(
http.StatusInternalServerError,
model.ErrorCodeRuntimeError,
@@ -256,7 +256,7 @@ func writeUploadStream(target string, r io.Reader) *uploadError {
if err := tmp.Chmod(existing.Mode().Perm()); err != nil {
return uploadWriteError(resolvedTarget, err)
}
- if err := copyFileOwnership(tmpName, existing); err != nil {
+ if err := copyFileOwnership(tmp, existing); err != nil {
log.Warn("upload: preserve owner/group of %s: %v", resolvedTarget, err)
}
}
diff --git a/components/execd/pkg/web/controller/filesystem_upload_atomic_test.go b/components/execd/pkg/web/controller/filesystem_upload_atomic_test.go
index 3e170df59..758b8c023 100644
--- a/components/execd/pkg/web/controller/filesystem_upload_atomic_test.go
+++ b/components/execd/pkg/web/controller/filesystem_upload_atomic_test.go
@@ -87,6 +87,60 @@ func TestWriteUploadStreamReplacesContent(t *testing.T) {
requireNoUploadStaging(t, dir)
}
+// A successful replacement keeps the destination mode an in-place write kept:
+// the staging file starts at the create default, so only explicit preservation
+// makes this pass.
+func TestWriteUploadStreamPreservesDestinationMode(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("Windows only models the read-only bit")
+ }
+ dir := t.TempDir()
+ target := filepath.Join(dir, "secret.txt")
+ require.NoError(t, os.WriteFile(target, []byte("old"), 0o640))
+
+ 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(0o640), info.Mode().Perm())
+}
+
+// A destination that cannot be written keeps failing: the rename alone would
+// only need a writable directory.
+func TestWriteUploadStreamRejectsReadOnlyDestination(t *testing.T) {
+ dir := t.TempDir()
+ target := filepath.Join(dir, "locked.txt")
+ require.NoError(t, os.WriteFile(target, []byte("old"), 0o644))
+ require.NoError(t, os.Chmod(target, 0o444))
+ if probe, err := os.OpenFile(target, os.O_WRONLY, 0); err == nil {
+ // root can write a read-only file; the rejection is unobservable here.
+ _ = probe.Close()
+ t.Skip("process bypasses file permissions")
+ }
+
+ uerr := writeUploadStream(target, bytes.NewReader([]byte("new")))
+
+ require.NotNil(t, uerr)
+ got, err := os.ReadFile(target)
+ require.NoError(t, err)
+ require.Equal(t, "old", string(got))
+}
+
+func TestWriteUploadStreamRejectsNonRegularDestination(t *testing.T) {
+ dir := t.TempDir()
+ subdir := filepath.Join(dir, "a-directory")
+ require.NoError(t, os.Mkdir(subdir, 0o755))
+
+ uerr := writeUploadStream(subdir, bytes.NewReader([]byte("new")))
+
+ require.NotNil(t, uerr)
+ info, err := os.Stat(subdir)
+ require.NoError(t, err)
+ require.True(t, info.IsDir(), "the destination itself must be untouched")
+ requireNoUploadStaging(t, dir)
+}
+
// An upload to a symlinked path keeps writing the link target, as an in-place
// write did, instead of replacing the link with a regular file.
func TestWriteUploadStreamFollowsSymlinkTarget(t *testing.T) {
diff --git a/docs/architecture/data-plane/execd.md b/docs/architecture/data-plane/execd.md
index 00086485a..f002411c0 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 t
One 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 replaces its destination only after the bytes are fully written and synced: a copy that fails partway leaves the file it was replacing untouched instead of truncating it. The replacement keeps the destination's mode and, on Unix, its owner and group.
+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.
### Filesystem execution identity