Skip to content

Commit

Permalink
Problem: medium shiftleft scan findings (fix crypto-org-chain#127)
Browse files Browse the repository at this point in the history
fix lint issue

use closure for file.close

fix manual file validity check
  • Loading branch information
leejw51crypto committed Oct 22, 2020
1 parent 59b2dad commit 02e11f6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
15 changes: 13 additions & 2 deletions cmd/chain-maind/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"io"
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/crypto-com/chain-main/app/params"
Expand Down Expand Up @@ -142,11 +143,20 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
config.SetRoot(clientCtx.HomeDir)
path := config.GenesisFile()

file, err := os.OpenFile(path, os.O_RDWR, 0644)
cleanedPath := filepath.Clean(path)
// nolint: gosec
file, err := os.OpenFile(cleanedPath, os.O_RDWR, 0600)
if err != nil {
return err
}
defer file.Close()

defer func() {
cerr := file.Close()
if err == nil {
err = cerr
}
}()

var genesis map[string]interface{}
if err := json.NewDecoder(file).Decode(&genesis); err != nil {
return err
Expand All @@ -161,6 +171,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
if _, err := file.Seek(0, 0); err != nil {
return err
}

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

Expand Down
2 changes: 1 addition & 1 deletion x/chainmain/client/cli/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func writeFile(name string, dir string, contents []byte) error {
return err
}

err = tmos.WriteFile(file, contents, 0644)
err = tmos.WriteFile(file, contents, 0600)
if err != nil {
return err
}
Expand Down
14 changes: 11 additions & 3 deletions x/genutil/client/cli/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,26 @@ func readUnsignedGenTxFile(clientCtx client.Context, r io.Reader) (sdk.Tx, error
}

func writeSignedGenTx(clientCtx client.Context, outputDocument string, tx sdk.Tx) error {
outputFile, err := os.OpenFile(outputDocument, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
cleanedPath := filepath.Clean(outputDocument)
// nolint: gosec
outputFile, err := os.OpenFile(cleanedPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)

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

defer func() {
cerr := outputFile.Close()
if err == nil {
err = cerr
}
}()

json, err := clientCtx.TxConfig.TxJSONEncoder()(tx)
if err != nil {
return err
}

_, err = fmt.Fprintf(outputFile, "%s\n", json)

return err
}

0 comments on commit 02e11f6

Please sign in to comment.