42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
//go:build windows
|
|
|
|
package guesttools
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestReadOnlyDriveFSRejectsWindowsJunctionEscape(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
root := filepath.Join(tmp, "root")
|
|
outside := filepath.Join(tmp, "outside")
|
|
if err := os.MkdirAll(root, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(outside, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
secret := filepath.Join(outside, "secret.txt")
|
|
if err := os.WriteFile(secret, []byte("outside"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
link := filepath.Join(root, "junction")
|
|
if output, err := exec.Command("cmd.exe", "/c", "mklink", "/J", link, outside).CombinedOutput(); err != nil {
|
|
t.Fatalf("create test junction: %v: %s", err, output)
|
|
}
|
|
if body, err := os.ReadFile(filepath.Join(link, "secret.txt")); err != nil || string(body) != "outside" {
|
|
t.Fatalf("test junction is not usable: %q, %v", body, err)
|
|
}
|
|
fsys, err := newReadOnlyDriveFS(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := fsys.OpenFile(context.Background(), "junction/secret.txt", os.O_RDONLY, 0); err == nil {
|
|
t.Fatal("junction escape unexpectedly succeeded")
|
|
}
|
|
}
|