3 // Licensed under the Apache License, Version 2.0; See LICENSE.APACHE
15 // TODO Windows: This needs some serious work to port to Windows. For now,
16 // turning off testing in this package.
18 type dirOrLink struct {
23 func makeFs(tmpdir string, fs []dirOrLink) error {
24 for _, s := range fs {
25 s.path = filepath.Join(tmpdir, s.path)
27 os.MkdirAll(s.path, 0755)
30 if err := os.MkdirAll(filepath.Dir(s.path), 0755); err != nil {
33 if err := os.Symlink(s.target, s.path); err != nil && !os.IsExist(err) {
40 func testSymlink(tmpdir, path, expected, scope string) error {
41 rewrite, err := FollowSymlinkInScope(filepath.Join(tmpdir, path), filepath.Join(tmpdir, scope))
45 expected, err = filepath.Abs(filepath.Join(tmpdir, expected))
49 if expected != rewrite {
50 return fmt.Errorf("Expected %q got %q", expected, rewrite)
55 func TestFollowSymlinkAbsolute(t *testing.T) {
56 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkAbsolute")
60 defer os.RemoveAll(tmpdir)
61 if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/d", target: "/b"}}); err != nil {
64 if err := testSymlink(tmpdir, "testdata/fs/a/d/c/data", "testdata/b/c/data", "testdata"); err != nil {
69 func TestFollowSymlinkRelativePath(t *testing.T) {
70 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkRelativePath")
74 defer os.RemoveAll(tmpdir)
75 if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/i", target: "a"}}); err != nil {
78 if err := testSymlink(tmpdir, "testdata/fs/i", "testdata/fs/a", "testdata"); err != nil {
83 func TestFollowSymlinkSkipSymlinksOutsideScope(t *testing.T) {
84 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkSkipSymlinksOutsideScope")
88 defer os.RemoveAll(tmpdir)
89 if err := makeFs(tmpdir, []dirOrLink{
90 {path: "linkdir", target: "realdir"},
91 {path: "linkdir/foo/bar"},
95 if err := testSymlink(tmpdir, "linkdir/foo/bar", "linkdir/foo/bar", "linkdir/foo"); err != nil {
100 func TestFollowSymlinkInvalidScopePathPair(t *testing.T) {
101 if _, err := FollowSymlinkInScope("toto", "testdata"); err == nil {
102 t.Fatal("expected an error")
106 func TestFollowSymlinkLastLink(t *testing.T) {
107 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkLastLink")
111 defer os.RemoveAll(tmpdir)
112 if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/d", target: "/b"}}); err != nil {
115 if err := testSymlink(tmpdir, "testdata/fs/a/d", "testdata/b", "testdata"); err != nil {
120 func TestFollowSymlinkRelativeLinkChangeScope(t *testing.T) {
121 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkRelativeLinkChangeScope")
125 defer os.RemoveAll(tmpdir)
126 if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/e", target: "../b"}}); err != nil {
129 if err := testSymlink(tmpdir, "testdata/fs/a/e/c/data", "testdata/fs/b/c/data", "testdata"); err != nil {
132 // avoid letting allowing symlink e lead us to ../b
133 // normalize to the "testdata/fs/a"
134 if err := testSymlink(tmpdir, "testdata/fs/a/e", "testdata/fs/a/b", "testdata/fs/a"); err != nil {
139 func TestFollowSymlinkDeepRelativeLinkChangeScope(t *testing.T) {
140 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkDeepRelativeLinkChangeScope")
144 defer os.RemoveAll(tmpdir)
146 if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/f", target: "../../../../test"}}); err != nil {
149 // avoid letting symlink f lead us out of the "testdata" scope
150 // we don't normalize because symlink f is in scope and there is no
152 if err := testSymlink(tmpdir, "testdata/fs/a/f", "testdata/test", "testdata"); err != nil {
155 // avoid letting symlink f lead us out of the "testdata/fs" scope
156 // we don't normalize because symlink f is in scope and there is no
158 if err := testSymlink(tmpdir, "testdata/fs/a/f", "testdata/fs/test", "testdata/fs"); err != nil {
163 func TestFollowSymlinkRelativeLinkChain(t *testing.T) {
164 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkRelativeLinkChain")
168 defer os.RemoveAll(tmpdir)
170 // avoid letting symlink g (pointed at by symlink h) take out of scope
171 // TODO: we should probably normalize to scope here because ../[....]/root
172 // is out of scope and we leak information
173 if err := makeFs(tmpdir, []dirOrLink{
174 {path: "testdata/fs/b/h", target: "../g"},
175 {path: "testdata/fs/g", target: "../../../../../../../../../../../../root"},
179 if err := testSymlink(tmpdir, "testdata/fs/b/h", "testdata/root", "testdata"); err != nil {
184 func TestFollowSymlinkBreakoutPath(t *testing.T) {
185 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkBreakoutPath")
189 defer os.RemoveAll(tmpdir)
191 // avoid letting symlink -> ../directory/file escape from scope
192 // normalize to "testdata/fs/j"
193 if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/j/k", target: "../i/a"}}); err != nil {
196 if err := testSymlink(tmpdir, "testdata/fs/j/k", "testdata/fs/j/i/a", "testdata/fs/j"); err != nil {
201 func TestFollowSymlinkToRoot(t *testing.T) {
202 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkToRoot")
206 defer os.RemoveAll(tmpdir)
208 // make sure we don't allow escaping to /
210 if err := makeFs(tmpdir, []dirOrLink{{path: "foo", target: "/"}}); err != nil {
213 if err := testSymlink(tmpdir, "foo", "", ""); err != nil {
218 func TestFollowSymlinkSlashDotdot(t *testing.T) {
219 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkSlashDotdot")
223 defer os.RemoveAll(tmpdir)
224 tmpdir = filepath.Join(tmpdir, "dir", "subdir")
226 // make sure we don't allow escaping to /
228 if err := makeFs(tmpdir, []dirOrLink{{path: "foo", target: "/../../"}}); err != nil {
231 if err := testSymlink(tmpdir, "foo", "", ""); err != nil {
236 func TestFollowSymlinkDotdot(t *testing.T) {
237 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkDotdot")
241 defer os.RemoveAll(tmpdir)
242 tmpdir = filepath.Join(tmpdir, "dir", "subdir")
244 // make sure we stay in scope without leaking information
245 // this also checks for escaping to /
247 if err := makeFs(tmpdir, []dirOrLink{{path: "foo", target: "../../"}}); err != nil {
250 if err := testSymlink(tmpdir, "foo", "", ""); err != nil {
255 func TestFollowSymlinkRelativePath2(t *testing.T) {
256 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkRelativePath2")
260 defer os.RemoveAll(tmpdir)
262 if err := makeFs(tmpdir, []dirOrLink{{path: "bar/foo", target: "baz/target"}}); err != nil {
265 if err := testSymlink(tmpdir, "bar/foo", "bar/baz/target", ""); err != nil {
270 func TestFollowSymlinkScopeLink(t *testing.T) {
271 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkScopeLink")
275 defer os.RemoveAll(tmpdir)
277 if err := makeFs(tmpdir, []dirOrLink{
279 {path: "root", target: "root2"},
280 {path: "root2/foo", target: "../bar"},
284 if err := testSymlink(tmpdir, "root/foo", "root/bar", "root"); err != nil {
289 func TestFollowSymlinkRootScope(t *testing.T) {
290 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkRootScope")
294 defer os.RemoveAll(tmpdir)
296 expected, err := filepath.EvalSymlinks(tmpdir)
300 rewrite, err := FollowSymlinkInScope(tmpdir, "/")
304 if rewrite != expected {
305 t.Fatalf("expected %q got %q", expected, rewrite)
309 func TestFollowSymlinkEmpty(t *testing.T) {
310 res, err := FollowSymlinkInScope("", "")
314 wd, err := os.Getwd()
319 t.Fatalf("expected %q got %q", wd, res)
323 func TestFollowSymlinkCircular(t *testing.T) {
324 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkCircular")
328 defer os.RemoveAll(tmpdir)
330 if err := makeFs(tmpdir, []dirOrLink{{path: "root/foo", target: "foo"}}); err != nil {
333 if err := testSymlink(tmpdir, "root/foo", "", "root"); err == nil {
334 t.Fatal("expected an error for foo -> foo")
337 if err := makeFs(tmpdir, []dirOrLink{
338 {path: "root/bar", target: "baz"},
339 {path: "root/baz", target: "../bak"},
340 {path: "root/bak", target: "/bar"},
344 if err := testSymlink(tmpdir, "root/foo", "", "root"); err == nil {
345 t.Fatal("expected an error for bar -> baz -> bak -> bar")
349 func TestFollowSymlinkComplexChainWithTargetPathsContainingLinks(t *testing.T) {
350 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkComplexChainWithTargetPathsContainingLinks")
354 defer os.RemoveAll(tmpdir)
356 if err := makeFs(tmpdir, []dirOrLink{
358 {path: "root", target: "root2"},
359 {path: "root/a", target: "r/s"},
360 {path: "root/r", target: "../root/t"},
361 {path: "root/root/t/s/b", target: "/../u"},
362 {path: "root/u/c", target: "."},
363 {path: "root/u/x/y", target: "../v"},
364 {path: "root/u/v", target: "/../w"},
368 if err := testSymlink(tmpdir, "root/a/b/c/x/y/z", "root/w/z", "root"); err != nil {
373 func TestFollowSymlinkBreakoutNonExistent(t *testing.T) {
374 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkBreakoutNonExistent")
378 defer os.RemoveAll(tmpdir)
380 if err := makeFs(tmpdir, []dirOrLink{
381 {path: "root/slash", target: "/"},
382 {path: "root/sym", target: "/idontexist/../slash"},
386 if err := testSymlink(tmpdir, "root/sym/file", "root/file", "root"); err != nil {
391 func TestFollowSymlinkNoLexicalCleaning(t *testing.T) {
392 tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkNoLexicalCleaning")
396 defer os.RemoveAll(tmpdir)
398 if err := makeFs(tmpdir, []dirOrLink{
399 {path: "root/sym", target: "/foo/bar"},
400 {path: "root/hello", target: "/sym/../baz"},
404 if err := testSymlink(tmpdir, "root/hello", "root/foo/baz", "root"); err != nil {