Skip to content

Commit 6444a7c

Browse files
cmd: refactor runServ()
Coalesce access mode detection into one place. Yes, "upload" really has opposite semantics for git commands vs. git-lfs commands. Wow. This commit makes no functional changes.
1 parent f79825c commit 6444a7c

File tree

1 file changed

+45
-28
lines changed

1 file changed

+45
-28
lines changed

cmd/serv.go

+45-28
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
asymkey_model "code.gitea.io/gitea/models/asymkey"
2121
git_model "code.gitea.io/gitea/models/git"
2222
"code.gitea.io/gitea/models/perm"
23+
"code.gitea.io/gitea/modules/container"
2324
"code.gitea.io/gitea/modules/git"
2425
"code.gitea.io/gitea/modules/json"
2526
"code.gitea.io/gitea/modules/log"
@@ -36,7 +37,10 @@ import (
3637
)
3738

3839
const (
39-
lfsAuthenticateVerb = "git-lfs-authenticate"
40+
verbUploadPack = "git-upload-pack"
41+
verbUploadArchive = "git-upload-archive"
42+
verbReceivePack = "git-receive-pack"
43+
verbLfsAuthenticate = "git-lfs-authenticate"
4044
)
4145

4246
// CmdServ represents the available serv sub-command.
@@ -73,12 +77,16 @@ func setup(ctx context.Context, debug bool) {
7377
}
7478

7579
var (
76-
allowedCommands = map[string]perm.AccessMode{
77-
"git-upload-pack": perm.AccessModeRead,
78-
"git-upload-archive": perm.AccessModeRead,
79-
"git-receive-pack": perm.AccessModeWrite,
80-
lfsAuthenticateVerb: perm.AccessModeNone,
81-
}
80+
// keep getAccessMode() in sync
81+
allowedCommands = container.SetOf(
82+
verbUploadPack,
83+
verbUploadArchive,
84+
verbReceivePack,
85+
verbLfsAuthenticate,
86+
)
87+
allowedCommandsLfs = container.SetOf(
88+
verbLfsAuthenticate,
89+
)
8290
alphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`)
8391
)
8492

@@ -124,6 +132,24 @@ func handleCliResponseExtra(extra private.ResponseExtra) error {
124132
return nil
125133
}
126134

135+
func getAccessMode(verb, lfsVerb string) perm.AccessMode {
136+
switch verb {
137+
case verbUploadPack, verbUploadArchive:
138+
return perm.AccessModeRead
139+
case verbReceivePack:
140+
return perm.AccessModeWrite
141+
case verbLfsAuthenticate:
142+
switch lfsVerb {
143+
case "upload":
144+
return perm.AccessModeWrite
145+
case "download":
146+
return perm.AccessModeRead
147+
}
148+
}
149+
// should be unreachable
150+
return perm.AccessModeNone
151+
}
152+
127153
func getLFSAuthToken(ctx context.Context, lfsVerb string, results *private.ServCommandResults) (string, error) {
128154
now := time.Now()
129155
claims := lfs.Claims{
@@ -216,15 +242,6 @@ func runServ(c *cli.Context) error {
216242
}
217243

218244
var lfsVerb string
219-
if verb == lfsAuthenticateVerb {
220-
if !setting.LFS.StartServer {
221-
return fail(ctx, "Unknown git command", "LFS authentication request over SSH denied, LFS support is disabled")
222-
}
223-
224-
if len(words) > 2 {
225-
lfsVerb = words[2]
226-
}
227-
}
228245

229246
rr := strings.SplitN(repoPath, "/", 2)
230247
if len(rr) != 2 {
@@ -261,28 +278,28 @@ func runServ(c *cli.Context) error {
261278
}()
262279
}
263280

264-
requestedMode, has := allowedCommands[verb]
265-
if !has {
281+
if allowedCommands.Contains(verb) {
282+
if allowedCommandsLfs.Contains(verb) {
283+
if !setting.LFS.StartServer {
284+
return fail(ctx, "Unknown git command", "LFS authentication request over SSH denied, LFS support is disabled")
285+
}
286+
if len(words) > 2 {
287+
lfsVerb = words[2]
288+
}
289+
}
290+
} else {
266291
return fail(ctx, "Unknown git command", "Unknown git command %s", verb)
267292
}
268293

269-
if verb == lfsAuthenticateVerb {
270-
if lfsVerb == "upload" {
271-
requestedMode = perm.AccessModeWrite
272-
} else if lfsVerb == "download" {
273-
requestedMode = perm.AccessModeRead
274-
} else {
275-
return fail(ctx, "Unknown LFS verb", "Unknown lfs verb %s", lfsVerb)
276-
}
277-
}
294+
requestedMode := getAccessMode(verb, lfsVerb)
278295

279296
results, extra := private.ServCommand(ctx, keyID, username, reponame, requestedMode, verb, lfsVerb)
280297
if extra.HasError() {
281298
return fail(ctx, extra.UserMsg, "ServCommand failed: %s", extra.Error)
282299
}
283300

284301
// LFS token authentication
285-
if verb == lfsAuthenticateVerb {
302+
if verb == verbLfsAuthenticate {
286303
url := fmt.Sprintf("%s%s/%s.git/info/lfs", setting.AppURL, url.PathEscape(results.OwnerName), url.PathEscape(results.RepoName))
287304

288305
token, err := getLFSAuthToken(ctx, lfsVerb, results)

0 commit comments

Comments
 (0)