rpc: fix ExpandHome restrictions bypass (#667)

`go-home` seems to be using environment variables first
This commit is contained in:
Tomas Tauber
2021-10-13 10:52:05 +00:00
committed by GitHub
parent 4ead9814cc
commit 8e12d94359
5 changed files with 31 additions and 14 deletions
+6 -1
View File
@@ -292,7 +292,12 @@ func (a *API) StartCPUProfile(file string) error {
a.logger.Debug("CPU profiling already in progress")
return errors.New("CPU profiling already in progress")
default:
f, err := os.Create(ExpandHome(file))
fp, err := ExpandHome(file)
if err != nil {
a.logger.Debug("failed to get filepath for the CPU profile file", "error", err.Error())
return err
}
f, err := os.Create(fp)
if err != nil {
a.logger.Debug("failed to create CPU profile file", "error", err.Error())
return err
+6 -1
View File
@@ -35,7 +35,12 @@ func (a *API) StartGoTrace(file string) error {
a.logger.Debug("trace already in progress")
return errors.New("trace already in progress")
}
f, err := os.Create(ExpandHome(file))
fp, err := ExpandHome(file)
if err != nil {
a.logger.Debug("failed to get filepath for the CPU profile file", "error", err.Error())
return err
}
f, err := os.Create(fp)
if err != nil {
a.logger.Debug("failed to create go trace file", "error", err.Error())
return err
+12 -11
View File
@@ -24,26 +24,27 @@ func isCPUProfileConfigurationActivated(ctx *server.Context) bool {
// ExpandHome expands home directory in file paths.
// ~someuser/tmp will not be expanded.
func ExpandHome(p string) string {
func ExpandHome(p string) (string, error) {
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
home := os.Getenv("HOME")
if home == "" {
if usr, err := user.Current(); err == nil {
home = usr.HomeDir
}
}
if home != "" {
p = home + p[1:]
usr, err := user.Current()
if err != nil {
return p, err
}
home := usr.HomeDir
p = home + p[1:]
}
return filepath.Clean(p)
return filepath.Clean(p), nil
}
// writeProfile writes the data to a file
func writeProfile(name, file string, log log.Logger) error {
p := pprof.Lookup(name)
log.Info("Writing profile records", "count", p.Count(), "type", name, "dump", file)
f, err := os.Create(ExpandHome(file))
fp, err := ExpandHome(file)
if err != nil {
return err
}
f, err := os.Create(fp)
if err != nil {
return err
}