Skip to content
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

Merged
merged 2 commits into from
Oct 22, 2020

Conversation

leejw51crypto
Copy link
Contributor

@leejw51crypto leejw51crypto commented Oct 21, 2020

Solution: change filemode to 600, check filepath validity, remove defer in file.close

@codecov
Copy link

codecov bot commented Oct 21, 2020

Codecov Report

Merging #199 into master will increase coverage by 2.43%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            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     
Flag Coverage Δ
#integration_tests 26.94% <0.00%> (-0.03%) ⬇️
#unit_tests 27.78% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
cmd/chain-maind/app/app.go 56.59% <0.00%> (-0.95%) ⬇️
x/chainmain/client/cli/testnet.go 9.66% <0.00%> (ø)
x/genutil/client/cli/gentx.go 21.27% <0.00%> (-0.47%) ⬇️
x/gov/client/cli/tx.go 47.68% <0.00%> (+0.66%) ⬆️
x/staking/client/cli/tx.go 48.40% <0.00%> (+19.42%) ⬆️
config/config.go 66.66% <0.00%> (+20.83%) ⬆️
x/bank/client/cli/tx.go 75.58% <0.00%> (+32.55%) ⬆️
x/staking/client/cli/utils.go 38.46% <0.00%> (+38.46%) ⬆️
app/coins.go 61.76% <0.00%> (+52.94%) ⬆️
x/gov/client/cli/parse.go 100.00% <0.00%> (+100.00%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 08d59d9...748b134. Read the comment docs.

return json.NewEncoder(file).Encode(&genesis)

ret := json.NewEncoder(file).Encode(&genesis)
file.Close()
Copy link
Collaborator

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()?

Copy link
Contributor Author

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

Copy link
Collaborator

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.

Copy link
Contributor Author

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

Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i'll check

@leejw51crypto
Copy link
Contributor Author

i'll add unit test for the coverage

if err != nil {
return err
}
defer outputFile.Close()
Copy link
Collaborator

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?

Copy link
Contributor Author

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

Copy link
Collaborator

@yihuang yihuang Oct 22, 2020

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.

Copy link
Contributor Author

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

Copy link
Contributor

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)

Copy link
Collaborator

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

Copy link
Contributor

@tomtau tomtau left a 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

Comment on lines 146 to 149
file, err := os.OpenFile(path, os.O_RDWR, 0600)
if !chaingenutilcli.IsValidPath(path) {
return fmt.Errorf("insecure filepath %s", path)
}
Copy link
Contributor

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:

  1. it's likely a false positive, as the scope of this is very limited -- at least in the case of genutil
  2. 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)

Comment on lines 155 to 156
if err == nil {
err = cerr
}
Copy link
Contributor

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?

Copy link
Collaborator

@yihuang yihuang Oct 22, 2020

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.

Copy link
Contributor Author

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

@leejw51crypto leejw51crypto force-pushed the cro-127 branch 2 times, most recently from 2cd75e8 to f3e377b Compare October 22, 2020 03:44
fix lint issue

use closure for file.close

fix manual file validity check

display error in closing file

fix lint issue

tidy up
@tomtau tomtau merged commit 4784eac into crypto-org-chain:master Oct 22, 2020
allthatjazzleo pushed a commit to allthatjazzleo/chain-main that referenced this pull request Oct 22, 2020
…rypto-org-chain#199)

Solution: change filemode to 600, cleanup filepath, defer file.close with error logging
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.

4 participants