仰望星辰工作室

miaopanel

miaopanel/ internal/httpapi/files_test.go 6.0 KB · 203 行 原始文件
1package httpapi
2
3import (
4 "bytes"
5 "encoding/json"
6 "io"
7 "mime/multipart"
8 "net/http"
9 "net/url"
10 "strings"
11 "testing"
12)
13
14func (h *harness) raw(t *testing.T, method, path string, body io.Reader, contentType, csrf string) *http.Response {
15 t.Helper()
16 req, err := http.NewRequest(method, h.url+"/"+testEntry+path, body)
17 if err != nil {
18 t.Fatal(err)
19 }
20 if contentType != "" {
21 req.Header.Set("Content-Type", contentType)
22 }
23 if csrf != "" {
24 req.Header.Set("X-CSRF-Token", csrf)
25 }
26 req.Header.Set("Origin", h.url)
27 resp, err := h.cli.Do(req)
28 if err != nil {
29 t.Fatal(err)
30 }
31 return resp
32}
33
34func TestFileAPICRUD(t *testing.T) {
35 h := newHarness(t)
36 csrf := h.login(t)
37
38 code, env := h.do(t, http.MethodPost, "/api/v1/files/mkdir", map[string]string{"path": "/assets"}, csrf)
39 if code != http.StatusOK {
40 t.Fatalf("mkdir status %d: %v", code, env)
41 }
42
43 code, env = h.do(t, http.MethodPut, "/api/v1/files/write",
44 map[string]string{"path": "/assets/app.js", "content": "console.log(1)"}, csrf)
45 if code != http.StatusOK {
46 t.Fatalf("write status %d: %v", code, env)
47 }
48
49 code, env = h.do(t, http.MethodGet, "/api/v1/files/read?path=%2Fassets%2Fapp.js", nil, "")
50 if code != http.StatusOK {
51 t.Fatalf("read status %d", code)
52 }
53 data := env["data"].(map[string]any)
54 if data["content"] != "console.log(1)" {
55 t.Fatalf("content = %v", data["content"])
56 }
57
58 code, env = h.do(t, http.MethodGet, "/api/v1/files?path=%2Fassets", nil, "")
59 if code != http.StatusOK {
60 t.Fatalf("list status %d", code)
61 }
62 entries := env["data"].(map[string]any)["entries"].([]any)
63 if len(entries) != 1 || entries[0].(map[string]any)["name"] != "app.js" {
64 t.Fatalf("unexpected listing: %v", entries)
65 }
66
67 code, env = h.do(t, http.MethodPost, "/api/v1/files/rename",
68 map[string]string{"path": "/assets/app.js", "name": "main.js"}, csrf)
69 if code != http.StatusOK {
70 t.Fatalf("rename status %d: %v", code, env)
71 }
72
73 code, env = h.do(t, http.MethodPost, "/api/v1/files/move",
74 map[string]string{"src": "/assets/main.js", "dst": "/main.js"}, csrf)
75 if code != http.StatusOK {
76 t.Fatalf("move status %d: %v", code, env)
77 }
78
79 code, env = h.do(t, http.MethodPost, "/api/v1/files/compress",
80 map[string]string{"path": "/assets", "format": "zip"}, csrf)
81 if code != http.StatusOK {
82 t.Fatalf("compress status %d: %v", code, env)
83 }
84
85 code, env = h.do(t, http.MethodPost, "/api/v1/files/extract",
86 map[string]string{"path": "/assets.zip", "dest": "/out"}, csrf)
87 if code != http.StatusOK {
88 t.Fatalf("extract status %d: %v", code, env)
89 }
90
91 code, env = h.do(t, http.MethodDelete, "/api/v1/files?path=%2Fmain.js", nil, csrf)
92 if code != http.StatusOK {
93 t.Fatalf("delete status %d: %v", code, env)
94 }
95 item := env["data"].(map[string]any)
96 trashID, _ := item["id"].(string)
97 if trashID == "" {
98 t.Fatal("trash id missing")
99 }
100
101 code, env = h.do(t, http.MethodGet, "/api/v1/files/trash", nil, "")
102 if code != http.StatusOK || len(env["data"].([]any)) != 1 {
103 t.Fatalf("trash list: %d %v", code, env)
104 }
105
106 code, _ = h.do(t, http.MethodPost, "/api/v1/files/trash/restore", map[string]string{"id": trashID}, csrf)
107 if code != http.StatusOK {
108 t.Fatalf("restore status %d", code)
109 }
110
111 resp := h.raw(t, http.MethodGet, "/api/v1/files/download?path=%2Fmain.js", nil, "", "")
112 defer resp.Body.Close()
113 body, _ := io.ReadAll(resp.Body)
114 if resp.StatusCode != http.StatusOK || string(body) != "console.log(1)" {
115 t.Fatalf("download: %d %q", resp.StatusCode, string(body))
116 }
117 if cd := resp.Header.Get("Content-Disposition"); !strings.Contains(cd, "attachment") {
118 t.Fatalf("content disposition = %q", cd)
119 }
120}
121
122func TestFileAPITraversal(t *testing.T) {
123 h := newHarness(t)
124 csrf := h.login(t)
125 for _, p := range []string{"%2F..%2Fetc%2Fpasswd", "%2F.%2E%2Fsecret", "%2Fassets%2F..%2F..%2Fetc"} {
126 code, _ := h.do(t, http.MethodGet, "/api/v1/files/read?path="+p, nil, "")
127 if code == http.StatusOK {
128 t.Fatalf("traversal %s accepted", p)
129 }
130 }
131 code, _ := h.do(t, http.MethodPut, "/api/v1/files/write",
132 map[string]string{"path": "/../escape.txt", "content": "x"}, csrf)
133 if code != http.StatusBadRequest {
134 t.Fatalf("traversal write status = %d", code)
135 }
136}
137
138func TestFileUpload(t *testing.T) {
139 h := newHarness(t)
140 csrf := h.login(t)
141
142 var buf bytes.Buffer
143 mw := multipart.NewWriter(&buf)
144 part, err := mw.CreateFormFile("file", "upload.txt")
145 if err != nil {
146 t.Fatal(err)
147 }
148 _, _ = part.Write([]byte("hello upload"))
149 _ = mw.Close()
150
151 resp := h.raw(t, http.MethodPost, "/api/v1/files/upload?dir=%2F", &buf, mw.FormDataContentType(), csrf)
152 defer resp.Body.Close()
153 if resp.StatusCode != http.StatusOK {
154 body, _ := io.ReadAll(resp.Body)
155 t.Fatalf("upload status %d: %s", resp.StatusCode, body)
156 }
157 var env struct {
158 Code int `json:"code"`
159 Data struct {
160 Path string `json:"path"`
161 Size int64 `json:"size"`
162 } `json:"data"`
163 }
164 if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
165 t.Fatal(err)
166 }
167 if env.Data.Path != "/upload.txt" || env.Data.Size != 12 {
168 t.Fatalf("upload result: %+v", env)
169 }
170
171 resp2 := h.raw(t, http.MethodGet, "/api/v1/files/download?path="+url.QueryEscape("/upload.txt"), nil, "", "")
172 defer resp2.Body.Close()
173 body, _ := io.ReadAll(resp2.Body)
174 if string(body) != "hello upload" {
175 t.Fatalf("downloaded %q", string(body))
176 }
177}
178
179func TestTerminalAuth(t *testing.T) {
180 h := newHarness(t)
181 // No session at all.
182 resp := h.raw(t, http.MethodGet, "/api/v1/ws/terminal", nil, "", "")
183 resp.Body.Close()
184 if resp.StatusCode != http.StatusUnauthorized {
185 t.Fatalf("unauthenticated ws status = %d", resp.StatusCode)
186 }
187
188 csrf := h.login(t)
189 // Authenticated but wrong CSRF token.
190 resp = h.raw(t, http.MethodGet, "/api/v1/ws/terminal?csrf=wrong", nil, "", "")
191 resp.Body.Close()
192 if resp.StatusCode != http.StatusForbidden {
193 t.Fatalf("bad csrf ws status = %d", resp.StatusCode)
194 }
195
196 // Authenticated with the right token but no upgrade headers: the upgrader
197 // must refuse the plain request.
198 resp = h.raw(t, http.MethodGet, "/api/v1/ws/terminal?csrf="+url.QueryEscape(csrf), nil, "", "")
199 resp.Body.Close()
200 if resp.StatusCode == http.StatusSwitchingProtocols {
201 t.Fatal("plain GET was upgraded")
202 }
203}