all repos

mugit @ aaf0e5f

🐮 git server that your cow will love

mugit/internal/ssh/ssh.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
ssh: refactor the disallowd command handling, 1 month ago
1
package ssh
2
3
import (
4
	"context"
5
	"errors"
6
	"fmt"
7
	"io"
8
	"strings"
9
10
	"olexsmir.xyz/mugit/internal/config"
11
	"olexsmir.xyz/mugit/internal/git"
12
13
	gossh "golang.org/x/crypto/ssh"
14
)
15
16
type Shell struct {
17
	cfg *config.Config
18
19
	keys []gossh.PublicKey
20
}
21
22
func NewShell(cfg *config.Config) (*Shell, error) {
23
	parsedKeys := make([]gossh.PublicKey, len(cfg.SSH.Keys))
24
	for i, key := range cfg.SSH.Keys {
25
		pkey, _, _, _, err := gossh.ParseAuthorizedKey([]byte(key))
26
		if err != nil {
27
			return nil, err
28
		}
29
		parsedKeys[i] = pkey
30
	}
31
32
	return &Shell{
33
		cfg:  cfg,
34
		keys: parsedKeys,
35
	}, nil
36
}
37
38
func (s *Shell) HandleCommand(ctx context.Context, cmd string, stdin io.Reader, stdout, stderr io.Writer) error {
39
	gitCmd, repoName, err := s.parseCommand(cmd)
40
	if err != nil {
41
		return s.replyWithGitError(stderr, "access denied: invalid command", err)
42
	}
43
44
	repoPath, err := git.ResolvePath(s.cfg.Repo.Dir, git.ResolveName(repoName))
45
	if err != nil {
46
		return s.replyWithGitError(stderr, "access denied", err)
47
	}
48
49
	repo, err := git.Open(repoPath, "")
50
	if err != nil {
51
		return s.replyWithGitError(stderr, "repository not found", err)
52
	}
53
54
	switch gitCmd {
55
	case "git-upload-pack":
56
		err = repo.UploadPack(ctx, false, "", stdin, stdout)
57
	case "git-upload-archive":
58
		err = repo.UploadArchive(ctx, stdin, stdout)
59
	case "git-receive-pack":
60
		err = repo.ReceivePack(ctx, stdin, stdout, stderr)
61
	default:
62
		msg := "access denied: invalid git command"
63
		return s.replyWithGitError(stderr, msg, errors.New(msg))
64
	}
65
66
	if err != nil {
67
		return err
68
	}
69
70
	return nil
71
}
72
73
func (s *Shell) AuthorizedKeys(executablePath string) string {
74
	var out strings.Builder
75
	for _, key := range s.cfg.SSH.Keys {
76
		fmt.Fprintf(&out, `command="%s shell",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s`+"\n",
77
			executablePath, key)
78
	}
79
	return out.String()
80
}
81
82
var validCommands = map[string]bool{
83
	"git-upload-pack":    true,
84
	"git-upload-archive": true,
85
	"git-receive-pack":   true,
86
}
87
88
func (s *Shell) parseCommand(cmd string) (gitCmd, repoName string, err error) {
89
	cmdParts := strings.Fields(cmd)
90
	if len(cmdParts) < 2 {
91
		return "", "", fmt.Errorf("invalid command: expected 'git-cmd repo', got %q", cmd)
92
	}
93
94
	gitCmd = cmdParts[0]
95
	if !validCommands[gitCmd] {
96
		return "", "", fmt.Errorf("invalid command: disallowd command")
97
	}
98
99
	repoName = strings.Trim(cmdParts[1], "'\"")
100
	if repoName == "" {
101
		return "", "", fmt.Errorf("invalid command: empty repository name")
102
	}
103
104
	return gitCmd, repoName, nil
105
}
106
107
func (s *Shell) replyWithGitError(stderr io.Writer, msg string, cause error) error {
108
	if _, err := fmt.Fprintf(stderr, "fatal: %s\n", msg); err != nil {
109
		return err
110
	}
111
112
	return cause
113
}