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

[ioctl] fix potential file inclusion via variable #3549

Merged
merged 2 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion e2etest/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/hex"
"math/big"
"os"
"path/filepath"

"github.com/iotexproject/go-pkgs/hash"
"github.com/pkg/errors"
Expand Down Expand Up @@ -194,7 +195,7 @@ func addTestingTsfBlocks(bc blockchain.Blockchain, ap actpool.ActPool) error {
}

func copyDB(srcDB, dstDB string) error {
input, err := os.ReadFile(srcDB)
input, err := os.ReadFile(filepath.Clean(srcDB))
if err != nil {
return errors.Wrap(err, "failed to read source db file")
}
Expand Down
3 changes: 2 additions & 1 deletion ioctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/ethereum/go-ethereum/accounts/keystore"
Expand Down Expand Up @@ -257,7 +258,7 @@ func (c *client) NewKeyStore() *keystore.KeyStore {
}

func (c *client) DecryptPrivateKey(passwordOfKeyStore, keyStorePath string) (*ecdsa.PrivateKey, error) {
keyJSON, err := os.ReadFile(keyStorePath)
keyJSON, err := os.ReadFile(filepath.Clean(keyStorePath))
if err != nil {
return nil, fmt.Errorf("keystore file \"%s\" read error", keyStorePath)
}
Expand Down
2 changes: 1 addition & 1 deletion ioctl/cmd/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func newAccountByKey(alias string, privateKey string, walletDir string) (string,
}

func newAccountByKeyStore(alias, passwordOfKeyStore, keyStorePath string, walletDir string) (string, error) {
keyJSON, err := os.ReadFile(keyStorePath)
keyJSON, err := os.ReadFile(filepath.Clean(keyStorePath))
if err != nil {
return "", output.NewError(output.ReadFileError,
fmt.Sprintf("keystore file \"%s\" read error", keyStorePath), nil)
Expand Down
3 changes: 2 additions & 1 deletion ioctl/cmd/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/hex"
"fmt"
"os"
"path/filepath"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common/compiler"
Expand Down Expand Up @@ -101,7 +102,7 @@ func checkCompilerVersion(solc *compiler.Solidity) bool {
}

func readAbiFile(abiFile string) (*abi.ABI, error) {
abiBytes, err := os.ReadFile(abiFile)
abiBytes, err := os.ReadFile(filepath.Clean(abiFile))
if err != nil {
return nil, output.NewError(output.ReadFileError, "failed to read abi file", err)
}
Expand Down
4 changes: 2 additions & 2 deletions ioctl/cmd/contract/contractshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func isDir(path string) bool {

func isReadOnly(path string) bool {
var readOnly = false
file, err := os.OpenFile(path, os.O_WRONLY, 0666)
file, err := os.OpenFile(filepath.Clean(path), os.O_WRONLY, 0666)
if err != nil {
if os.IsPermission(err) {
log.Println("Error: Write permission denied.")
Expand Down Expand Up @@ -198,7 +198,7 @@ func share(args []string) error {
t := request.Payload
getPayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{})
getPayloadPath := getPayload["path"].(string)
upload, err := os.ReadFile(_givenPath + "/" + getPayloadPath)
upload, err := os.ReadFile(filepath.Clean(_givenPath + "/" + getPayloadPath))
if err != nil {
log.Println("read file failed: ", err)
}
Expand Down
3 changes: 2 additions & 1 deletion ioctl/cmd/hdwallet/hdwalletderive.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"bytes"
"fmt"
"os"
"path/filepath"

ecrypt "github.com/ethereum/go-ethereum/crypto"
hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
Expand Down Expand Up @@ -71,7 +72,7 @@ func DeriveKey(account, change, index uint32, password string) (string, crypto.P
return "", nil, output.NewError(output.InputError, "Run 'ioctl hdwallet create' to create your HDWallet first.", nil)
}

enctxt, err := os.ReadFile(hdWalletConfigFile)
enctxt, err := os.ReadFile(filepath.Clean(hdWalletConfigFile))
if err != nil {
return "", nil, output.NewError(output.InputError, "failed to read config", err)
}
Expand Down
3 changes: 2 additions & 1 deletion ioctl/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package config
import (
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -137,7 +138,7 @@ func LoadConfig() (Config, error) {
ReadConfig := Config{
Aliases: make(map[string]string),
}
in, err := os.ReadFile(DefaultConfigFile)
in, err := os.ReadFile(filepath.Clean(DefaultConfigFile))
if err == nil {
if err := yaml.Unmarshal(in, &ReadConfig); err != nil {
return ReadConfig, err
Expand Down
2 changes: 1 addition & 1 deletion ioctl/doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func GenMarkdownTreeCustom(c *cobra.Command, dir string, name string, path strin
filename = filepath.Join(path, "README.md")
}

f, err := os.Create(filename)
f, err := os.Create(filepath.Clean(filename))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion ioctl/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"math/big"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -169,7 +170,7 @@ func Address(in string) (string, error) {
// JwtAuth used for ioctl set auth and send for every grpc request
func JwtAuth() (jwt metadata.MD, err error) {
jwtFile := os.Getenv("HOME") + "/.config/ioctl/default/auth.jwt"
jwtString, err := os.ReadFile(jwtFile)
jwtString, err := os.ReadFile(filepath.Clean(jwtFile))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/recovery/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func LogCrash(r interface{}) {
}

func writeHeapProfile(path string) {
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
f, err := os.OpenFile(filepath.Clean(path), os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
log.S().Errorf("crashlog: open heap profile error: %v", err)
return
Expand Down
9 changes: 5 additions & 4 deletions state/factory/patchstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/hex"
"io"
"os"
"path/filepath"
"strconv"

"github.com/pkg/errors"
Expand Down Expand Up @@ -43,16 +44,16 @@ type (
* key: hex string
* value: hex string
*/
func newPatchStore(filepath string) (*patchStore, error) {
func newPatchStore(fpath string) (*patchStore, error) {
store := &patchStore{
patchs: map[uint64][]*patch{},
}
if filepath == "" {
if fpath == "" {
return store, nil
}
file, err := os.Open(filepath)
file, err := os.Open(filepath.Clean(fpath))
if err != nil {
return nil, errors.Wrapf(err, "failed to open kvstore patch, %s", filepath)
return nil, errors.Wrapf(err, "failed to open kvstore patch, %s", fpath)
}
reader := csv.NewReader(file)
reader.FieldsPerRecord = -1
Expand Down
3 changes: 2 additions & 1 deletion tools/actioninjector.v2/internal/cmd/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"math/big"
"math/rand"
"os"
"path/filepath"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -112,7 +113,7 @@ func (p *injectProcessor) randAccounts(num int) error {
}

func (p *injectProcessor) loadAccounts(keypairsPath string) error {
keyPairBytes, err := os.ReadFile(keypairsPath)
keyPairBytes, err := os.ReadFile(filepath.Clean(keypairsPath))
if err != nil {
return errors.Wrap(err, "failed to read key pairs file")
}
Expand Down
3 changes: 2 additions & 1 deletion tools/util/injectorutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"math/big"
"math/rand"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -81,7 +82,7 @@ func GetTotalTsfFailed() uint64 {
// LoadAddresses loads key pairs from key pair path and construct addresses
func LoadAddresses(keypairsPath string, chainID uint32) ([]*AddressKey, error) {
// Load Senders' public/private key pairs
keyPairBytes, err := os.ReadFile(keypairsPath)
keyPairBytes, err := os.ReadFile(filepath.Clean(keypairsPath))
if err != nil {
return nil, errors.Wrap(err, "failed to read key pairs file")
}
Expand Down