forked from MatiasDesuu/ThinkDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.go
More file actions
266 lines (229 loc) · 6.58 KB
/
Copy pathbackup.go
File metadata and controls
266 lines (229 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"archive/zip"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
)
// validateBookmarkURL checks if the bookmark URL has a safe scheme (http or https)
func validateBookmarkURL(bookmarkURL string) error {
if bookmarkURL == "" {
return nil // Allow empty URLs
}
parsedURL, err := url.Parse(bookmarkURL)
if err != nil {
return fmt.Errorf("invalid URL format")
}
// Only allow http and https schemes
if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
return fmt.Errorf("URL scheme '%s' is not allowed. Only http and https are permitted", parsedURL.Scheme)
}
return nil
}
// isValidImportFilename validates that the filename is safe and allowed for import
func (h *Handlers) isValidImportFilename(filename string) bool {
// Prevent path traversal, but allow icons/ subdirectory
if strings.Contains(filename, "..") {
return false
}
if strings.Contains(filename, "\\") {
return false
}
// Allow / only in icons/ prefix
if strings.Contains(filename, "/") && !strings.HasPrefix(filename, "icons/") {
return false
}
// Allow only specific filenames with their extensions
allowedFiles := []string{
"settings.json",
"colors.json",
"pages.json",
"finders.json",
"favicon.ico",
"favicon.png",
"favicon.jpg",
"favicon.gif",
"font.woff",
"font.woff2",
"font.ttf",
"font.otf",
}
// Check if it's one of the specific files
for _, allowed := range allowedFiles {
if filename == allowed {
return true
}
}
// Check if it's a bookmarks file (bookmarks- followed by digits and .json)
if strings.HasPrefix(filename, "bookmarks-") && strings.HasSuffix(filename, ".json") {
// Extract the number part
numberPart := strings.TrimPrefix(strings.TrimSuffix(filename, ".json"), "bookmarks-")
if _, err := strconv.Atoi(numberPart); err == nil {
return true
}
}
// Check if it's an image file in root data directory
if !strings.Contains(filename, "/") {
validImageExtensions := []string{".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico", ".webp"}
for _, ext := range validImageExtensions {
if strings.HasSuffix(filename, ext) {
return true
}
}
}
// Check if it's an icon file (icons/ followed by filename with image extension)
if strings.HasPrefix(filename, "icons/") {
// Allow common image extensions
validExtensions := []string{".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico", ".webp"}
for _, ext := range validExtensions {
if strings.HasSuffix(filename, ext) {
return true
}
}
}
return false
}
// Import handles the import of backup files
func (h *Handlers) Import(w http.ResponseWriter, r *http.Request) {
// Parse multipart form
err := r.ParseMultipartForm(32 << 20) // 32MB max
if err != nil {
http.Error(w, "Failed to parse form", http.StatusBadRequest)
return
}
files := r.MultipartForm.File["files"]
if len(files) == 0 {
http.Error(w, "No files provided", http.StatusBadRequest)
return
}
// Process each file
for _, fileHeader := range files {
filename := fileHeader.Filename
// Normalize path separators to /
filename = strings.ReplaceAll(filename, "\\", "/")
fmt.Printf("Processing file: %s\n", filename)
// Validate filename to prevent path traversal and ensure only allowed files
if !h.isValidImportFilename(filename) {
fmt.Printf("Invalid filename: %s\n", filename)
http.Error(w, fmt.Sprintf("Invalid filename: %s", filename), http.StatusBadRequest)
return
}
file, err := fileHeader.Open()
if err != nil {
http.Error(w, "Failed to open file", http.StatusInternalServerError)
return
}
defer file.Close()
// Read file content
content, err := io.ReadAll(file)
if err != nil {
http.Error(w, "Failed to read file", http.StatusInternalServerError)
return
}
// Validate JSON content for JSON files
if strings.HasSuffix(filename, ".json") {
if !json.Valid(content) {
fmt.Printf("Invalid JSON in file: %s\n", filename)
http.Error(w, fmt.Sprintf("Invalid JSON content in file: %s", filename), http.StatusBadRequest)
return
}
}
// Determine destination path
var destPath string
if strings.HasPrefix(filename, "favicon.") {
destPath = filename
} else if !strings.Contains(filename, "/") {
// Check if it's an image file that should go to icons/
validImageExtensions := []string{".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico", ".webp"}
isImage := false
for _, ext := range validImageExtensions {
if strings.HasSuffix(filename, ext) {
isImage = true
break
}
}
if isImage {
destPath = filepath.Join("data", "icons", filename)
} else {
destPath = filepath.Join("data", filename)
}
} else {
destPath = filepath.Join("data", filename)
}
// Ensure the directory exists
dir := filepath.Dir(destPath)
err = os.MkdirAll(dir, 0755)
if err != nil {
http.Error(w, "Failed to create directory", http.StatusInternalServerError)
return
}
// Write file
err = os.WriteFile(destPath, content, 0644)
if err != nil {
http.Error(w, "Failed to write file", http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Import successful"))
}
// Backup creates a zip file with all data from the data directory
func (h *Handlers) Backup(w http.ResponseWriter, r *http.Request) {
// Create a buffer to write our archive to
buf := new(bytes.Buffer)
// Create a new zip archive
zipWriter := zip.NewWriter(buf)
// Walk through the data directory
dataDir := "data"
err := filepath.Walk(dataDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip directories
if info.IsDir() {
return nil
}
// Create a relative path for the zip entry
relPath, err := filepath.Rel(dataDir, path)
if err != nil {
return err
}
// Create zip file entry
zipFile, err := zipWriter.Create(relPath)
if err != nil {
return err
}
// Open the file
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// Copy file content to zip
_, err = io.Copy(zipFile, file)
return err
})
if err != nil {
http.Error(w, "Failed to create backup", http.StatusInternalServerError)
return
}
// Close the zip writer
err = zipWriter.Close()
if err != nil {
http.Error(w, "Failed to finalize backup", http.StatusInternalServerError)
return
}
// Set headers for file download
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", "attachment; filename=thinkdashboard-backup.zip")
w.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
// Write the zip content to response
w.Write(buf.Bytes())
}