|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +func handleFileWithSanitizers(w http.ResponseWriter, r *http.Request) { |
| 11 | + userPath := r.URL.Query().Get("file") |
| 12 | + safeDir := "/home/user/documents" |
| 13 | + |
| 14 | + // Method 1: Using filepath.IsLocal to validate the path is local |
| 15 | + if !filepath.IsLocal(userPath) { |
| 16 | + http.Error(w, "Invalid path: must be local", http.StatusBadRequest) |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + // Method 2: Using strings.Contains to check for path traversal sequences |
| 21 | + if strings.Contains(userPath, "..") { |
| 22 | + http.Error(w, "Invalid path: contains path traversal", http.StatusBadRequest) |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + // Method 3: Using filepath.Rel to ensure path is within safe directory |
| 27 | + relPath, err := filepath.Rel(safeDir, filepath.Join(safeDir, userPath)) |
| 28 | + if err != nil || strings.HasPrefix(relPath, "..") { |
| 29 | + http.Error(w, "Invalid path: outside safe directory", http.StatusBadRequest) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + // Method 4: Using filepath.Clean with absolute prefix for normalization |
| 34 | + cleanPath := filepath.Clean("/" + userPath) |
| 35 | + if strings.Contains(cleanPath, "..") { |
| 36 | + http.Error(w, "Invalid path after normalization", http.StatusBadRequest) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + // Method 5: Using strings.HasPrefix for additional validation |
| 41 | + finalPath := filepath.Join(safeDir, userPath) |
| 42 | + if !strings.HasPrefix(finalPath, safeDir) { |
| 43 | + http.Error(w, "Invalid path: must be within safe directory", http.StatusBadRequest) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + // Safe to open file |
| 48 | + file, err := os.Open(finalPath) |
| 49 | + if err != nil { |
| 50 | + http.Error(w, "File not found", http.StatusNotFound) |
| 51 | + return |
| 52 | + } |
| 53 | + defer file.Close() |
| 54 | + |
| 55 | + // Process file... |
| 56 | +} |
| 57 | + |
| 58 | +// Example using mime/multipart filename sanitization |
| 59 | +func handleUpload(w http.ResponseWriter, r *http.Request) { |
| 60 | + err := r.ParseMultipartForm(32 << 20) // 32MB max |
| 61 | + if err != nil { |
| 62 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + file, header, err := r.FormFile("upload") |
| 67 | + if err != nil { |
| 68 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 69 | + return |
| 70 | + } |
| 71 | + defer file.Close() |
| 72 | + |
| 73 | + // The Filename field is automatically sanitized by mime/multipart |
| 74 | + // using filepath.Base, making it safe from path traversal |
| 75 | + filename := header.Filename |
| 76 | + |
| 77 | + // Additional validation can still be useful |
| 78 | + if strings.Contains(filename, "..") || strings.ContainsAny(filename, "/\\") { |
| 79 | + http.Error(w, "Invalid filename", http.StatusBadRequest) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + // Safe to use filename |
| 84 | + _ = filename |
| 85 | +} |
0 commit comments