mirror of
https://github.com/gopasspw/gopass.git
synced 2025-12-08 19:24:54 +00:00
* [chore] Migrate to golangci-lint v2 Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * [chore] Fix more lint issues Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * [chore] Fix more lint issue Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * [chore] Fix more lint issues Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * [chore] Add more package comments. Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * [chore] Fix golangci-lint config and the remaining checks Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * [fix] Use Go 1.24 Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * [fix] Fix container builds Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * Fix more failing tests Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * Fix test failure Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * Fix another len assertion Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * Move location tests Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * [fix] Fix most remaining lint issues Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * [fix] Only run XDG specific tests on linux Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> * [fix] Attempt to address on source of flaky failures Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org> --------- Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
93 lines
2.1 KiB
Go
93 lines
2.1 KiB
Go
// Package hook provides a flexible hook system for gopass.
|
|
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
|
|
}
|