From c92f4bf41083dff65caa7d9f82b870fea52024a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ngh=C4=A9a=20Nguy=E1=BB=85n=20Ng=E1=BB=8Dc?= Date: Sat, 9 May 2026 10:02:46 +0700 Subject: [PATCH] fix(context): only chmod newly created dirs in SaveUploadedFile Fixes #4622 - SaveUploadedFile no longer attempts to modify permissions on existing directories, which breaks saving to /tmp and other system directories. - Check if directory exists before calling os.MkdirAll - Only call os.Chmod if directory was newly created - Preserves backward compatibility for newly created dirs - Allows saving files to existing system directories This regression was introduced in v1.12.0 when chmod support was added, but was incorrectly applied to all directories. BREAKING CHANGE REVERTED: Restores v1.10.1 behavior for existing dirs Co-authored-by: Nghia Nguyen --- context.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index 5174033e..368dc719 100644 --- a/context.go +++ b/context.go @@ -728,11 +728,17 @@ func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string, perm mode = perm[0] } dir := filepath.Dir(dst) + // Check if directory exists to only chmod newly created directories + _, statErr := os.Stat(dir) + dirExists := !os.IsNotExist(statErr) if err = os.MkdirAll(dir, mode); err != nil { return err } - if err = os.Chmod(dir, mode); err != nil { - return err + // Only chmod if directory was newly created (fixes #4622) + if !dirExists { + if err = os.Chmod(dir, mode); err != nil { + return err + } } out, err := os.Create(dst)