-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent overwriting irregular files (cp, save, export commands) #1515
Prevent overwriting irregular files (cp, save, export commands) #1515
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1515 +/- ##
==========================================
- Coverage 56.13% 55.08% -1.05%
==========================================
Files 306 289 -17
Lines 20931 19389 -1542
==========================================
- Hits 11749 10681 -1068
+ Misses 8334 8015 -319
+ Partials 848 693 -155 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I only had a quick glance over this; left some comments/suggestions
cli/command/utils.go
Outdated
dir := filepath.Dir(path) | ||
if dir != "" && dir != "." { | ||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
return errors.Errorf("unable to validate output path: directory %q does not exist", dir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps this should be "invalid output path" instead of "unable to validate output path" (because we were able to validate it, but it turned out to be an invalid path)
cli/command/utils.go
Outdated
// (if the path exists and doesn't point to a directory) | ||
if fileInfo, err := os.Stat(path); !os.IsNotExist(err) { | ||
if !fileInfo.Mode().IsDir() && !fileInfo.Mode().IsRegular() { | ||
return errors.Errorf("unable to validate output path: %q must be a directory or a regular file", path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here for the "unable to validate.."
Also thinking if we should mention that the target is a "special" file (not sure)
Hi and thanks for your feedback! Applying changes shortly. |
ee5b322
to
416087a
Compare
I've pushed the changes, made some additional refactoring and updated my original post. The major changes to the post are:
|
ping @thaJeztah - is there else to change? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay; I've been a bit busy 😅
Left some comments inline, but feedback welcome, as usual 👍 Thanks!
cli/command/utils.go
Outdated
// helper to `ValidateOutputPath` | ||
func ValidateOutputPathFileMode(fileMode os.FileMode) error { | ||
if fileMode&os.ModeDevice != 0 { | ||
return errors.New("Got a device") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably be lowercase for an error;
return errors.New("Got a device") | |
return errors.New("got a device") |
416087a
to
7f8e0d1
Compare
Hi @thaJeztah, I've just pushed my changes. Thanks for the feedback! |
ping @thaJeztah :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Sorry for the delay 😅
ping @vdemeester @silvin-lubecki PTAL
cli/command/utils.go
Outdated
func ValidateOutputPathFileMode(fileMode os.FileMode) error { | ||
if fileMode&os.ModeDevice != 0 { | ||
return errors.New("got a device") | ||
} else if fileMode&os.ModeIrregular != 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very small nit: I think the else
can be removed. The 2 if can even be transformed to a switch:
switch{
case fileMode&os.ModeDevice != 0:
return errors.New("got a device")
case fileMode&os.ModeIrregular != 0:
return errors.New("got an irregular file")
}
return nil
cli/command/utils.go
Outdated
} | ||
|
||
if err := ValidateOutputPathFileMode(fileInfo.Mode()); err != nil { | ||
return errors.Wrap(err, fmt.Sprintf("invalid output path: %q must be a directory or a regular file", path)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small nit: use errors.Wrapf instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 small nits but otherwise LGTM, thank you @sw-pschmied !
Signed-off-by: Philipp Schmied <pschmied@schutzwerk.com>
7f8e0d1
to
7632776
Compare
Thanks, I just pushed my changes @silvin-lubecki |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🐯
Thanks a lot @sw-pschmied for detecting this issue AND fixing it ! 👍👍 |
Signed-off-by: Philipp Schmied pschmied@schutzwerk.com
- What I did
This fixes #1514.
- How I did it
The function
command.ValidateOutputPath
was already present incli/command/image/save.go
. I've moved this tocli/command/utils.go
in order to use this function to fix the issue for all three commands.After that, I've added code to this existing function to use a new helper method called
ValidateOutputPathFileMode
which also resides incli/command/utils.go
. This is being used to check for irregular files/devices and returns an error in case an invalid target is being passed. This is now being called at the start of the functions implementing thecp
,export
andsave
commands. Forcp
operations to a container, the fix only uses the helper method mentioned above, so it's possible to unify this check for all three operations. The helper method also determines whether the target path points to a device or any other invalid/irregular file to display an error message accordingly.Also, I've added test cases for all commands.
- How to verify it
cli
master branch and compile a staticdocker
binarydocker pull ubuntu:latest
docker save ubuntu:latest -o /dev/random
stat /dev/random
indicating/dev/random
is now a regular filefailed to save image: invalid output path: "/dev/random" must be a directory or a regular file: Got a device
.- Description for the changelog
cp, save, export: Prevent overwriting irregular files
- A picture of a cute animal (not mandatory but encouraged)