Debian Codegraph @

symlink/fs_unix_test.go

raw symbols SCIP
1 //go:build !windows
2
3 // Licensed under the Apache License, Version 2.0; See LICENSE.APACHE
4
5 package symlink
6
7 import (
8 "fmt"
9 "os"
10 "path/filepath"
11 "testing"
12 )
13
14 // TODO Windows: This needs some serious work to port to Windows. For now,
15 // turning off testing in this package.
16
17 type dirOrLink struct {
18 path string
19 target string
20 }
21
22 func makeFs(tmpdir string, fs []dirOrLink) error {
23 for _, s := range fs {
24 s.path = filepath.Join(tmpdir, s.path)
25 if s.target == "" {
26 _ = os.MkdirAll(s.path, 0o755)
27 continue
28 }
29 if err := os.MkdirAll(filepath.Dir(s.path), 0o755); err != nil {
30 return err
31 }
32 if err := os.Symlink(s.target, s.path); err != nil && !os.IsExist(err) {
33 return err
34 }
35 }
36 return nil
37 }
38
39 func testSymlink(tmpdir, path, expected, scope string) error {
40 rewrite, err := FollowSymlinkInScope(filepath.Join(tmpdir, path), filepath.Join(tmpdir, scope))
41 if err != nil {
42 return err
43 }
44 expected, err = filepath.Abs(filepath.Join(tmpdir, expected))
45 if err != nil {
46 return err
47 }
48 if expected != rewrite {
49 return fmt.Errorf("expected %q got %q", expected, rewrite)
50 }
51 return nil
52 }
53
54 func TestFollowSymlinkAbsolute(t *testing.T) {
55 tmpdir := t.TempDir()
56
57 if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/d", target: "/b"}}); err != nil {
58 t.Fatal(err)
59 }
60 if err := testSymlink(tmpdir, "testdata/fs/a/d/c/data", "testdata/b/c/data", "testdata"); err != nil {
61 t.Fatal(err)
62 }
63 }
64
65 func TestFollowSymlinkRelativePath(t *testing.T) {
66 tmpdir := t.TempDir()
67
68 if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/i", target: "a"}}); err != nil {
69 t.Fatal(err)
70 }
71 if err := testSymlink(tmpdir, "testdata/fs/i", "testdata/fs/a", "testdata"); err != nil {
72 t.Fatal(err)
73 }
74 }
75
76 func TestFollowSymlinkSkipSymlinksOutsideScope(t *testing.T) {
77 tmpdir := t.TempDir()
78
79 if err := makeFs(tmpdir, []dirOrLink{
80 {path: "linkdir", target: "realdir"},
81 {path: "linkdir/foo/bar"},
82 }); err != nil {
83 t.Fatal(err)
84 }
85 if err := testSymlink(tmpdir, "linkdir/foo/bar", "linkdir/foo/bar", "linkdir/foo"); err != nil {
86 t.Fatal(err)
87 }
88 }
89
90 func TestFollowSymlinkInvalidScopePathPair(t *testing.T) {
91 if _, err := FollowSymlinkInScope("toto", "testdata"); err == nil {
92 t.Fatal("expected an error")
93 }
94 }
95
96 func TestFollowSymlinkLastLink(t *testing.T) {
97 tmpdir := t.TempDir()
98
99 if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/d", target: "/b"}}); err != nil {
100 t.Fatal(err)
101 }
102 if err := testSymlink(tmpdir, "testdata/fs/a/d", "testdata/b", "testdata"); err != nil {
103 t.Fatal(err)
104 }
105 }
106
107 func TestFollowSymlinkRelativeLinkChangeScope(t *testing.T) {
108 tmpdir := t.TempDir()
109
110 if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/e", target: "../b"}}); err != nil {
111 t.Fatal(err)
112 }
113 if err := testSymlink(tmpdir, "testdata/fs/a/e/c/data", "testdata/fs/b/c/data", "testdata"); err != nil {
114 t.Fatal(err)
115 }
116 // avoid letting allowing symlink e lead us to ../b
117 // normalize to the "testdata/fs/a"
118 if err := testSymlink(tmpdir, "testdata/fs/a/e", "testdata/fs/a/b", "testdata/fs/a"); err != nil {
119 t.Fatal(err)
120 }
121 }
122
123 func TestFollowSymlinkDeepRelativeLinkChangeScope(t *testing.T) {
124 tmpdir := t.TempDir()
125
126 if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/f", target: "../../../../test"}}); err != nil {
127 t.Fatal(err)
128 }
129 // avoid letting symlink f lead us out of the "testdata" scope
130 // we don't normalize because symlink f is in scope and there is no
131 // information leak
132 if err := testSymlink(tmpdir, "testdata/fs/a/f", "testdata/test", "testdata"); err != nil {
133 t.Fatal(err)
134 }
135 // avoid letting symlink f lead us out of the "testdata/fs" scope
136 // we don't normalize because symlink f is in scope and there is no
137 // information leak
138 if err := testSymlink(tmpdir, "testdata/fs/a/f", "testdata/fs/test", "testdata/fs"); err != nil {
139 t.Fatal(err)
140 }
141 }
142
143 func TestFollowSymlinkRelativeLinkChain(t *testing.T) {
144 tmpdir := t.TempDir()
145
146 // avoid letting symlink g (pointed at by symlink h) take out of scope
147 // TODO: we should probably normalize to scope here because ../[....]/root
148 // is out of scope and we leak information
149 if err := makeFs(tmpdir, []dirOrLink{
150 {path: "testdata/fs/b/h", target: "../g"},
151 {path: "testdata/fs/g", target: "../../../../../../../../../../../../root"},
152 }); err != nil {
153 t.Fatal(err)
154 }
155 if err := testSymlink(tmpdir, "testdata/fs/b/h", "testdata/root", "testdata"); err != nil {
156 t.Fatal(err)
157 }
158 }
159
160 func TestFollowSymlinkBreakoutPath(t *testing.T) {
161 tmpdir := t.TempDir()
162
163 // avoid letting symlink -> ../directory/file escape from scope
164 // normalize to "testdata/fs/j"
165 if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/j/k", target: "../i/a"}}); err != nil {
166 t.Fatal(err)
167 }
168 if err := testSymlink(tmpdir, "testdata/fs/j/k", "testdata/fs/j/i/a", "testdata/fs/j"); err != nil {
169 t.Fatal(err)
170 }
171 }
172
173 func TestFollowSymlinkToRoot(t *testing.T) {
174 tmpdir := t.TempDir()
175
176 // make sure we don't allow escaping to /
177 // normalize to dir
178 if err := makeFs(tmpdir, []dirOrLink{{path: "foo", target: "/"}}); err != nil {
179 t.Fatal(err)
180 }
181 if err := testSymlink(tmpdir, "foo", "", ""); err != nil {
182 t.Fatal(err)
183 }
184 }
185
186 func TestFollowSymlinkSlashDotdot(t *testing.T) {
187 tmpdir := t.TempDir()
188 tmpdir = filepath.Join(tmpdir, "dir", "subdir")
189
190 // make sure we don't allow escaping to /
191 // normalize to dir
192 if err := makeFs(tmpdir, []dirOrLink{{path: "foo", target: "/../../"}}); err != nil {
193 t.Fatal(err)
194 }
195 if err := testSymlink(tmpdir, "foo", "", ""); err != nil {
196 t.Fatal(err)
197 }
198 }
199
200 func TestFollowSymlinkDotdot(t *testing.T) {
201 tmpdir := t.TempDir()
202 tmpdir = filepath.Join(tmpdir, "dir", "subdir")
203
204 // make sure we stay in scope without leaking information
205 // this also checks for escaping to /
206 // normalize to dir
207 if err := makeFs(tmpdir, []dirOrLink{{path: "foo", target: "../../"}}); err != nil {
208 t.Fatal(err)
209 }
210 if err := testSymlink(tmpdir, "foo", "", ""); err != nil {
211 t.Fatal(err)
212 }
213 }
214
215 func TestFollowSymlinkRelativePath2(t *testing.T) {
216 tmpdir := t.TempDir()
217
218 if err := makeFs(tmpdir, []dirOrLink{{path: "bar/foo", target: "baz/target"}}); err != nil {
219 t.Fatal(err)
220 }
221 if err := testSymlink(tmpdir, "bar/foo", "bar/baz/target", ""); err != nil {
222 t.Fatal(err)
223 }
224 }
225
226 func TestFollowSymlinkScopeLink(t *testing.T) {
227 tmpdir := t.TempDir()
228
229 if err := makeFs(tmpdir, []dirOrLink{
230 {path: "root2"},
231 {path: "root", target: "root2"},
232 {path: "root2/foo", target: "../bar"},
233 }); err != nil {
234 t.Fatal(err)
235 }
236 if err := testSymlink(tmpdir, "root/foo", "root/bar", "root"); err != nil {
237 t.Fatal(err)
238 }
239 }
240
241 func TestFollowSymlinkRootScope(t *testing.T) {
242 tmpdir := t.TempDir()
243
244 expected, err := filepath.EvalSymlinks(tmpdir)
245 if err != nil {
246 t.Fatal(err)
247 }
248 rewrite, err := FollowSymlinkInScope(tmpdir, "/")
249 if err != nil {
250 t.Fatal(err)
251 }
252 if rewrite != expected {
253 t.Fatalf("expected %q got %q", expected, rewrite)
254 }
255 }
256
257 func TestFollowSymlinkEmpty(t *testing.T) {
258 res, err := FollowSymlinkInScope("", "")
259 if err != nil {
260 t.Fatal(err)
261 }
262 wd, err := os.Getwd()
263 if err != nil {
264 t.Fatal(err)
265 }
266 if res != wd {
267 t.Fatalf("expected %q got %q", wd, res)
268 }
269 }
270
271 func TestFollowSymlinkCircular(t *testing.T) {
272 tmpdir := t.TempDir()
273
274 if err := makeFs(tmpdir, []dirOrLink{{path: "root/foo", target: "foo"}}); err != nil {
275 t.Fatal(err)
276 }
277 if err := testSymlink(tmpdir, "root/foo", "", "root"); err == nil {
278 t.Fatal("expected an error for foo -> foo")
279 }
280
281 if err := makeFs(tmpdir, []dirOrLink{
282 {path: "root/bar", target: "baz"},
283 {path: "root/baz", target: "../bak"},
284 {path: "root/bak", target: "/bar"},
285 }); err != nil {
286 t.Fatal(err)
287 }
288 if err := testSymlink(tmpdir, "root/foo", "", "root"); err == nil {
289 t.Fatal("expected an error for bar -> baz -> bak -> bar")
290 }
291 }
292
293 func TestFollowSymlinkComplexChainWithTargetPathsContainingLinks(t *testing.T) {
294 tmpdir := t.TempDir()
295
296 if err := makeFs(tmpdir, []dirOrLink{
297 {path: "root2"},
298 {path: "root", target: "root2"},
299 {path: "root/a", target: "r/s"},
300 {path: "root/r", target: "../root/t"},
301 {path: "root/root/t/s/b", target: "/../u"},
302 {path: "root/u/c", target: "."},
303 {path: "root/u/x/y", target: "../v"},
304 {path: "root/u/v", target: "/../w"},
305 }); err != nil {
306 t.Fatal(err)
307 }
308 if err := testSymlink(tmpdir, "root/a/b/c/x/y/z", "root/w/z", "root"); err != nil {
309 t.Fatal(err)
310 }
311 }
312
313 func TestFollowSymlinkBreakoutNonExistent(t *testing.T) {
314 tmpdir := t.TempDir()
315
316 if err := makeFs(tmpdir, []dirOrLink{
317 {path: "root/slash", target: "/"},
318 {path: "root/sym", target: "/idontexist/../slash"},
319 }); err != nil {
320 t.Fatal(err)
321 }
322 if err := testSymlink(tmpdir, "root/sym/file", "root/file", "root"); err != nil {
323 t.Fatal(err)
324 }
325 }
326
327 func TestFollowSymlinkNoLexicalCleaning(t *testing.T) {
328 tmpdir := t.TempDir()
329
330 if err := makeFs(tmpdir, []dirOrLink{
331 {path: "root/sym", target: "/foo/bar"},
332 {path: "root/hello", target: "/sym/../baz"},
333 }); err != nil {
334 t.Fatal(err)
335 }
336 if err := testSymlink(tmpdir, "root/hello", "root/foo/baz", "root"); err != nil {
337 t.Fatal(err)
338 }
339 }