mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-03 12:21:50 +08:00
Docker exec no longer wraps commands in setsid, which exited immediately and cut stdout after about two seconds. Netstack releases the pending-dial slot once the handshake finishes, raises the in-flight limits, and treats local file-descriptor exhaustion as a silent drop. The gateway container gets a higher open-file limit.
255 lines
8.1 KiB
Go
255 lines
8.1 KiB
Go
package sandbox
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestResolveConfinedBlocksTraversalAndSymlinkEscape(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
root := filepath.Join(tmp, "tasks", "42")
|
|
outside := filepath.Join(tmp, "secret.txt")
|
|
if err := os.MkdirAll(filepath.Join(root, "i10"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(outside, []byte("top-secret"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Symlink inside workspace pointing outside
|
|
escapeLink := filepath.Join(root, "i10", "evil.txt")
|
|
if err := os.Symlink(outside, escapeLink); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Symlink directory pointing outside
|
|
escapeDir := filepath.Join(root, "escape-dir")
|
|
if err := os.Symlink(tmp, escapeDir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Valid container paths
|
|
got, ok := ResolveConfined(root, "i10", "/workspace/i10/out.txt")
|
|
if !ok {
|
|
t.Fatalf("expected valid container path to resolve")
|
|
}
|
|
wantReal, _ := filepath.EvalSymlinks(filepath.Join(root, "i10"))
|
|
if got != filepath.Join(wantReal, "out.txt") {
|
|
t.Fatalf("got %q, want %q", got, filepath.Join(wantReal, "out.txt"))
|
|
}
|
|
|
|
// Relative path resolves under defaultSubdir
|
|
gotRel, ok := ResolveConfined(root, "i10", "notes.md")
|
|
if !ok || gotRel != filepath.Join(wantReal, "notes.md") {
|
|
t.Fatalf("relative resolve got (%q, %v)", gotRel, ok)
|
|
}
|
|
|
|
// Blocked cases: traversal, outside abs path, symlink file, symlink dir + new leaf
|
|
for _, bad := range []string{
|
|
"../../secret.txt",
|
|
"/etc/passwd",
|
|
outside,
|
|
"/workspace/i10/evil.txt",
|
|
"evil.txt",
|
|
"/workspace/escape-dir/secret.txt",
|
|
"/workspace/escape-dir/new-file.txt",
|
|
} {
|
|
if p, ok := ResolveConfined(root, "i10", bad); ok {
|
|
t.Fatalf("expected %q to be rejected, got %q", bad, p)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCopyRegularFileSafeSkipsSymlinksAndDeduplicates(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
srcRoot := filepath.Join(tmp, "sessions", "conv-1")
|
|
dstRoot := filepath.Join(tmp, "tasks", "99")
|
|
if err := os.MkdirAll(filepath.Join(srcRoot, "uploads"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(dstRoot, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srcFile := filepath.Join(srcRoot, "uploads", "targets.txt")
|
|
if err := os.WriteFile(srcFile, []byte("example.com\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
outside := filepath.Join(tmp, "jwt.key")
|
|
if err := os.WriteFile(outside, []byte("secret"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sym := filepath.Join(srcRoot, "uploads", "link.txt")
|
|
if err := os.Symlink(outside, sym); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rel1, err := CopyRegularFileSafe(srcRoot, "/workspace/uploads/targets.txt", dstRoot, "uploads")
|
|
if err != nil || rel1 != "uploads/targets.txt" {
|
|
t.Fatalf("first copy = (%q, %v)", rel1, err)
|
|
}
|
|
rel2, err := CopyRegularFileSafe(srcRoot, "uploads/targets.txt", dstRoot, "uploads")
|
|
if err != nil || rel2 != "uploads/targets-1.txt" {
|
|
t.Fatalf("dedup copy = (%q, %v)", rel2, err)
|
|
}
|
|
if _, err := CopyRegularFileSafe(srcRoot, "uploads/link.txt", dstRoot, "uploads"); err == nil {
|
|
t.Fatalf("expected symlink copy to be rejected")
|
|
}
|
|
}
|
|
|
|
func TestDemuxDockerStream(t *testing.T) {
|
|
var raw bytes.Buffer
|
|
writeFrame := func(kind byte, msg string) {
|
|
var hdr [8]byte
|
|
hdr[0] = kind
|
|
binary.BigEndian.PutUint32(hdr[4:8], uint32(len(msg)))
|
|
raw.Write(hdr[:])
|
|
raw.WriteString(msg)
|
|
}
|
|
writeFrame(1, "hello ")
|
|
writeFrame(2, "warn!")
|
|
writeFrame(1, "world")
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
if err := demuxDockerStream(&raw, &stdout, &stderr); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if stdout.String() != "hello world" || stderr.String() != "warn!" {
|
|
t.Fatalf("stdout=%q stderr=%q", stdout.String(), stderr.String())
|
|
}
|
|
}
|
|
|
|
func TestHostToContainerAndDataPathMapping(t *testing.T) {
|
|
cfg := Config{
|
|
DataDir: "/app/data",
|
|
HostDataDir: "/srv/artex/data",
|
|
}
|
|
if got := cfg.MapDataPathToHost("/app/data/tasks/12"); got != "/srv/artex/data/tasks/12" {
|
|
t.Fatalf("MapDataPathToHost = %q", got)
|
|
}
|
|
if got := HostToContainerPath("/app/data/tasks/12", "/app/data/tasks/12/i7"); got != "/workspace/i7" {
|
|
t.Fatalf("HostToContainerPath = %q", got)
|
|
}
|
|
if got := HostToContainerPath("/app/data/tasks/12", "/app/data/tasks/12"); got != "/workspace" {
|
|
t.Fatalf("HostToContainerPath root = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestManagerContainerIPMap(t *testing.T) {
|
|
m := &Manager{ipMap: map[string]ContainerIdentity{}}
|
|
m.registerContainerIP("172.28.0.5", ContainerIdentity{
|
|
Scope: ScopeTask,
|
|
OwnerID: "12",
|
|
ContainerID: "abc123def456",
|
|
ContainerName: "artex-sbx-inst-task-12",
|
|
IP: "172.28.0.5",
|
|
})
|
|
got, ok := m.ResolveContainerByIP("172.28.0.5:54321")
|
|
if !ok || got.Scope != ScopeTask || got.OwnerID != "12" || got.ContainerID != "abc123def456" {
|
|
t.Fatalf("ResolveContainerByIP failed: ok=%v got=%+v", ok, got)
|
|
}
|
|
// Re-registering same container with a new IP must evict the stale IP.
|
|
m.registerContainerIP("172.28.0.9", ContainerIdentity{
|
|
Scope: ScopeTask,
|
|
OwnerID: "12",
|
|
ContainerID: "new999888777",
|
|
ContainerName: "artex-sbx-inst-task-12",
|
|
IP: "172.28.0.9",
|
|
})
|
|
if _, stale := m.ResolveContainerByIP("172.28.0.5"); stale {
|
|
t.Fatalf("stale IP 172.28.0.5 should have been evicted")
|
|
}
|
|
if got2, ok2 := m.ResolveContainerByIP("172.28.0.9"); !ok2 || got2.ContainerID != "new999888777" {
|
|
t.Fatalf("new IP 172.28.0.9 lookup failed: ok=%v got=%+v", ok2, got2)
|
|
}
|
|
// Explicit eviction by container name.
|
|
m.evictContainerIP("artex-sbx-inst-task-12")
|
|
if _, ok3 := m.ResolveContainerByIP("172.28.0.9"); ok3 {
|
|
t.Fatalf("evicted container IP should no longer resolve")
|
|
}
|
|
}
|
|
|
|
func TestConfigFromEnvAllowNetRaw(t *testing.T) {
|
|
t.Setenv("ARTEX_SANDBOX_ALLOW_NET_RAW", "")
|
|
if cfg := ConfigFromEnv(t.TempDir(), ""); !cfg.AllowNetRaw {
|
|
t.Fatalf("AllowNetRaw should default to true")
|
|
}
|
|
t.Setenv("ARTEX_SANDBOX_ALLOW_NET_RAW", "false")
|
|
if cfg := ConfigFromEnv(t.TempDir(), ""); cfg.AllowNetRaw {
|
|
t.Fatalf("AllowNetRaw should respect ARTEX_SANDBOX_ALLOW_NET_RAW=false")
|
|
}
|
|
}
|
|
|
|
func TestEnsureWritableDirMigratesNestedFilesWithoutFollowingSymlinks(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
outside := filepath.Join(tmp, "outside-secret.txt")
|
|
if err := os.WriteFile(outside, []byte("secret"), 0o400); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ws := filepath.Join(tmp, "tasks", "99")
|
|
nestedDir := filepath.Join(ws, "findings")
|
|
nestedFile := filepath.Join(nestedDir, "note.txt")
|
|
if err := os.MkdirAll(nestedDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(nestedFile, []byte("migrated"), 0o444); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(nestedDir, 0o555); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = os.Chmod(nestedDir, 0o755) })
|
|
|
|
if err := os.Symlink(outside, filepath.Join(ws, "escape-link")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := ensureWritableDir(ws, "1000:1000"); err != nil {
|
|
t.Fatalf("ensureWritableDir: %v", err)
|
|
}
|
|
|
|
fi, err := os.Stat(nestedFile)
|
|
if err != nil {
|
|
t.Fatalf("stat nestedFile: %v", err)
|
|
}
|
|
if fi.Mode().Perm()&0o222 != 0o222 {
|
|
t.Fatalf("nested migrated file should be writable, got mode=%o", fi.Mode().Perm())
|
|
}
|
|
outInfo, err := os.Stat(outside)
|
|
if err != nil {
|
|
t.Fatalf("stat outside: %v", err)
|
|
}
|
|
if outInfo.Mode().Perm() != 0o400 {
|
|
t.Fatalf("symlink target outside workspace must stay 0400, got %o", outInfo.Mode().Perm())
|
|
}
|
|
}
|
|
|
|
func TestWrapPGIDCommandDoesNotDetachAndPropagatesExitCode(t *testing.T) {
|
|
pgidFile := filepath.Join(t.TempDir(), "test.pgid")
|
|
cmd := wrapPGIDCommand(pgidFile, []string{"sh", "-c", "echo hi; exit 42"})
|
|
if len(cmd) == 0 || cmd[0] == "setsid" {
|
|
t.Fatalf("wrapPGIDCommand must not wrap with detaching setsid, got %v", cmd)
|
|
}
|
|
if runtime.GOOS == "windows" {
|
|
return
|
|
}
|
|
c := exec.Command(cmd[0], cmd[1:]...)
|
|
out, err := c.CombinedOutput()
|
|
if strings.TrimSpace(string(out)) != "hi" {
|
|
t.Fatalf("expected output 'hi', got %q (err=%v)", string(out), err)
|
|
}
|
|
var exitErr *exec.ExitError
|
|
if !errors.As(err, &exitErr) || exitErr.ExitCode() != 42 {
|
|
t.Fatalf("expected exit code 42, got err=%v", err)
|
|
}
|
|
pidBytes, rerr := os.ReadFile(pgidFile)
|
|
if rerr != nil || strings.TrimSpace(string(pidBytes)) == "" {
|
|
t.Fatalf("expected pgid file to contain PID, got %q err=%v", string(pidBytes), rerr)
|
|
}
|
|
}
|