@@ -20,6 +20,7 @@ import (
20
20
asymkey_model "code.gitea.io/gitea/models/asymkey"
21
21
git_model "code.gitea.io/gitea/models/git"
22
22
"code.gitea.io/gitea/models/perm"
23
+ "code.gitea.io/gitea/modules/container"
23
24
"code.gitea.io/gitea/modules/git"
24
25
"code.gitea.io/gitea/modules/json"
25
26
"code.gitea.io/gitea/modules/log"
@@ -36,7 +37,10 @@ import (
36
37
)
37
38
38
39
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"
40
44
)
41
45
42
46
// CmdServ represents the available serv sub-command.
@@ -73,12 +77,16 @@ func setup(ctx context.Context, debug bool) {
73
77
}
74
78
75
79
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
+ )
82
90
alphaDashDotPattern = regexp .MustCompile (`[^\w-\.]` )
83
91
)
84
92
@@ -124,6 +132,24 @@ func handleCliResponseExtra(extra private.ResponseExtra) error {
124
132
return nil
125
133
}
126
134
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
+
127
153
func getLFSAuthToken (ctx context.Context , lfsVerb string , results * private.ServCommandResults ) (string , error ) {
128
154
now := time .Now ()
129
155
claims := lfs.Claims {
@@ -216,15 +242,6 @@ func runServ(c *cli.Context) error {
216
242
}
217
243
218
244
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
- }
228
245
229
246
rr := strings .SplitN (repoPath , "/" , 2 )
230
247
if len (rr ) != 2 {
@@ -261,28 +278,28 @@ func runServ(c *cli.Context) error {
261
278
}()
262
279
}
263
280
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 {
266
291
return fail (ctx , "Unknown git command" , "Unknown git command %s" , verb )
267
292
}
268
293
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 )
278
295
279
296
results , extra := private .ServCommand (ctx , keyID , username , reponame , requestedMode , verb , lfsVerb )
280
297
if extra .HasError () {
281
298
return fail (ctx , extra .UserMsg , "ServCommand failed: %s" , extra .Error )
282
299
}
283
300
284
301
// LFS token authentication
285
- if verb == lfsAuthenticateVerb {
302
+ if verb == verbLfsAuthenticate {
286
303
url := fmt .Sprintf ("%s%s/%s.git/info/lfs" , setting .AppURL , url .PathEscape (results .OwnerName ), url .PathEscape (results .RepoName ))
287
304
288
305
token , err := getLFSAuthToken (ctx , lfsVerb , results )
0 commit comments