Files
Reims-Go/internal/guesttools/drive_share_windows_test.go
T

82 lines
2.9 KiB
Go

//go:build windows
package guesttools
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"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")
makeSharedTestLink(t, outside, link)
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 := newRootWebDAVFS(sharedRoot{path: root}, true, false, nil)
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")
}
}
func TestLegacySharedFilesRejectWindowsJunctionEscape(t *testing.T) {
root, outside := t.TempDir(), t.TempDir()
if err := os.WriteFile(filepath.Join(outside, "secret.txt"), []byte("outside"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "inside.txt"), []byte("inside"), 0o600); err != nil {
t.Fatal(err)
}
makeSharedTestLink(t, outside, filepath.Join(root, "junction"))
srv := NewServer(&memClipboard{})
srv.RegisterVM(VMConfig{VMName: "mac", Token: "token", SharedDir: root})
for _, attempt := range []struct{ method, name string }{
{http.MethodGet, "junction/secret.txt"},
{http.MethodPut, "junction/secret.txt"},
{http.MethodPost, "junction/new-dir/new.txt"},
} {
r := httptest.NewRequest(attempt.method, "/v1/file?path="+url.QueryEscape(attempt.name), strings.NewReader("changed"))
r.Header.Set("Authorization", "Bearer token")
w := httptest.NewRecorder()
srv.handleFile(w, r)
if w.Code < 400 || strings.Contains(w.Body.String(), "\noutside") || w.Body.String() == "outside" {
t.Errorf("%s %q: status=%d body=%q", attempt.method, attempt.name, w.Code, w.Body.String())
}
}
if data, err := os.ReadFile(filepath.Join(outside, "secret.txt")); err != nil || string(data) != "outside" {
t.Fatalf("external file changed: %q, %v", data, err)
}
if entries, err := os.ReadDir(outside); err != nil || len(entries) != 1 {
t.Fatalf("external directory changed: %v, %v", entries, err)
}
r := httptest.NewRequest(http.MethodGet, "/v1/files", nil)
r.Header.Set("Authorization", "Bearer token")
w := httptest.NewRecorder()
srv.handleListFiles(w, r)
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "inside.txt") || strings.Contains(w.Body.String(), "secret.txt") {
t.Fatalf("list shared files: status=%d body=%q", w.Code, w.Body.String())
}
}