-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Go: Update go/path-injection docs to include more sanitizers
#20507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package main | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func handleFileWithSanitizers(w http.ResponseWriter, r *http.Request) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userPath := r.URL.Query().Get("file") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| safeDir := "/home/user/documents" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Method 1: Using filepath.IsLocal to validate the path is local | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !filepath.IsLocal(userPath) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| http.Error(w, "Invalid path: must be local", http.StatusBadRequest) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Method 2: Using strings.Contains to check for path traversal sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if strings.Contains(userPath, "..") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| http.Error(w, "Invalid path: contains path traversal", http.StatusBadRequest) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Method 3: Using filepath.Rel to ensure path is within safe directory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| relPath, err := filepath.Rel(safeDir, filepath.Join(safeDir, userPath)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil || strings.HasPrefix(relPath, "..") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| http.Error(w, "Invalid path: outside safe directory", http.StatusBadRequest) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Method 4: Using filepath.Clean with absolute prefix for normalization | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cleanPath := filepath.Clean("/" + userPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if strings.Contains(cleanPath, "..") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| http.Error(w, "Invalid path after normalization", http.StatusBadRequest) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Method 5: Using strings.HasPrefix for additional validation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| finalPath := filepath.Join(safeDir, userPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !strings.HasPrefix(finalPath, safeDir) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !strings.HasPrefix(finalPath, safeDir) { | |
| absFinalPath, err := filepath.Abs(finalPath) | |
| if err != nil { | |
| http.Error(w, "Invalid path: could not resolve absolute path", http.StatusBadRequest) | |
| return | |
| } | |
| absSafeDir, err := filepath.Abs(safeDir) | |
| if err != nil { | |
| http.Error(w, "Invalid safe directory", http.StatusInternalServerError) | |
| return | |
| } | |
| if !strings.HasPrefix(absFinalPath, absSafeDir+string(os.PathSeparator)) && absFinalPath != absSafeDir { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect: the result of filepath.Join is Clean'd.
Copilot
AI
Sep 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sanitization method has a logical flaw. The filepath.Join(safeDir, userPath) operation happens before validation, which means a malicious userPath like ../../../etc/passwd could escape the safe directory before filepath.Rel is called. Consider validating userPath first or using filepath.Rel(safeDir, userPath) directly to check if the user path would escape the safe directory.
| // Method 3: Using filepath.Rel to ensure path is within safe directory | |
| relPath, err := filepath.Rel(safeDir, filepath.Join(safeDir, userPath)) | |
| if err != nil || strings.HasPrefix(relPath, "..") { | |
| http.Error(w, "Invalid path: outside safe directory", http.StatusBadRequest) | |
| return | |
| } | |
| // Method 4: Using filepath.Clean with absolute prefix for normalization | |
| cleanPath := filepath.Clean("/" + userPath) | |
| if strings.Contains(cleanPath, "..") { | |
| http.Error(w, "Invalid path after normalization", http.StatusBadRequest) | |
| return | |
| } | |
| // Method 5: Using strings.HasPrefix for additional validation | |
| finalPath := filepath.Join(safeDir, userPath) | |
| if !strings.HasPrefix(finalPath, safeDir) { | |
| http.Error(w, "Invalid path: must be within safe directory", http.StatusBadRequest) | |
| return | |
| } | |
| // Safe to open file | |
| file, err := os.Open(finalPath) | |
| // Robust: Resolve absolute path and ensure it's within safeDir | |
| absSafeDir, err := filepath.Abs(safeDir) | |
| if err != nil { | |
| http.Error(w, "Internal error: cannot resolve safe directory", http.StatusInternalServerError) | |
| return | |
| } | |
| absFilePath, err := filepath.Abs(filepath.Join(absSafeDir, userPath)) | |
| if err != nil { | |
| http.Error(w, "Invalid path", http.StatusBadRequest) | |
| return | |
| } | |
| rel, err := filepath.Rel(absSafeDir, absFilePath) | |
| if err != nil || strings.HasPrefix(rel, "..") || filepath.IsAbs(rel) { | |
| http.Error(w, "Invalid path: outside safe directory", http.StatusBadRequest) | |
| return | |
| } | |
| // Safe to open file | |
| file, err := os.Open(absFilePath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This description is incorrect.
filepath.Rel()does not return an error when the target path is outside the base path. It returns a relative path that may contain..sequences to reach the target. The error checking should focus on whether the result contains..prefixes, not just if an error is returned.