mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-02 20:01:51 +08:00
- Implement full-traffic transparent gateway (cmd/artex-gateway, traffic/netstack.go, traffic/transparent.go) using Linux TUN + gVisor netstack deferred TCP handshakes (eliminating nmap/nc false-positive open ports), real ICMPv4 Echo proxying, UDP/DNS interception, and per-container upstream proxy overrides. - Add egress blacklist + asset-intercept sync, non-HTTP flow/DNS SQLite persistence and APIs, and /system/proxy & /function/traffic TCP/UDP/DNS UI panels. - Fix sandbox Playwright MCP browser loopback --proxy-server rewrite and NODE_EXTRA_CA_CERTS override in server/mcpstdio.go. - Strip all SUID/SGID binaries in Dockerfile.sandbox, grant minimal cap_net_raw+ep to nmap/nping/ping with ARTEX_SANDBOX_ALLOW_NET_RAW toggle, and enforce IPv4/IPv6 anti-spoofing in iptables and netstack. - Remove hardcoded Chinese "(全局)" from GET /api/proxy/routes, localize ContainerRoutesTab / NonHTTPTrafficPanel labels, and expose reactive locale/isEn from useLiveTranslation for React Compiler compatibility.
182 lines
5.8 KiB
Go
182 lines
5.8 KiB
Go
package sandbox
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"os"
|
|
"path/filepath"
|
|
"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")
|
|
}
|
|
}
|