mirror of
https://github.com/restic/restic.git
synced 2025-08-23 04:07:25 +00:00
Sanitize environment before starting backend processes (rclone, ssh)
The restic security model includes full trust of the local machine, so this should not fix any actual security problems, but it's better to be safe than sorry. Fixes #2192.
This commit is contained in:
26
internal/backend/foreground.go
Normal file
26
internal/backend/foreground.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// StartForeground runs cmd in the foreground, by temporarily switching to the
|
||||
// new process group created for cmd. The returned function `bg` switches back
|
||||
// to the previous process group.
|
||||
//
|
||||
// The command's environment has all RESTIC_* variables removed.
|
||||
func StartForeground(cmd *exec.Cmd) (bg func() error, err error) {
|
||||
env := os.Environ() // Returns a copy that we can modify.
|
||||
|
||||
cmd.Env = env[:0]
|
||||
for _, kv := range env {
|
||||
if strings.HasPrefix(kv, "RESTIC_") {
|
||||
continue
|
||||
}
|
||||
cmd.Env = append(cmd.Env, kv)
|
||||
}
|
||||
|
||||
return startForeground(cmd)
|
||||
}
|
Reference in New Issue
Block a user