Skip to content

Commit 7ba6fea

Browse files
authored
Use shellquote to unpack arguments to gitea serv (#12624)
Fix #12471 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 274f923 commit 7ba6fea

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

cmd/serv.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"code.gitea.io/gitea/modules/setting"
2626

2727
"github.com/dgrijalva/jwt-go"
28+
"github.com/kballard/go-shellquote"
2829
"github.com/unknwon/com"
2930
"github.com/urfave/cli"
3031
)
@@ -59,14 +60,6 @@ func setup(logPath string, debug bool) {
5960
}
6061
}
6162

62-
func parseCmd(cmd string) (string, string) {
63-
ss := strings.SplitN(cmd, " ", 2)
64-
if len(ss) != 2 {
65-
return "", ""
66-
}
67-
return ss[0], strings.Replace(ss[1], "'/", "'", 1)
68-
}
69-
7063
var (
7164
allowedCommands = map[string]models.AccessMode{
7265
"git-upload-pack": models.AccessModeRead,
@@ -126,25 +119,35 @@ func runServ(c *cli.Context) error {
126119
return nil
127120
}
128121

129-
verb, args := parseCmd(cmd)
122+
words, err := shellquote.Split(cmd)
123+
if err != nil {
124+
fail("Error parsing arguments", "Failed to parse arguments: %v", err)
125+
}
126+
127+
if len(words) < 2 {
128+
fail("Too few arguments", "Too few arguments in cmd: %s", cmd)
129+
}
130+
131+
verb := words[0]
132+
repoPath := words[1]
133+
if repoPath[0] == '/' {
134+
repoPath = repoPath[1:]
135+
}
130136

131137
var lfsVerb string
132138
if verb == lfsAuthenticateVerb {
133139
if !setting.LFS.StartServer {
134140
fail("Unknown git command", "LFS authentication request over SSH denied, LFS support is disabled")
135141
}
136142

137-
argsSplit := strings.Split(args, " ")
138-
if len(argsSplit) >= 2 {
139-
args = strings.TrimSpace(argsSplit[0])
140-
lfsVerb = strings.TrimSpace(argsSplit[1])
143+
if len(words) > 2 {
144+
lfsVerb = words[2]
141145
}
142146
}
143147

144-
repoPath := strings.ToLower(strings.Trim(args, "'"))
145148
rr := strings.SplitN(repoPath, "/", 2)
146149
if len(rr) != 2 {
147-
fail("Invalid repository path", "Invalid repository path: %v", args)
150+
fail("Invalid repository path", "Invalid repository path: %v", repoPath)
148151
}
149152

150153
username := strings.ToLower(rr[0])

0 commit comments

Comments
 (0)