Skip to content

fix(context): only chmod newly created dirs in SaveUploadedFile (fixes #4622)#4650

Open
nghiack7 wants to merge 2 commits into
gin-gonic:masterfrom
nghiack7:fix/issue-4622-saveuploadedfile-chmod
Open

fix(context): only chmod newly created dirs in SaveUploadedFile (fixes #4622)#4650
nghiack7 wants to merge 2 commits into
gin-gonic:masterfrom
nghiack7:fix/issue-4622-saveuploadedfile-chmod

Conversation

@nghiack7
Copy link
Copy Markdown

@nghiack7 nghiack7 commented May 9, 2026

🐛 Bug Fix

Fixes #4622 - SaveUploadedFile no longer attempts to modify permissions on existing directories.

Problem

After upgrading from v1.10.1 to v1.12.0, SaveUploadedFile started failing when saving files directly into existing system directories like /tmp. The failure happens due to an attempt to call os.Chmod on the target directory, even if it already exists and is not owned by the process.

Solution

Only apply os.Chmod when the directory is newly created:

  1. Check if directory exists before os.MkdirAll
  2. Only call os.Chmod if directory was newly created
  3. Preserves backward compatibility for newly created dirs

Impact

  • ✅ Allows saving files to existing system directories (/tmp, etc.)
  • ✅ Preserves chmod behavior for newly created directories
  • ✅ Restores v1.10.1 behavior (fixes regression)
  • ✅ No breaking changes for existing usage

Testing

Tested manually:

  • Saving to /tmp/test.txt (existing dir) ✅
  • Saving to new dir with custom perms ✅
  • Build passes: go build ./...

Code Changes

// Before: Always chmod (breaks /tmp)
os.MkdirAll(dir, mode)
os.Chmod(dir, mode)

// After: Only chmod if newly created
dirExists := !os.IsNotExist(os.Stat(dir))
os.MkdirAll(dir, mode)
if !dirExists {
    os.Chmod(dir, mode)
}

Co-authored-by: Nghia Nguyen nghiack7@users.noreply.github.com

Fixes gin-gonic#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 <nghiack7@users.noreply.github.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 9, 2026

Codecov Report

❌ Patch coverage is 60.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.37%. Comparing base (3dc1cd6) to head (ec746b4).
⚠️ Report is 276 commits behind head on master.

Files with missing lines Patch % Lines
context.go 60.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4650      +/-   ##
==========================================
- Coverage   99.21%   98.37%   -0.84%     
==========================================
  Files          42       48       +6     
  Lines        3182     3141      -41     
==========================================
- Hits         3157     3090      -67     
- Misses         17       42      +25     
- Partials        8        9       +1     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 98.36% <60.00%> (?)
-tags go_json 98.29% <60.00%> (?)
-tags nomsgpack 98.35% <60.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.25 98.37% <60.00%> (?)
go-1.26 98.37% <60.00%> (?)
macos-latest 98.37% <60.00%> (-0.84%) ⬇️
ubuntu-latest 98.37% <60.00%> (-0.84%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Add a regression test for gin-gonic#4622 that asserts SaveUploadedFile leaves
permissions on a pre-existing target directory untouched.

Make TestSaveUploadedFileWithPermission hermetic by saving into a
not-yet-existing subdirectory under t.TempDir(), so the assertion on
the requested mode is meaningful regardless of the test runner's
working-directory permissions (the previous form relied on the
soon-to-be-removed chmod-on-existing-dir behaviour to pass).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SaveUploadedFile: unexpected chmod on existing directories (breaks /tmp usage)

1 participant