package server import ( "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" ) func postHomeFile(t *testing.T, home, dir, name, body string) *httptest.ResponseRecorder { t.Helper() req := httptest.NewRequest(http.MethodPost, "/files?path="+dir, strings.NewReader(body)) if name != "" { req.Header.Set(chatUploadHeader, name) } req.Header.Set("Content-Type", "text/plain") rec := httptest.NewRecorder() serveHomeFileUploadTo(rec, req, home) return rec } func TestServeHomeFileUpload(t *testing.T) { root := t.TempDir() home := filepath.Join(root, "home") outside := filepath.Join(root, "outside") for _, dir := range []string{filepath.Join(home, "project"), outside} { if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } } if err := os.WriteFile(filepath.Join(home, "project", "kept.txt"), []byte("original"), 0o644); err != nil { t.Fatal(err) } if err := os.Symlink(outside, filepath.Join(home, "escape")); err != nil { t.Fatal(err) } t.Run("stores a new file in the shown folder", func(t *testing.T) { rec := postHomeFile(t, home, "~/project", "big%20photo.txt", "hello upload") if rec.Code != http.StatusOK { t.Fatalf("status = %d %s", rec.Code, rec.Body) } var reply homeFileUploadReply if err := json.Unmarshal(rec.Body.Bytes(), &reply); err != nil { t.Fatal(err) } want := filepath.Join(home, "project", "big photo.txt") if reply.Name != "big photo.txt" || reply.Path != want || reply.Size != int64(len("hello upload")) { t.Fatalf("reply = %+v", reply) } info, err := os.Stat(want) if err != nil || info.Mode().Perm() != 0o644 { t.Fatalf("stored file = %v %v", info, err) } data, _ := os.ReadFile(want) if string(data) != "hello upload" { t.Fatalf("contents = %q", data) } }) t.Run("operator home seam and absolute path", func(t *testing.T) { originalHomeDirectory := operatorHomeDirectory operatorHomeDirectory = func() (string, error) { return home, nil } t.Cleanup(func() { operatorHomeDirectory = originalHomeDirectory }) req := httptest.NewRequest(http.MethodPost, "/files?path="+filepath.Join(home, "project"), strings.NewReader("x")) req.Header.Set(chatUploadHeader, "seam.txt") rec := httptest.NewRecorder() serveHomeFileUpload(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d %s", rec.Code, rec.Body) } if _, err := os.Stat(filepath.Join(home, "project", "seam.txt")); err != nil { t.Fatal(err) } }) t.Run("existing file is refused and kept", func(t *testing.T) { rec := postHomeFile(t, home, "~/project", "kept.txt", "replacement") if rec.Code != http.StatusConflict || !strings.Contains(rec.Body.String(), "already exists") { t.Fatalf("response = %d %s", rec.Code, rec.Body) } data, _ := os.ReadFile(filepath.Join(home, "project", "kept.txt")) if string(data) != "original" { t.Fatalf("contents = %q, want original", data) } }) t.Run("empty file is stored", func(t *testing.T) { rec := postHomeFile(t, home, "~", "empty.txt", "") if rec.Code != http.StatusOK { t.Fatalf("status = %d %s", rec.Code, rec.Body) } info, err := os.Stat(filepath.Join(home, "empty.txt")) if err != nil || info.Size() != 0 { t.Fatalf("stored file = %v %v", info, err) } }) refusals := []struct { label string dir string name string want int }{ {"missing name", "~/project", "", http.StatusBadRequest}, {"name with slash", "~/project", "sub%2Fname.txt", http.StatusBadRequest}, {"dot dot name", "~/project", "..", http.StatusBadRequest}, {"outside home", filepath.Join(home, "..", "outside"), "x.txt", http.StatusForbidden}, {"symlink outside home", "~/escape", "x.txt", http.StatusForbidden}, {"missing folder", "~/nowhere", "x.txt", http.StatusNotFound}, {"file is not a folder", "~/project/kept.txt", "x.txt", http.StatusNotFound}, {"relative path", "project", "x.txt", http.StatusNotFound}, } for _, tc := range refusals { t.Run(tc.label, func(t *testing.T) { rec := postHomeFile(t, home, tc.dir, tc.name, "x") if rec.Code != tc.want { t.Fatalf("status = %d %s, want %d", rec.Code, rec.Body, tc.want) } }) } if entries, _ := os.ReadDir(outside); len(entries) != 0 { t.Fatalf("outside directory gained %d entries", len(entries)) } t.Run("declared size over the limit", func(t *testing.T) { req := httptest.NewRequest(http.MethodPost, "/files?path=~/project", strings.NewReader("x")) req.Header.Set(chatUploadHeader, "huge.bin") req.ContentLength = maxHomeFileUploadBytes + 1 rec := httptest.NewRecorder() serveHomeFileUploadTo(rec, req, home) if rec.Code != http.StatusRequestEntityTooLarge { t.Fatalf("status = %d %s", rec.Code, rec.Body) } if _, err := os.Stat(filepath.Join(home, "project", "huge.bin")); !os.IsNotExist(err) { t.Fatalf("oversized file was stored: %v", err) } }) t.Run("cross-site post is refused", func(t *testing.T) { req := httptest.NewRequest(http.MethodPost, "/files?path=~/project", strings.NewReader("x")) req.Header.Set(chatUploadHeader, "cross.txt") req.Header.Set("Sec-Fetch-Site", "cross-site") rec := httptest.NewRecorder() serveHomeFileUploadTo(rec, req, home) if rec.Code != http.StatusForbidden { t.Fatalf("status = %d %s", rec.Code, rec.Body) } }) }