8979f6b7d78af74ac3cc664f25aed3f5d10d7e41

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

fix(git): disable pushing to subdirectories

Related: https://github.com/charmbracelet/soft-serve/issues/120
Related: https://github.com/charmbracelet/wish/commit/8808de520d3ea21931f13113c6b0b6d0141272d4

Diff

 1diff --git a/server/server.go b/server/server.go
 2index c1e9d8c6081ac3647e0d77e6afe0a1708c8eed57..c651776f11bc0c50119437eead438c1cb339ef96 100644
 3--- a/server/server.go
 4+++ b/server/server.go
 5@@ -5,6 +5,8 @@ import (
 6 	"fmt"
 7 	"log"
 8 	"net"
 9+	"path/filepath"
10+	"strings"
11 
12 	appCfg "github.com/charmbracelet/soft-serve/config"
13 	"github.com/charmbracelet/soft-serve/server/config"
14@@ -40,6 +42,27 @@ func NewServer(cfg *config.Config) *Server {
15 			softMiddleware(ac),
16 			bm.MiddlewareWithProgramHandler(SessionHandler(ac), termenv.ANSI256),
17 			gm.Middleware(cfg.RepoPath, ac),
18+			// Note: disable pushing to subdirectories as it can create
19+			// conflicts with existing repos. This only affects the git
20+			// middleware.
21+			//
22+			// This is related to
23+			// https://github.com/charmbracelet/soft-serve/issues/120
24+			// https://github.com/charmbracelet/wish/commit/8808de520d3ea21931f13113c6b0b6d0141272d4
25+			func(sh ssh.Handler) ssh.Handler {
26+				return func(s ssh.Session) {
27+					cmds := s.Command()
28+					if len(cmds) == 2 && strings.HasPrefix(cmds[0], "git") {
29+						repo := strings.TrimSuffix(strings.TrimPrefix(cmds[1], "/"), "/")
30+						repo = filepath.Clean(repo)
31+						if n := strings.Count(repo, "/"); n != 0 {
32+							wish.Fatalln(s, fmt.Errorf("invalid repo path: subdirectories not allowed"))
33+							return
34+						}
35+					}
36+					sh(s)
37+				}
38+			},
39 			lm.Middleware(),
40 		),
41 	}