fix(builtin): send_file resolves symlinks before the confinement check
This commit is contained in:
1 parent
cf2942c97a
commit
db560fdb8b
3 files changed
+80
-5
No files matched your search
@@ -12,16 +12,33 @@ import (
|
||||
// ErrPathNotAllowed reports a path refused by the workspace confinement.
|
||||
var ErrPathNotAllowed = errors.New("path is outside the configured workspace")
|
||||
|
||||
// ResolveReadable resolves path as the read tool does and returns it when
|
||||
// the read tool would allow it.
|
||||
// ResolveReadable resolves path as the read tool does, then follows
|
||||
// symlinks and re-checks where they lead, so a link inside the workspace
|
||||
// cannot hand out a file outside it. The returned path has no symlinks.
|
||||
func ResolveReadable(cwd, path string) (string, error) {
|
||||
resolved := resolvePath(cwd, path)
|
||||
if !isReadConfined(cwd, resolved) {
|
||||
return "", fmt.Errorf("%w: %s", ErrPathNotAllowed, resolved)
|
||||
}
|
||||
abs, err := filepath.Abs(resolved)
|
||||
target, err := filepath.EvalSymlinks(resolved)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve %s: %w", resolved, err)
|
||||
}
|
||||
return abs, nil
|
||||
target, err = filepath.Abs(target)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve %s: %w", resolved, err)
|
||||
}
|
||||
cache := goModCacheDir()
|
||||
if !isConfined(realDir(cwd), target) && (cache == "" || !isConfined(realDir(cache), target)) {
|
||||
return "", fmt.Errorf("%w: %s links to %s", ErrPathNotAllowed, resolved, target)
|
||||
}
|
||||
return target, nil
|
||||
}
|
||||
|
||||
// realDir returns dir with symlinks resolved, or dir when it cannot be.
|
||||
func realDir(dir string) string {
|
||||
if r, err := filepath.EvalSymlinks(dir); err == nil {
|
||||
return r
|
||||
}
|
||||
return dir
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package builtin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestResolveReadableFollowsSymlinks(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
ws := filepath.Join(root, "ws")
|
||||
outside := filepath.Join(root, "secret")
|
||||
for _, d := range []string{ws, filepath.Join(ws, "sub")} {
|
||||
if err := os.Mkdir(d, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, f := range []string{outside, filepath.Join(ws, "sub", "ok.txt")} {
|
||||
if err := os.WriteFile(f, []byte("x"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
links := map[string]string{"out": outside, "in": filepath.Join(ws, "sub", "ok.txt"), "outdir": root}
|
||||
for name, target := range links {
|
||||
if err := os.Symlink(target, filepath.Join(ws, name)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
linkedWS := filepath.Join(root, "linkedws")
|
||||
if err := os.Symlink(ws, linkedWS); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name, cwd, path string
|
||||
allowed bool
|
||||
}{
|
||||
{"plain file", ws, "sub/ok.txt", true},
|
||||
{"link inside workspace", ws, "in", true},
|
||||
{"link to file outside", ws, "out", false},
|
||||
{"link to dir outside", ws, "outdir/secret", false},
|
||||
{"symlinked workspace", linkedWS, "sub/ok.txt", true},
|
||||
{"lexical escape", ws, "../secret", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := ResolveReadable(tt.cwd, tt.path)
|
||||
if tt.allowed && err != nil {
|
||||
t.Fatalf("want allowed, got %v", err)
|
||||
}
|
||||
if !tt.allowed && !errors.Is(err, ErrPathNotAllowed) {
|
||||
t.Fatalf("want ErrPathNotAllowed, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
// Package sendfile provides send_file: it validates a host file under the
|
||||
// read tool's path policy and returns it for the turn's channel to upload.
|
||||
// read tool's path policy, symlinks resolved, and returns it for the turn's
|
||||
// channel to upload.
|
||||
package sendfile
|
||||
|
||||
import (
|
||||
|
||||
Reference in new issue
Block a user