-
Notifications
You must be signed in to change notification settings - Fork 351
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
Problem: medium shiftleft scan findings (fix #127) #199
Conversation
Codecov Report
@@ Coverage Diff @@
## master #199 +/- ##
==========================================
+ Coverage 26.96% 29.40% +2.43%
==========================================
Files 32 32
Lines 5762 5768 +6
==========================================
+ Hits 1554 1696 +142
+ Misses 4031 3870 -161
- Partials 177 202 +25
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
0032bbc
to
51de149
Compare
cmd/chain-maind/app/app.go
Outdated
return json.NewEncoder(file).Encode(&genesis) | ||
|
||
ret := json.NewEncoder(file).Encode(&genesis) | ||
file.Close() |
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.
Can we remove the earlier file.Close()?
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.
previously, it was defer flie.Close
, so closed after function returns.
now it explicitly close file
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.
defer can close the file in case of panic.
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.
in golang, there is no exception,
so this is the same with previous version
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.
in golang, there is no exception,
so this is the same with previous version
golang has panic, and defer can run when that happens.
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.
ok, i'll check
i'll add unit test for the coverage |
if err != nil { | ||
return err | ||
} | ||
defer outputFile.Close() |
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.
why remove this defer line, isn't defer is better than close manually?
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.
i think so, too.
but code analysis tool recommends not use defer to file.close
in issue #127
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.
https://www.joeshaw.org/dont-defer-close-on-writable-files/
I guess the objection to defer close method is it don't check and properly handle the return value of it, so if we don't handle the error of close method in new style, we don't solve the real issue. and we lost the handling of panic.
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.
ok, i'll modify based upon the link
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.
one other option: securego/gosec#512 (comment)
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.
we can make a extension method file.CloseOrWarn()
? ;D
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.
-
defer for closing files is still OK -- it's a common pattern -- the problematic part is missing the error code -- the easiest way may be just to defer a function that will close the file, but also log any errors (if they happen)
-
for the path from env, keeping it simple with filepath.Clean
cmd/chain-maind/app/app.go
Outdated
file, err := os.OpenFile(path, os.O_RDWR, 0600) | ||
if !chaingenutilcli.IsValidPath(path) { | ||
return fmt.Errorf("insecure filepath %s", 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.
For this file path finding, two things:
- it's likely a false positive, as the scope of this is very limited -- at least in the case of genutil
- this "fix" is a bit problematic:
- it's done after opening the file
- it does some custom input validation with a deny list
So I suggest keeping this simple and either:
- suppress this warning (
// nolint: gosec
) - or just do a simple path cleanup / canonical path --
filepath.Clean(path)
5c612d5
to
02e11f6
Compare
cmd/chain-maind/app/app.go
Outdated
if err == nil { | ||
err = cerr | ||
} |
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.
and if err is not nil, it'll be lost? I'm not sure of the exact defer semantics, but perhaps logging the error (if cerr is not nil) won't hurt?
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.
I guess assign to err at here don't have any effect.
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.
yes, i changed to display the log
2cd75e8
to
f3e377b
Compare
fix lint issue use closure for file.close fix manual file validity check display error in closing file fix lint issue tidy up
f3e377b
to
31544ac
Compare
…rypto-org-chain#199) Solution: change filemode to 600, cleanup filepath, defer file.close with error logging
Solution: change filemode to 600, check filepath validity, remove
defer
in file.close