| 1 | |
| 2 | |
| 3 | package reversesshfs |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | type guardCall struct { |
| 15 | rel string |
| 16 | entry bool |
| 17 | } |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | func TestRootedGuardParents(t *testing.T) { |
| 23 | if runtime.GOOS == "windows" { |
| 24 | t.Skip("Windows has no symlinks here and asks with normalized names") |
| 25 | } |
| 26 | var calls []guardCall |
| 27 | allow := false |
| 28 | Guard = func(root, rel string, entry bool) bool { |
| 29 | calls = append(calls, guardCall{rel, entry}) |
| 30 | parts := strings.Split(strings.ToLower(rel), "/") |
| 31 | last := parts[len(parts)-1] |
| 32 | if allow { |
| 33 | return true |
| 34 | } |
| 35 | if last == ".vscode" && entry { |
| 36 | return false |
| 37 | } |
| 38 | return !(last == "tasks.json" && len(parts) > 1 && parts[len(parts)-2] == ".vscode") |
| 39 | } |
| 40 | t.Cleanup(func() { Guard = nil }) |
| 41 | c, root := setupRooted(t, false) |
| 42 | p := func(s string) string { return filepath.Join(root, s) } |
| 43 | must := func(err error) { |
| 44 | t.Helper() |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | } |
| 49 | must(os.MkdirAll(p(".vscode"), 0o755)) |
| 50 | must(os.WriteFile(p(".vscode/tasks.json"), []byte("orig"), 0o644)) |
| 51 | must(c.Mkdir(p("evil"))) |
| 52 | f, err := c.Create(p("evil/tasks.json")) |
| 53 | must(err) |
| 54 | _, _ = f.Write([]byte("pwn")) |
| 55 | must(f.Close()) |
| 56 | if !slices.Contains(calls, guardCall{"evil/tasks.json", false}) { |
| 57 | t.Errorf("a write is not an entry: %v", calls) |
| 58 | } |
| 59 | |
| 60 | for _, op := range []struct { |
| 61 | name string |
| 62 | rel string |
| 63 | f func() error |
| 64 | }{ |
| 65 | {"rename onto the parent", ".vscode", func() error { return c.PosixRename(p("evil"), p(".vscode")) }}, |
| 66 | {"rename onto the parent, no replace", ".vscode", func() error { return c.Rename(p("evil"), p(".vscode")) }}, |
| 67 | {"symlink as the parent", ".vscode", func() error { return c.Symlink("evil", p(".vscode")) }}, |
| 68 | {"rename the parent away", ".vscode", func() error { return c.PosixRename(p(".vscode"), p("old")) }}, |
| 69 | {"remove the parent", ".vscode", func() error { return c.RemoveDirectory(p(".vscode")) }}, |
| 70 | {"hard link to the file", ".vscode/tasks.json", func() error { return c.Link(p(".vscode/tasks.json"), p("evil/alias")) }}, |
| 71 | {"mkdir the parent", "sub/.vscode", func() error { return c.Mkdir(p("sub/.vscode")) }}, |
| 72 | } { |
| 73 | if op.rel == "sub/.vscode" { |
| 74 | must(os.Mkdir(p("sub"), 0o755)) |
| 75 | } |
| 76 | calls = nil |
| 77 | if err := op.f(); err == nil { |
| 78 | t.Errorf("%s: allowed though Guard said no", op.name) |
| 79 | } |
| 80 | if !slices.Contains(calls, guardCall{op.rel, true}) { |
| 81 | t.Errorf("%s: Guard asked %v, not %q as an entry", op.name, calls, op.rel) |
| 82 | } |
| 83 | } |
| 84 | if b, _ := os.ReadFile(p(".vscode/tasks.json")); string(b) != "orig" { |
| 85 | t.Errorf("tasks.json: %q", b) |
| 86 | } |
| 87 | if fi, err := os.Lstat(p(".vscode")); err != nil || !fi.IsDir() { |
| 88 | t.Errorf(".vscode: %v, %v", fi, err) |
| 89 | } |
| 90 | if _, err := os.Lstat(p("evil/alias")); err == nil { |
| 91 | t.Error("hard link made") |
| 92 | } |
| 93 | |
| 94 | |
| 95 | allow = true |
| 96 | must(c.PosixRename(p(".vscode"), p("old"))) |
| 97 | must(c.PosixRename(p("evil"), p(".vscode"))) |
| 98 | if b, _ := os.ReadFile(p(".vscode/tasks.json")); string(b) != "pwn" { |
| 99 | t.Errorf("tasks.json: %q", b) |
| 100 | } |
| 101 | } |