v2.30.0-pre3 63e7f3a

pass.go RawHistoryFolder

1// Package secrets implements secret provider adapters.
2package secrets
3
4import (
5	"context"
6	"fmt"
7	"os/exec"
8	"regexp"
9	"strings"
10	"time"
11
12	"github.com/bnema/zerowrap"
13
14	"github.com/bnema/gordon/internal/domain"
15)
16
17// passPathRegex validates pass paths.
18// Allows alphanumeric characters, forward slashes, underscores, dots, and hyphens.
19// This prevents command injection via shell metacharacters.
20var passPathRegex = regexp.MustCompile(`^[a-zA-Z0-9/_.-]+$`)
21
22// PassProvider implements the SecretProvider interface using the pass password manager.
23type PassProvider struct {
24	timeout time.Duration
25	log     zerowrap.Logger
26}
27
28// NewPassProvider creates a new pass provider.
29func NewPassProvider(log zerowrap.Logger) *PassProvider {
30	return &PassProvider{
31		timeout: 10 * time.Second,
32		log:     log,
33	}
34}
35
36// Name returns the provider name.
37func (p *PassProvider) Name() string {
38	return "pass"
39}
40
41// ValidatePath validates a pass path to prevent command injection and path traversal.
42// Returns an error if the path is invalid.
43func ValidatePath(path string) error {
44	if path == "" {
45		return fmt.Errorf("path cannot be empty")
46	}
47
48	// SECURITY: Validate path format to prevent command injection
49	if !passPathRegex.MatchString(path) {
50		return fmt.Errorf("invalid path: must contain only alphanumeric characters, /, _, ., -")
51	}
52
53	// SECURITY: Prevent path traversal
54	if strings.Contains(path, "..") {
55		return domain.ErrPathTraversal
56	}
57
58	// SECURITY: Reject absolute paths
59	if strings.HasPrefix(path, "/") {
60		return fmt.Errorf("invalid path: absolute paths not allowed")
61	}
62
63	return nil
64}
65
66// GetSecret retrieves a secret from pass by path.
67func (p *PassProvider) GetSecret(ctx context.Context, path string) (string, error) {
68	// SECURITY: Validate path before executing command
69	if err := ValidatePath(path); err != nil {
70		p.log.Warn().
71			Str(zerowrap.FieldLayer, "adapter").
72			Str(zerowrap.FieldAdapter, "secrets").
73			Str("provider", "pass").
74			Str("path", path).
75			Err(err).
76			Msg("rejected invalid pass path")
77		return "", err
78	}
79
80	ctx, cancel := context.WithTimeout(ctx, p.timeout)
81	defer cancel()
82
83	cmd := exec.CommandContext(ctx, "pass", "show", path) //nolint:gosec // binary is constant ("pass"); arguments are validated secret paths
84	output, err := cmd.Output()
85	if err != nil {
86		if exitError, ok := err.(*exec.ExitError); ok {
87			return "", fmt.Errorf("pass command failed: %s", string(exitError.Stderr))
88		}
89		return "", fmt.Errorf("failed to execute pass command: %w", err)
90	}
91
92	secret := strings.TrimSpace(string(output))
93	if secret == "" {
94		return "", fmt.Errorf("empty secret returned from pass for path: %s", path)
95	}
96
97	p.log.Debug().
98		Str(zerowrap.FieldLayer, "adapter").
99		Str(zerowrap.FieldAdapter, "secrets").
100		Str("provider", "pass").
101		Str("path", path).
102		Msg("successfully retrieved secret from pass")
103
104	return secret, nil
105}
106
107// IsAvailable checks if pass is available in the system.
108// Uses a short timeout so a stalled GPG agent does not hang the caller.
109func (p *PassProvider) IsAvailable() bool {
110	ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
111	defer cancel()
112	cmd := exec.CommandContext(ctx, "pass", "version") //nolint:gosec // binary is a constant ("pass"), no user input
113	return cmd.Run() == nil
114}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close