-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
🐛 bug: align upload root config #3934
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -7,10 +7,16 @@ package fiber | |
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "io/fs" | ||
| "maps" | ||
| "mime/multipart" | ||
| "os" | ||
| pathpkg "path" | ||
| "path/filepath" | ||
| "slices" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
@@ -453,12 +459,26 @@ func (c *DefaultCtx) IsPreflight() bool { | |
| } | ||
|
|
||
| // SaveFile saves any multipart file to disk. | ||
| func (*DefaultCtx) SaveFile(fileheader *multipart.FileHeader, path string) error { | ||
| return fasthttp.SaveMultipartFile(fileheader, path) | ||
| func (c *DefaultCtx) SaveFile(fileheader *multipart.FileHeader, path string) error { | ||
| _, absolutePath, err := resolveUploadPath(c.app, path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := os.MkdirAll(filepath.Dir(absolutePath), c.app.config.RootPerms); err != nil { | ||
| return fmt.Errorf("failed to prepare upload path: %w", err) | ||
| } | ||
|
|
||
| return fasthttp.SaveMultipartFile(fileheader, absolutePath) | ||
| } | ||
|
|
||
| // SaveFileToStorage saves any multipart file to an external storage system. | ||
| func (c *DefaultCtx) SaveFileToStorage(fileheader *multipart.FileHeader, path string, storage Storage) error { | ||
| safePath, _, err := resolveUploadPath(c.app, path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| file, err := fileheader.Open() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open: %w", err) | ||
|
|
@@ -488,13 +508,158 @@ func (c *DefaultCtx) SaveFileToStorage(fileheader *multipart.FileHeader, path st | |
|
|
||
| data := append([]byte(nil), buf.Bytes()...) | ||
|
|
||
| if err := storage.SetWithContext(c.Context(), path, data, 0); err != nil { | ||
| if err := storage.SetWithContext(c.Context(), safePath, data, 0); err != nil { | ||
| return fmt.Errorf("failed to store: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| //nolint:nonamedreturns // names clarify path handling through normalization and validation | ||
| func resolveUploadPath(app *App, path string) (normalizedPath, absolutePath string, err error) { | ||
| if app == nil { | ||
| return "", "", fmt.Errorf("invalid upload root: %w", errors.New("app is nil")) | ||
| } | ||
|
|
||
| uploadRoot, err := getRootDir(app) | ||
| if err != nil { | ||
| return "", "", err | ||
| } | ||
|
|
||
| uploadFS := app.config.RootFS | ||
| if uploadFS == nil { | ||
| uploadFS = os.DirFS(uploadRoot) | ||
| } | ||
|
|
||
| normalizedPath, err = sanitizeUploadPath(path, uploadFS) | ||
| if err != nil { | ||
| return "", "", err | ||
| } | ||
|
|
||
| relativePath := filepath.FromSlash(normalizedPath) | ||
| absolutePath = filepath.Join(uploadRoot, relativePath) | ||
| if !isWithinRoot(uploadRoot, absolutePath) { | ||
| return "", "", errUploadOutsideRoot | ||
| } | ||
|
|
||
| return normalizedPath, absolutePath, nil | ||
| } | ||
|
|
||
| func getRootDir(app *App) (string, error) { | ||
| root := app.config.RootDir | ||
| if root == "" { | ||
| root = "." | ||
| } | ||
|
|
||
| perms := app.config.RootPerms | ||
| if perms == 0 { | ||
| perms = 0o750 | ||
| } | ||
|
|
||
| absoluteRoot, err := filepath.Abs(root) | ||
| if err != nil { | ||
| return "", fmt.Errorf("invalid upload root: %w", err) | ||
| } | ||
|
|
||
| if err = os.MkdirAll(absoluteRoot, perms); err != nil { | ||
| return "", fmt.Errorf("invalid upload root: %w", err) | ||
| } | ||
|
|
||
| resolvedRoot, err := filepath.EvalSymlinks(absoluteRoot) | ||
| if err == nil { | ||
| absoluteRoot = resolvedRoot | ||
| } else if !errors.Is(err, fs.ErrNotExist) { | ||
gaby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return "", fmt.Errorf("invalid upload root: %w", err) | ||
| } | ||
|
Comment on lines
+568
to
+573
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of |
||
|
|
||
| info, err := os.Stat(absoluteRoot) | ||
| if err != nil { | ||
| return "", fmt.Errorf("invalid upload root: %w", err) | ||
| } | ||
|
|
||
| if !info.IsDir() { | ||
| return "", fmt.Errorf("invalid upload root: %s is not a directory", absoluteRoot) | ||
| } | ||
|
|
||
| return absoluteRoot, nil | ||
| } | ||
|
|
||
| func sanitizeUploadPath(path string, uploadFS fs.FS) (string, error) { | ||
| if filepath.IsAbs(path) { | ||
| return "", errUploadAbsolute | ||
| } | ||
|
Comment on lines
+588
to
+590
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rejecting absolute paths ( |
||
|
|
||
| rawNormalized := strings.ReplaceAll(path, "\\", "/") | ||
| if containsParentDir(rawNormalized) { | ||
| return "", errUploadTraversal | ||
| } | ||
|
|
||
| normalized := pathpkg.Clean(rawNormalized) | ||
| normalized = utils.TrimLeft(normalized, '/') | ||
| if normalized == "" || normalized == "." { | ||
| return "", errUploadTraversal | ||
|
Comment on lines
+597
to
+600
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The combination of |
||
| } | ||
|
|
||
| if !fs.ValidPath(normalized) { | ||
| return "", errUploadTraversal | ||
| } | ||
|
Comment on lines
+603
to
+605
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| if err := rejectSymlinkTraversal(uploadFS, normalized); err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return normalized, nil | ||
| } | ||
|
|
||
| func rejectSymlinkTraversal(uploadFS fs.FS, normalized string) error { | ||
| if uploadFS == nil { | ||
| return nil | ||
| } | ||
|
|
||
| parts := strings.Split(normalized, "/") | ||
| current := "." | ||
|
|
||
| for i, part := range parts { | ||
| next := part | ||
| if current != "." { | ||
| next = pathpkg.Join(current, part) | ||
| } | ||
|
|
||
| info, err := fs.Stat(uploadFS, next) | ||
| if err != nil { | ||
|
Comment on lines
+628
to
+629
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Upload validation walks path components with Useful? React with 👍 / 👎. |
||
| if errors.Is(err, fs.ErrNotExist) { | ||
| return nil | ||
| } | ||
| return fmt.Errorf("invalid upload path: %w", err) | ||
| } | ||
|
|
||
| if info.Mode()&fs.ModeSymlink != 0 { | ||
| return errUploadSymlinkPath | ||
| } | ||
|
|
||
| if i < len(parts)-1 && !info.IsDir() { | ||
| return errUploadTraversal | ||
| } | ||
|
|
||
| current = next | ||
| } | ||
|
|
||
| return nil | ||
|
Comment on lines
+614
to
+647
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| } | ||
|
|
||
| func containsParentDir(p string) bool { | ||
| return slices.Contains(strings.Split(p, "/"), "..") | ||
| } | ||
|
|
||
| func isWithinRoot(root, target string) bool { | ||
| rel, err := filepath.Rel(root, target) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| return rel != ".." && !strings.HasPrefix(rel, "../") && rel != "..\\" && !strings.HasPrefix(rel, "..\\") | ||
| } | ||
|
Comment on lines
+654
to
+661
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| // Secure returns whether a secure connection was established. | ||
| func (c *DefaultCtx) Secure() bool { | ||
| return c.Protocol() == schemeHTTPS | ||
|
|
||
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.
The comment for
RootFSsuggests a default ofos.DirFS(RootDir). While this is indeed the fallback logic inresolveUploadPath, theNewfunction itself doesn't explicitly set this default. It might be clearer to either setapp.config.RootFS = os.DirFS(app.config.RootDir)in theNewfunction (afterRootDiris initialized) or adjust the comment to reflect that it's a runtime default if not explicitly configured.