| 1 | |
| 2 | |
| 3 | @@ -8,7 +8,6 @@ |
| 4 | "os" |
| 5 | "path" |
| 6 | "path/filepath" |
| 7 | - "strings" |
| 8 | "sync" |
| 9 | |
| 10 | "github.com/pkg/sftp" |
| 11 | @@ -27,7 +26,7 @@ |
| 12 | |
| 13 | // OpenCloudShare expects an absolute, symlink-resolved path supplied by the local picker. |
| 14 | func OpenCloudShare(directory string, readOnly bool) (*CloudShare, error) { |
| 15 | - if !filepath.IsAbs(directory) || filepath.Clean(directory) != directory || filepath.Dir(directory) == directory || CloudPathHidden(filepath.ToSlash(directory)) { |
| 16 | + if !filepath.IsAbs(directory) || filepath.Clean(directory) != directory || filepath.Dir(directory) == directory { |
| 17 | return nil, os.ErrPermission |
| 18 | } |
| 19 | sys, err := openCloudRoot(directory) |
| 20 | @@ -84,33 +83,6 @@ |
| 21 | return nil |
| 22 | } |
| 23 | |
| 24 | -// CloudPathHidden reports whether the file policy withholds this relative |
| 25 | -// path, including credential stores, Git history and host hooks. |
| 26 | -// This filename policy cannot identify secrets embedded in ordinary source. |
| 27 | -func CloudPathHidden(rel string) bool { |
| 28 | - for _, name := range strings.Split(rel, "/") { |
| 29 | - n := FoldName(name) |
| 30 | - switch n { |
| 31 | - case ".git", ".hg", ".sl", ".jj", ".ssh", ".gnupg", ".aws", ".kube", ".azure", ".docker", ".config", ".claude-cage", ".agent-vm", ".lima", |
| 32 | - ".env", ".envrc", ".netrc", "_netrc", ".npmrc", ".pypirc", ".git-credentials", ".gitconfig", ".gitmodules", |
| 33 | - ".claude", ".codex", ".cursor", ".vscode", ".idea", ".zed", ".gemini", ".devcontainer", ".devcontainer.json", |
| 34 | - ".dir-locals.el", ".mcp.json", ".githooks", ".husky", ".pre-commit-config.yaml", |
| 35 | - ".yarn", ".yarnrc.yml", ".pnpmfile.cjs", ".cargo", ".lfsconfig", ".mise", "mise.toml", "mise.local.toml", ".mise.toml", ".mise.local.toml", |
| 36 | - "lefthook.yml", ".lefthook.yml", "lefthook.yaml", ".lefthook.yaml", "lefthook-local.yml", ".lefthook-local.yml", |
| 37 | - "..namedfork", "desktop.ini", ".ds_store": |
| 38 | - return true |
| 39 | - } |
| 40 | - if strings.HasPrefix(n, ".env.") || strings.HasPrefix(n, "id_rsa") || strings.HasPrefix(n, "id_ed25519") || strings.HasPrefix(n, "id_ecdsa") || strings.HasPrefix(n, "id_dsa") { |
| 41 | - return true |
| 42 | - } |
| 43 | - switch path.Ext(n) { |
| 44 | - case ".pem", ".key", ".p12", ".pfx", ".ppk", ".lnk", ".url", ".scf", ".library-ms", ".searchconnector-ms": |
| 45 | - return true |
| 46 | - } |
| 47 | - } |
| 48 | - return false |
| 49 | -} |
| 50 | - |
| 51 | // Use the confined server's handle-relative writes. All reads and metadata |
| 52 | // requests use the same no-follow path walk, including the leaf. An internal |
| 53 | // symlink could otherwise expose a protected path under another name. |
| 54 | @@ -118,7 +90,7 @@ |
| 55 | |
| 56 | func (h *cloudHandlers) accessible(p string) (string, error) { |
| 57 | rel, err := h.rel(p) |
| 58 | - if err != nil || !writableName(rel) || CloudPathHidden(rel) { |
| 59 | + if err != nil || !writableName(rel) { |
| 60 | return "", errDenied |
| 61 | } |
| 62 | return rel, nil |
| 63 | |
| 64 | |
| 65 | @@ -90,6 +90,63 @@ |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | +func TestCloudGitMetadataFollowsGrantPermissions(t *testing.T) { |
| 70 | + for _, readonly := range []bool{true, false} { |
| 71 | + t.Run(map[bool]string{true: "read-only", false: "read-write"}[readonly], func(t *testing.T) { |
| 72 | + root := t.TempDir() |
| 73 | + if err := os.MkdirAll(filepath.Join(root, ".git", "refs", "heads"), 0700); err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + if err := os.WriteFile(filepath.Join(root, ".git", "HEAD"), []byte("ref: refs/heads/main\n"), 0600); err != nil { |
| 77 | + t.Fatal(err) |
| 78 | + } |
| 79 | + for _, dir := range []string{".vscode", ".claude", ".codex"} { |
| 80 | + if err := os.Mkdir(filepath.Join(root, dir), 0700); err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + } |
| 84 | + _, c := cloudClient(t, root, readonly) |
| 85 | + files, err := c.ReadDir("/") |
| 86 | + if err != nil || !slices.ContainsFunc(files, func(f os.FileInfo) bool { return f.Name() == ".git" }) { |
| 87 | + t.Fatalf("Git directory hidden: %v", err) |
| 88 | + } |
| 89 | + f, err := c.Open("/.git/HEAD") |
| 90 | + if err != nil { |
| 91 | + t.Fatal(err) |
| 92 | + } |
| 93 | + data, err := io.ReadAll(f) |
| 94 | + f.Close() |
| 95 | + if err != nil || string(data) != "ref: refs/heads/main\n" { |
| 96 | + t.Fatalf("Git HEAD unreadable: %q %v", data, err) |
| 97 | + } |
| 98 | + for _, name := range []string{".git/index", ".git/refs/heads/main", ".git/config", ".gitmodules", ".gitconfig", ".lfsconfig", ".pre-commit-config.yaml", ".git-credentials", ".env", ".npmrc", "private.key", ".vscode/settings.json", ".claude/settings.json", ".codex/config.toml"} { |
| 99 | + f, err = c.Create("/" + name + ".lock") |
| 100 | + if readonly { |
| 101 | + if err == nil { |
| 102 | + f.Close() |
| 103 | + t.Fatal("Git write allowed by read-only grant") |
| 104 | + } |
| 105 | + continue |
| 106 | + } |
| 107 | + if err != nil { |
| 108 | + t.Fatal(err) |
| 109 | + } |
| 110 | + _, err = f.Write([]byte("git-write")) |
| 111 | + f.Close() |
| 112 | + if err != nil { |
| 113 | + t.Fatal(err) |
| 114 | + } |
| 115 | + if err = c.PosixRename("/"+name+".lock", "/"+name); err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + if err = c.Remove("/" + name); err != nil { |
| 119 | + t.Fatal(err) |
| 120 | + } |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | func TestCloudHostileClient(t *testing.T) { |
| 127 | tmp := t.TempDir() |
| 128 | root := filepath.Join(tmp, "project") |
| 129 | @@ -115,12 +172,12 @@ |
| 130 | } |
| 131 | } |
| 132 | _, c := cloudClient(t, root, false) |
| 133 | - for _, p := range []string{"../outside", "/alias", "/symlink", "/inner/config", "/.Git/config", "/.eNv.production", "/id_ed25519", "/file.key"} { |
| 134 | + for _, p := range []string{"../outside", "/alias", "/symlink", "/inner/config"} { |
| 135 | if _, err := c.Open(p); err == nil { |
| 136 | t.Fatalf("opened %s", p) |
| 137 | } |
| 138 | } |
| 139 | - for _, p := range []string{"/alias", "/.Git/config", "/.eNv.production", "/id_ed25519", "/file.key"} { |
| 140 | + for _, p := range []string{"/alias"} { |
| 141 | if _, err := c.Create(p); err == nil { |
| 142 | t.Fatalf("created/truncated %s", p) |
| 143 | } |
| 144 | @@ -134,15 +191,15 @@ |
| 145 | if err := c.Symlink("../outside", "/link"); err == nil { |
| 146 | t.Fatal("symlink created") |
| 147 | } |
| 148 | - if err := c.PosixRename("/alias", "/.env"); err == nil { |
| 149 | - t.Fatal("replaced secret") |
| 150 | + if err := c.PosixRename("/alias", "/.env"); err != nil { |
| 151 | + t.Fatal("project entry rename was blocked by its filename", err) |
| 152 | } |
| 153 | files, err := c.ReadDir("/") |
| 154 | if err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | - if slices.ContainsFunc(files, func(fi os.FileInfo) bool { return CloudPathHidden(fi.Name()) }) { |
| 158 | - t.Fatal("listed protected file") |
| 159 | + if !slices.ContainsFunc(files, func(fi os.FileInfo) bool { return fi.Name() == ".eNv.production" }) { |
| 160 | + t.Fatal("project dotfile was hidden") |
| 161 | } |
| 162 | content, err := os.ReadFile(out) |
| 163 | if err != nil || string(content) != "private" { |
| 164 | |
| 165 | |
| 166 | @@ -26,7 +26,7 @@ |
| 167 | return rootedSys{}, err |
| 168 | } |
| 169 | name, err := normalizedName(h) |
| 170 | - if err != nil || CloudPathHidden(filepath.ToSlash(name)) { |
| 171 | + if err != nil { |
| 172 | windows.CloseHandle(h) |
| 173 | return rootedSys{}, errDenied |
| 174 | } |
| 175 | |
| 176 | |
| 177 | @@ -115,7 +115,7 @@ |
| 178 | if err != nil { |
| 179 | return "", err |
| 180 | } |
| 181 | - if h.isReadonlyName(r) || !writableName(r) || (h.cloud && (r == "." || CloudPathHidden(r))) { |
| 182 | + if h.isReadonlyName(r) || !writableName(r) || (h.cloud && r == ".") { |
| 183 | return "", errDenied |
| 184 | } |
| 185 | if lexicalGuard { |
| 186 | @@ -433,11 +433,6 @@ |
| 187 | } |
| 188 | for len(d.buf) < len(ls) && !d.eof { |
| 189 | fis, err := d.f.Readdir(len(ls) - len(d.buf)) |
| 190 | - if d.cloud { |
| 191 | - fis = slices.DeleteFunc(fis, func(info os.FileInfo) bool { |
| 192 | - return CloudPathHidden(info.Name()) || !(info.IsDir() || info.Mode().IsRegular()) |
| 193 | - }) |
| 194 | - } |
| 195 | d.buf = append(d.buf, fis...) |
| 196 | if errors.Is(err, io.EOF) { |
| 197 | d.eof = true |
| 198 | |
| 199 | |
| 200 | @@ -192,9 +192,6 @@ |
| 201 | rel, err := h.normalizedRel(fh) |
| 202 | if err != nil { |
| 203 | return err |
| 204 | - } |
| 205 | - if h.cloud && CloudPathHidden(rel) { |
| 206 | - return errDenied |
| 207 | } |
| 208 | if rel == "." { |
| 209 | return nil |