package httpapi import ( "bytes" "encoding/json" "io" "mime/multipart" "net/http" "net/url" "strings" "testing" ) func (h *harness) raw(t *testing.T, method, path string, body io.Reader, contentType, csrf string) *http.Response { t.Helper() req, err := http.NewRequest(method, h.url+"/"+testEntry+path, body) if err != nil { t.Fatal(err) } if contentType != "" { req.Header.Set("Content-Type", contentType) } if csrf != "" { req.Header.Set("X-CSRF-Token", csrf) } req.Header.Set("Origin", h.url) resp, err := h.cli.Do(req) if err != nil { t.Fatal(err) } return resp } func TestFileAPICRUD(t *testing.T) { h := newHarness(t) csrf := h.login(t) code, env := h.do(t, http.MethodPost, "/api/v1/files/mkdir", map[string]string{"path": "/assets"}, csrf) if code != http.StatusOK { t.Fatalf("mkdir status %d: %v", code, env) } code, env = h.do(t, http.MethodPut, "/api/v1/files/write", map[string]string{"path": "/assets/app.js", "content": "console.log(1)"}, csrf) if code != http.StatusOK { t.Fatalf("write status %d: %v", code, env) } code, env = h.do(t, http.MethodGet, "/api/v1/files/read?path=%2Fassets%2Fapp.js", nil, "") if code != http.StatusOK { t.Fatalf("read status %d", code) } data := env["data"].(map[string]any) if data["content"] != "console.log(1)" { t.Fatalf("content = %v", data["content"]) } code, env = h.do(t, http.MethodGet, "/api/v1/files?path=%2Fassets", nil, "") if code != http.StatusOK { t.Fatalf("list status %d", code) } entries := env["data"].(map[string]any)["entries"].([]any) if len(entries) != 1 || entries[0].(map[string]any)["name"] != "app.js" { t.Fatalf("unexpected listing: %v", entries) } code, env = h.do(t, http.MethodPost, "/api/v1/files/rename", map[string]string{"path": "/assets/app.js", "name": "main.js"}, csrf) if code != http.StatusOK { t.Fatalf("rename status %d: %v", code, env) } code, env = h.do(t, http.MethodPost, "/api/v1/files/move", map[string]string{"src": "/assets/main.js", "dst": "/main.js"}, csrf) if code != http.StatusOK { t.Fatalf("move status %d: %v", code, env) } code, env = h.do(t, http.MethodPost, "/api/v1/files/compress", map[string]string{"path": "/assets", "format": "zip"}, csrf) if code != http.StatusOK { t.Fatalf("compress status %d: %v", code, env) } code, env = h.do(t, http.MethodPost, "/api/v1/files/extract", map[string]string{"path": "/assets.zip", "dest": "/out"}, csrf) if code != http.StatusOK { t.Fatalf("extract status %d: %v", code, env) } code, env = h.do(t, http.MethodDelete, "/api/v1/files?path=%2Fmain.js", nil, csrf) if code != http.StatusOK { t.Fatalf("delete status %d: %v", code, env) } item := env["data"].(map[string]any) trashID, _ := item["id"].(string) if trashID == "" { t.Fatal("trash id missing") } code, env = h.do(t, http.MethodGet, "/api/v1/files/trash", nil, "") if code != http.StatusOK || len(env["data"].([]any)) != 1 { t.Fatalf("trash list: %d %v", code, env) } code, _ = h.do(t, http.MethodPost, "/api/v1/files/trash/restore", map[string]string{"id": trashID}, csrf) if code != http.StatusOK { t.Fatalf("restore status %d", code) } resp := h.raw(t, http.MethodGet, "/api/v1/files/download?path=%2Fmain.js", nil, "", "") defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) if resp.StatusCode != http.StatusOK || string(body) != "console.log(1)" { t.Fatalf("download: %d %q", resp.StatusCode, string(body)) } if cd := resp.Header.Get("Content-Disposition"); !strings.Contains(cd, "attachment") { t.Fatalf("content disposition = %q", cd) } } func TestFileAPITraversal(t *testing.T) { h := newHarness(t) csrf := h.login(t) for _, p := range []string{"%2F..%2Fetc%2Fpasswd", "%2F.%2E%2Fsecret", "%2Fassets%2F..%2F..%2Fetc"} { code, _ := h.do(t, http.MethodGet, "/api/v1/files/read?path="+p, nil, "") if code == http.StatusOK { t.Fatalf("traversal %s accepted", p) } } code, _ := h.do(t, http.MethodPut, "/api/v1/files/write", map[string]string{"path": "/../escape.txt", "content": "x"}, csrf) if code != http.StatusBadRequest { t.Fatalf("traversal write status = %d", code) } } func TestFileUpload(t *testing.T) { h := newHarness(t) csrf := h.login(t) var buf bytes.Buffer mw := multipart.NewWriter(&buf) part, err := mw.CreateFormFile("file", "upload.txt") if err != nil { t.Fatal(err) } _, _ = part.Write([]byte("hello upload")) _ = mw.Close() resp := h.raw(t, http.MethodPost, "/api/v1/files/upload?dir=%2F", &buf, mw.FormDataContentType(), csrf) defer resp.Body.Close() if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) t.Fatalf("upload status %d: %s", resp.StatusCode, body) } var env struct { Code int `json:"code"` Data struct { Path string `json:"path"` Size int64 `json:"size"` } `json:"data"` } if err := json.NewDecoder(resp.Body).Decode(&env); err != nil { t.Fatal(err) } if env.Data.Path != "/upload.txt" || env.Data.Size != 12 { t.Fatalf("upload result: %+v", env) } resp2 := h.raw(t, http.MethodGet, "/api/v1/files/download?path="+url.QueryEscape("/upload.txt"), nil, "", "") defer resp2.Body.Close() body, _ := io.ReadAll(resp2.Body) if string(body) != "hello upload" { t.Fatalf("downloaded %q", string(body)) } } func TestTerminalAuth(t *testing.T) { h := newHarness(t) // No session at all. resp := h.raw(t, http.MethodGet, "/api/v1/ws/terminal", nil, "", "") resp.Body.Close() if resp.StatusCode != http.StatusUnauthorized { t.Fatalf("unauthenticated ws status = %d", resp.StatusCode) } csrf := h.login(t) // Authenticated but wrong CSRF token. resp = h.raw(t, http.MethodGet, "/api/v1/ws/terminal?csrf=wrong", nil, "", "") resp.Body.Close() if resp.StatusCode != http.StatusForbidden { t.Fatalf("bad csrf ws status = %d", resp.StatusCode) } // Authenticated with the right token but no upgrade headers: the upgrader // must refuse the plain request. resp = h.raw(t, http.MethodGet, "/api/v1/ws/terminal?csrf="+url.QueryEscape(csrf), nil, "", "") resp.Body.Close() if resp.StatusCode == http.StatusSwitchingProtocols { t.Fatal("plain GET was upgraded") } }