fix(context): only chmod newly created dirs in SaveUploadedFile (fixes #4622)#4650
Open
nghiack7 wants to merge 2 commits into
Open
fix(context): only chmod newly created dirs in SaveUploadedFile (fixes #4622)#4650nghiack7 wants to merge 2 commits into
nghiack7 wants to merge 2 commits into
Conversation
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 Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🐛 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,
SaveUploadedFilestarted failing when saving files directly into existing system directories like/tmp. The failure happens due to an attempt to callos.Chmodon the target directory, even if it already exists and is not owned by the process.Solution
Only apply
os.Chmodwhen the directory is newly created:os.MkdirAllos.Chmodif directory was newly createdImpact
/tmp, etc.)Testing
Tested manually:
/tmp/test.txt(existing dir) ✅go build ./...✅Code Changes
Co-authored-by: Nghia Nguyen nghiack7@users.noreply.github.com