Skip to content

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

fix(execd): keep upload staging private and hidden

JasonBuildAIc941c435ac00
diff --git a/components/execd/pkg/web/controller/filesystem.go b/components/execd/pkg/web/controller/filesystem.go
index f935d1e29..51131b0d7 100644
--- a/components/execd/pkg/web/controller/filesystem.go
+++ b/components/execd/pkg/web/controller/filesystem.go
@@ -307,6 +307,9 @@ func listDirectoryEntries(root string, maxDepth int) ([]model.FileInfo, error) {
}
for _, entry := range dirEntries {
+ if isUploadStagingName(entry.Name()) {
+ continue
+ }
entryPath := filepath.Join(dir, entry.Name())
info, err := entry.Info()
if err != nil {
@@ -377,6 +380,9 @@ func (c *FilesystemController) SearchFiles() {
if info.IsDir() {
return nil
}
+ if isUploadStagingName(info.Name()) {
+ return nil
+ }
match, err := glob.PathMatch(pattern, info.Name())
if err != nil {
diff --git a/components/execd/pkg/web/controller/filesystem_test.go b/components/execd/pkg/web/controller/filesystem_test.go
index e0e769c90..858568889 100644
--- a/components/execd/pkg/web/controller/filesystem_test.go
+++ b/components/execd/pkg/web/controller/filesystem_test.go
@@ -127,6 +127,25 @@ func TestFilesystemControllerSearchFiles(t *testing.T) {
require.Equal(t, "file", files[0].Type)
}
+func TestFilesystemControllerSearchFilesHidesUploadStaging(t *testing.T) {
+ tmpDir := t.TempDir()
+ visible := filepath.Join(tmpDir, "visible.txt")
+ staging := filepath.Join(tmpDir, uploadTempPattern+"0123456789abcdef")
+ require.NoError(t, os.WriteFile(visible, []byte("visible"), 0o644))
+ require.NoError(t, os.WriteFile(staging, []byte("in flight"), 0o600))
+
+ rawURL := fmt.Sprintf("/files/search?path=%s&pattern=%s", url.QueryEscape(tmpDir), url.QueryEscape("**"))
+ ctrl, rec := newFilesystemController(t, http.MethodGet, rawURL, nil)
+
+ ctrl.SearchFiles()
+
+ require.Equal(t, http.StatusOK, rec.Code)
+ var files []model.FileInfo
+ require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &files))
+ require.Len(t, files, 1)
+ require.Equal(t, visible, files[0].Path)
+}
+
func TestFilesystemControllerListDirectoryDefaultDepth(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "alpha.txt")
@@ -155,6 +174,25 @@ func TestFilesystemControllerListDirectoryDefaultDepth(t *testing.T) {
require.NotContains(t, byPath, deepFile)
}
+func TestFilesystemControllerListDirectoryHidesUploadStaging(t *testing.T) {
+ tmpDir := t.TempDir()
+ visible := filepath.Join(tmpDir, "visible.txt")
+ staging := filepath.Join(tmpDir, uploadTempPattern+"0123456789abcdef")
+ require.NoError(t, os.WriteFile(visible, []byte("visible"), 0o644))
+ require.NoError(t, os.WriteFile(staging, []byte("in flight"), 0o600))
+
+ rawURL := fmt.Sprintf("/directories/list?path=%s", url.QueryEscape(tmpDir))
+ ctrl, rec := newFilesystemController(t, http.MethodGet, rawURL, nil)
+
+ ctrl.ListDirectory()
+
+ require.Equal(t, http.StatusOK, rec.Code)
+ var entries []model.FileInfo
+ require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &entries))
+ require.Len(t, entries, 1)
+ require.Equal(t, visible, entries[0].Path)
+}
+
func TestFilesystemControllerListDirectoryDepth(t *testing.T) {
tmpDir := t.TempDir()
nested := filepath.Join(tmpDir, "nested")
diff --git a/components/execd/pkg/web/controller/filesystem_upload.go b/components/execd/pkg/web/controller/filesystem_upload.go
index 1cedcbc33..567aa846e 100644
--- a/components/execd/pkg/web/controller/filesystem_upload.go
+++ b/components/execd/pkg/web/controller/filesystem_upload.go
@@ -26,6 +26,7 @@ import (
"net/http"
"os"
"path/filepath"
+ "strings"
"time"
"github.com/gin-gonic/gin"
@@ -181,7 +182,8 @@ func writeUploadFile(resolvedPath string, fileHeader *multipart.FileHeader) *upl
}
// uploadTempPattern prefixes the staging file an upload writes next to its
-// destination. The leading dot keeps it out of a plain directory listing.
+// destination. The filesystem APIs hide these names while the file is in
+// flight or after a crash leaves one behind.
const uploadTempPattern = ".osb-upload-"
// maxUploadSymlinkDepth bounds the final-component symlink walk; it mirrors the
@@ -231,7 +233,14 @@ func writeUploadStream(target string, r io.Reader) *uploadError {
)
}
- tmp, err := createUploadTemp(filepath.Dir(resolvedTarget))
+ stagingPerm := os.FileMode(os.ModePerm)
+ if statErr == nil {
+ // The destination mode is applied before the rename. Keep the content
+ // private while it is being copied instead of exposing it through the
+ // staging file's default mode.
+ stagingPerm = 0o600
+ }
+ tmp, err := createUploadTemp(filepath.Dir(resolvedTarget), stagingPerm)
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
@@ -331,17 +340,30 @@ func uploadWriteError(path string, err error) *uploadError {
)
}
-// createUploadTemp creates the staging file with the mode a plain create in
-// this directory has today (os.ModePerm masked by the umask), so a client that
-// sends no permission keeps the mode it used to get.
-func createUploadTemp(dir string) (*os.File, error) {
+// isUploadStagingName reports whether name is one of execd's upload staging
+// files. The listing and search paths skip these so an in-flight upload, or a
+// stage left by a killed daemon, is not returned as a user file.
+func isUploadStagingName(name string) bool {
+ suffix, ok := strings.CutPrefix(name, uploadTempPattern)
+ if !ok || len(suffix) != 16 {
+ return false
+ }
+ _, err := hex.DecodeString(suffix)
+ return err == nil
+}
+
+// createUploadTemp creates the staging file with perm. A new destination keeps
+// the mode a plain create in this directory has today (os.ModePerm masked by
+// the umask); replacing an existing file starts at 0600 until its mode is
+// restored before the rename.
+func createUploadTemp(dir string, perm os.FileMode) (*os.File, error) {
var suffix [8]byte
for attempt := 0; attempt < 10; attempt++ {
if _, err := rand.Read(suffix[:]); err != nil {
return nil, err
}
name := filepath.Join(dir, uploadTempPattern+hex.EncodeToString(suffix[:]))
- f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_EXCL, os.ModePerm)
+ f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
if err == nil {
return f, nil
}
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 3b375b294..3b9fc6c14 100644
--- a/components/execd/pkg/web/controller/filesystem_upload_atomic_test.go
+++ b/components/execd/pkg/web/controller/filesystem_upload_atomic_test.go
@@ -17,6 +17,7 @@ package controller
import (
"bytes"
"errors"
+ "io"
"mime/multipart"
"net/http"
"os"
@@ -25,9 +26,25 @@ import (
"testing"
"testing/iotest"
+ "github.com/alibaba/opensandbox/execd/pkg/vfs"
"github.com/stretchr/testify/require"
)
+type uploadObservingReader func([]byte) (int, error)
+
+func (f uploadObservingReader) Read(p []byte) (int, error) {
+ return f(p)
+}
+
+type uploadListingFS struct {
+ vfs.FS
+ entries []os.DirEntry
+}
+
+func (f uploadListingFS) ReadDir(string) ([]os.DirEntry, error) {
+ return f.entries, nil
+}
+
// requireNoUploadStaging asserts the staging file of a failed upload is gone.
func requireNoUploadStaging(t *testing.T, dir string) {
t.Helper()
@@ -106,6 +123,42 @@ func TestWriteUploadStreamPreservesDestinationMode(t *testing.T) {
require.Equal(t, os.FileMode(0o640), info.Mode().Perm())
}
+// Replacing a restrictive file must not expose or let a co-tenant modify the
+// in-flight content through the staging file's default permissions.
+func TestWriteUploadStreamStagesRestrictiveDestinationPrivately(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("Windows does not model Unix permission bits")
+ }
+ dir := t.TempDir()
+ target := filepath.Join(dir, "secret.txt")
+ require.NoError(t, os.WriteFile(target, []byte("old"), 0o600))
+
+ var stagingPerm os.FileMode
+ found := false
+ uerr := writeUploadStream(target, uploadObservingReader(func(p []byte) (int, error) {
+ entries, err := os.ReadDir(dir)
+ require.NoError(t, err)
+ for _, entry := range entries {
+ if !isUploadStagingName(entry.Name()) {
+ continue
+ }
+ info, err := entry.Info()
+ require.NoError(t, err)
+ stagingPerm = info.Mode().Perm()
+ found = true
+ break
+ }
+ return copy(p, []byte("new")), io.EOF
+ }))
+
+ require.Nil(t, uerr)
+ require.True(t, found, "upload did not create a staging file before reading content")
+ require.Equal(t, os.FileMode(0o600), stagingPerm)
+ info, err := os.Stat(target)
+ require.NoError(t, err)
+ require.Equal(t, os.FileMode(0o600), 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) {
@@ -163,6 +216,67 @@ func TestWriteUploadStreamFollowsSymlinkTarget(t *testing.T) {
require.Equal(t, "through the link", string(got))
}
+func TestResolveUploadTargetPathRejectsSymlinkLoop(t *testing.T) {
+ dir := t.TempDir()
+ first := filepath.Join(dir, "first")
+ second := filepath.Join(dir, "second")
+ if err := os.Symlink("second", first); err != nil {
+ t.Skipf("symlinks unavailable: %v", err)
+ }
+ if err := os.Symlink("first", second); err != nil {
+ t.Skipf("symlinks unavailable: %v", err)
+ }
+
+ resolved, uerr := resolveUploadTargetPath(first)
+
+ require.Empty(t, resolved)
+ require.NotNil(t, uerr)
+ require.Contains(t, uerr.message, "too many levels of symbolic links")
+}
+
+// A dangling final-component link keeps the old O_CREATE behavior: the upload
+// creates the link target and leaves the link itself in place.
+func TestWriteUploadStreamCreatesDanglingSymlinkTarget(t *testing.T) {
+ dir := t.TempDir()
+ target := filepath.Join(dir, "created.txt")
+ link := filepath.Join(dir, "dangling")
+ if err := os.Symlink(filepath.Base(target), link); err != nil {
+ t.Skipf("symlinks unavailable: %v", err)
+ }
+
+ uerr := writeUploadStream(link, bytes.NewReader([]byte("through the link")))
+
+ require.Nil(t, uerr)
+ info, err := os.Lstat(link)
+ require.NoError(t, err)
+ require.NotZero(t, info.Mode()&os.ModeSymlink, "the dangling symlink must survive the upload")
+ got, err := os.ReadFile(target)
+ require.NoError(t, err)
+ require.Equal(t, "through the link", string(got))
+}
+
+func TestIsUploadStagingName(t *testing.T) {
+ require.True(t, isUploadStagingName(uploadTempPattern+"0123456789abcdef"))
+ require.False(t, isUploadStagingName(uploadTempPattern+"not-a-valid-name"))
+ require.False(t, isUploadStagingName("notes.md"))
+}
+
+func TestListIsolatedDirHidesUploadStaging(t *testing.T) {
+ dir := t.TempDir()
+ visible := filepath.Join(dir, "visible.txt")
+ require.NoError(t, os.WriteFile(visible, []byte("visible"), 0o644))
+ require.NoError(t, os.WriteFile(filepath.Join(dir, uploadTempPattern+"0123456789abcdef"), []byte("in flight"), 0o600))
+ dirEntries, err := os.ReadDir(dir)
+ require.NoError(t, err)
+
+ ctrl := &IsolatedSessionController{}
+ entries, err := ctrl.listIsolatedDir(uploadListingFS{entries: dirEntries}, ".", 1)
+
+ require.NoError(t, err)
+ require.Len(t, entries, 1)
+ require.Equal(t, filepath.Base(visible), entries[0].Path)
+}
+
// The multipart entry point keeps honoring the metadata path and mode.
func TestUploadFileWritesThroughMultipart(t *testing.T) {
dir := t.TempDir()
diff --git a/components/execd/pkg/web/controller/filesystem_windows.go b/components/execd/pkg/web/controller/filesystem_windows.go
index 7dca59e6b..292eec967 100644
--- a/components/execd/pkg/web/controller/filesystem_windows.go
+++ b/components/execd/pkg/web/controller/filesystem_windows.go
@@ -304,6 +304,9 @@ func listDirectoryEntries(root string, maxDepth int) ([]model.FileInfo, error) {
}
for _, entry := range dirEntries {
+ if isUploadStagingName(entry.Name()) {
+ continue
+ }
entryPath := filepath.Join(dir, entry.Name())
info, err := entry.Info()
if err != nil {
@@ -374,6 +377,9 @@ func (c *FilesystemController) SearchFiles() {
if info.IsDir() {
return nil
}
+ if isUploadStagingName(info.Name()) {
+ return nil
+ }
match, err := glob.PathMatch(pattern, info.Name())
if err != nil {
diff --git a/components/execd/pkg/web/controller/isolated_session_files.go b/components/execd/pkg/web/controller/isolated_session_files.go
index 2e9a525be..1c0f5e50a 100644
--- a/components/execd/pkg/web/controller/isolated_session_files.go
+++ b/components/execd/pkg/web/controller/isolated_session_files.go
@@ -113,6 +113,9 @@ func (c *IsolatedSessionController) SearchFiles() {
files := make([]model.FileInfo, 0, len(results))
for _, rel := range results {
+ if isUploadStagingName(filepath.Base(rel)) {
+ continue
+ }
info, err := mv.Stat(rel)
if err != nil {
continue
@@ -537,6 +540,9 @@ func (c *IsolatedSessionController) listIsolatedDir(mv vfs.FS, root string, maxD
return err
}
for _, entry := range dirEntries {
+ if isUploadStagingName(entry.Name()) {
+ continue
+ }
entryPath := filepath.Join(dir, entry.Name())
info, err := entry.Info()
if err != nil {
diff --git a/docs/architecture/data-plane/execd.md b/docs/architecture/data-plane/execd.md
index 419a7e0e2..ef7efd20a 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.
-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.
+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. Staging files are created privately when a destination already exists, and execd hides their names from directory listings and search results. The isolated-session upload route (`/v1/isolated/session/:sessionId/files/upload`) is not staged and keeps its in-place behavior.
### Filesystem execution identity