返回 DeepSeek-Reasonix
rebuild_external_refs_test.go
根目录 / internal / boot / rebuild_external_refs_test.go
1 package boot
2
3 import (
4 "encoding/json"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9
10 "reasonix/internal/config"
11 "reasonix/internal/control"
12 )
13
14 func TestFullRebuildPreservesExternalFolderAccess(t *testing.T) {
15 for _, useAlias := range []bool{false, true} {
16 name := "native-path"
17 if useAlias {
18 name = "symlink-alias"
19 }
20 t.Run(name, func(t *testing.T) { testFullRebuildExternalFolderAccess(t, useAlias) })
21 }
22 }
23
24 func testFullRebuildExternalFolderAccess(t *testing.T, useAlias bool) {
25 isolateConfigHome(t)
26 root := robustTempDir(t)
27 t.Chdir(root)
28 writeRuntimeFixture(t, root)
29 writeCompactRatio(t, config.UserConfigPath(), .85)
30 external := robustTempDir(t)
31 writeFile(t, external, "sub/allowed.txt", "external content")
32 if useAlias {
33 alias := filepath.Join(robustTempDir(t), "external-alias")
34 if err := os.Symlink(external, alias); err != nil {
35 t.Skipf("symlink fixture unavailable: %v", err)
36 }
37 external = alias
38 }
39 outside := robustTempDir(t)
40 writeFile(t, outside, "secret.txt", "unregistered content")
41 current, err := BuildRuntime(t.Context(), withTestSession(t, Options{WorkspaceRoot: root}))
42 if err != nil {
43 t.Fatal(err)
44 }
45 t.Cleanup(current.Controller.Close)
46 token, _, err := current.Controller.RegisterExternalFolderRef(external)
47 if err != nil {
48 t.Fatal(err)
49 }
50 assertExternalFolderAccess(t, current, token, external, outside)
51 for _, ratio := range []float64{.80, .75} {
52 writeCompactRatio(t, config.UserConfigPath(), ratio)
53 next, err := RebuildFrom(t.Context(), current, Options{WorkspaceRoot: root, RuntimeReload: RuntimeReload{ForceFullRebuild: true}})
54 if err != nil {
55 t.Fatal(err)
56 }
57 t.Cleanup(next.Controller.Close)
58 if next.ReusedController || next.Controller.CompactRatio() != ratio {
59 t.Fatal("expected full settings replacement")
60 }
61 if err := control.ActivateControllerReplacement(current.Controller, next.Controller); err != nil {
62 t.Fatal(err)
63 }
64 current.Controller.ReleaseResources()
65 assertExternalFolderAccess(t, next, token, external, outside)
66 current = next
67 }
68 }
69
70 func assertExternalFolderAccess(t *testing.T, result *BuildResult, token, external, outside string) {
71 t.Helper()
72 ctrl := result.Controller
73 for _, suffix := range []string{"", "/sub/allowed.txt"} {
74 expected, err := filepath.EvalSymlinks(filepath.Join(external, filepath.FromSlash(suffix)))
75 if err != nil {
76 t.Fatalf("resolve expected external path: %v", err)
77 }
78 if path, _, ok := ctrl.ExternalFolderRefLocalPath(token + suffix); !ok || path != expected {
79 t.Errorf("original token %q lost: path=%q, want=%q, ok=%v", token+suffix, path, expected, ok)
80 }
81 }
82 block, errs := ctrl.ResolveScopedRefs(t.Context(), "read @"+token+"/sub/allowed.txt")
83 if len(errs) != 0 || !strings.Contains(block, "external content") {
84 t.Errorf("scoped reference lost: %q, %v", block, errs)
85 }
86 reader, ok := result.Assembly.Registry.Get("read_file")
87 if !ok {
88 t.Fatal("missing assembled read_file")
89 }
90 args, _ := json.Marshal(map[string]string{"path": token + "/sub/allowed.txt"})
91 content, err := reader.Execute(t.Context(), args)
92 if err != nil || !strings.Contains(content, "external content") {
93 t.Errorf("tool read root lost: %q, %v", content, err)
94 }
95 for _, path := range []string{token + "/../secret.txt", "__reasonix_external_folder/unregistered/secret.txt"} {
96 if _, _, ok := ctrl.ExternalFolderRefLocalPath(path); ok {
97 t.Errorf("reference permits escape %q", path)
98 }
99 args, _ := json.Marshal(map[string]string{"path": path})
100 if content, err := reader.Execute(t.Context(), args); err == nil || strings.Contains(content, "unregistered content") {
101 t.Errorf("read tool permits escape %q: %q, %v", path, content, err)
102 }
103 }
104 if _, ok := ctrl.AuthorizedExternalFolderLocalPath(filepath.Join(outside, "secret.txt")); ok {
105 t.Error("unregistered root became authorized")
106 }
107 writer, ok := result.Assembly.Registry.Get("write_file")
108 if !ok {
109 t.Fatal("missing assembled write_file")
110 }
111 args, _ = json.Marshal(map[string]string{"path": filepath.Join(external, "sub/allowed.txt"), "content": "unauthorized write"})
112 if _, err := writer.Execute(t.Context(), args); err == nil {
113 t.Error("read authorization broadened to writes")
114 }
115 }
116
116 lines GO