fix(context): skip chmod on existing dir in SaveUploadedFile#4629
Open
barry3406 wants to merge 1 commit intogin-gonic:masterfrom
Open
fix(context): skip chmod on existing dir in SaveUploadedFile#4629barry3406 wants to merge 1 commit intogin-gonic:masterfrom
barry3406 wants to merge 1 commit intogin-gonic:masterfrom
Conversation
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 Report❌ Patch coverage is
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
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:
|
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.
Pull Request Checklist
Please ensure your pull request meets the following requirements:
masterbranch.docs/doc.md.Description
Since v1.12.0,
SaveUploadedFilecallsos.Chmodon the destination's parent directory unconditionally afteros.MkdirAll, which breaks saving into pre-existing directories the process does not own (for exampleSaveUploadedFile(file, "/tmp/foo.txt")fails withchmod /tmp: operation not permitted).The fix stats the directory before
MkdirAlland only chmods when the stat returnedos.ErrNotExist, so the configurable mode from #4088 still applies to directoriesSaveUploadedFileactually creates while existing directories are left alone.Added
TestSaveUploadedFilePreservesExistingDirPermissions, which creates a temp dir with mode0o700, saves into it with a different requested mode, and asserts the dir's mode survives and the file contents are correct. AddedTestSaveUploadedFileAppliesModeToCreatedDiras 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