Skip to content

fix(context): skip chmod on existing dir in SaveUploadedFile#4629

Open
barry3406 wants to merge 1 commit intogin-gonic:masterfrom
barry3406:fix/saveuploadedfile-chmod-existing-dir
Open

fix(context): skip chmod on existing dir in SaveUploadedFile#4629
barry3406 wants to merge 1 commit intogin-gonic:masterfrom
barry3406:fix/saveuploadedfile-chmod-existing-dir

Conversation

@barry3406
Copy link
Copy Markdown

Pull Request Checklist

Please ensure your pull request meets the following requirements:

  • Open your pull request against the master branch.
  • All tests pass in available continuous integration systems (e.g., GitHub Actions).
  • Tests are added or modified as needed to cover code changes.
  • If the pull request introduces a new feature, the feature is documented in the docs/doc.md.

Description

Since v1.12.0, SaveUploadedFile calls os.Chmod on the destination's parent directory unconditionally after os.MkdirAll, which breaks saving into pre-existing directories the process does not own (for example SaveUploadedFile(file, "/tmp/foo.txt") fails with chmod /tmp: operation not permitted).

The fix stats the directory before MkdirAll and only chmods when the stat returned os.ErrNotExist, so the configurable mode from #4088 still applies to directories SaveUploadedFile actually creates while existing directories are left alone.

Added TestSaveUploadedFilePreservesExistingDirPermissions, which creates a temp dir with mode 0o700, saves into it with a different requested mode, and asserts the dir's mode survives and the file contents are correct. Added TestSaveUploadedFileAppliesModeToCreatedDir as a companion that saves into a nested directory which does not yet exist and asserts the new directory ends up with the requested mode.

Fixes #4622

Since v1.12.0, SaveUploadedFile calls os.Chmod on the destination's
parent directory unconditionally after os.MkdirAll. For pre-existing
directories the caller does not own (e.g. /tmp), that fails with
"operation not permitted" and breaks a very common usage pattern.

Stat the directory before MkdirAll and only chmod when MkdirAll
actually created it, preserving the configurable mode behavior from
gin-gonic#4088 for newly created directories.

Fixes gin-gonic#4622
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 11, 2026

Codecov Report

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

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

johnrald1

This comment was marked as resolved.

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)

2 participants