-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
hook.go
91 lines (71 loc) · 2.01 KB
/
hook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package hook
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
"time"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/store/leaf"
"github.com/gopasspw/gopass/pkg/appdir"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/kballard/go-shellquote"
)
// Stderr is exported for tests.
var Stderr io.Writer = os.Stderr
type subStoreGetter interface {
GetSubStore(string) (*leaf.Store, error)
MountPoint(string) string
}
func InvokeRoot(ctx context.Context, hookName, secName string, s subStoreGetter, hookArgs ...string) error {
sub, err := s.GetSubStore(s.MountPoint(secName))
if err != nil {
return err
}
return Invoke(ctx, hookName, sub.Storage().Path(), hookArgs...)
}
func Invoke(ctx context.Context, hook, dir string, hookArgs ...string) error {
if true {
// TODO(GH-2546) disabled until further discussion, cf. https://www.cvedetails.com/cve/CVE-2023-24055/
return nil
}
hCmd := strings.TrimSpace(config.String(ctx, hook))
if hCmd == "" {
return nil
}
if sv := os.Getenv("GOPASS_HOOK"); sv == "1" {
debug.Log("GOPASS_HOOK=1, skipping reentrant hook execution")
return nil
}
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
args := make([]string, 0, 4)
if runtime.GOOS != "windows" {
cmdArgs, err := shellquote.Split(hCmd)
if err != nil {
return fmt.Errorf("failed to parse hook command `%s`", hCmd)
}
hook = cmdArgs[0]
args = append(args, cmdArgs[1:]...)
}
if len(hook) > 2 && hook[:2] == "~/" {
hook = appdir.UserHome() + hook[1:]
}
args = append(args, hookArgs...)
cmd := exec.CommandContext(ctx, hook, args...)
cmd.Stdin = nil
cmd.Stdout = nil
cmd.Stderr = Stderr
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "GOPASS_HOOK=1")
cmd.Dir = dir
debug.Log("running hook %s with: %s %+v", hook, cmd.Path, cmd.Args)
if err := cmd.Run(); err != nil {
debug.Log("cmd: %s %+v - error: %+v", cmd.Path, cmd.Args, err)
return fmt.Errorf("failed to run %s %v: %w", hook, args, err)
}
return nil
}