Skip to content

Commit 64c2ec6

Browse files
committed
Fix /verify LFS handler expecting wrong content-type
Fixes #6960 According to [spec][1], /verify requests must have `Accept: application/vnd.git-lfs+json` Previous code works because `git-lfs` also [violates spec and doesn't send any Accept header at all][2] For other clients that DO set `Accept: application/vnd.git-lfs+json`, addition of `Accept: application/vnd.git-lfs` either forces them to violate the spec or is ignored, depending on order in what they create header list. [1]: https://github.com/git-lfs/git-lfs/blob/master/docs/api/basic-transfers.md#verification [2]: git-lfs/git-lfs#3662
1 parent 6eb53ac commit 64c2ec6

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

modules/lfs/server.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import (
2222
)
2323

2424
const (
25-
contentMediaType = "application/vnd.git-lfs"
26-
metaMediaType = contentMediaType + "+json"
25+
metaMediaType = "application/vnd.git-lfs+json"
2726
)
2827

2928
// RequestVars contain variables from the HTTP request. Variables from routing, json body decoding, and
@@ -101,11 +100,10 @@ func ObjectOidHandler(ctx *context.Context) {
101100
getMetaHandler(ctx)
102101
return
103102
}
104-
if ContentMatcher(ctx.Req) || len(ctx.Params("filename")) > 0 {
105-
getContentHandler(ctx)
106-
return
107-
}
108-
} else if ctx.Req.Method == "PUT" && ContentMatcher(ctx.Req) {
103+
104+
getContentHandler(ctx)
105+
return
106+
} else if ctx.Req.Method == "PUT" {
109107
PutHandler(ctx)
110108
return
111109
}
@@ -348,7 +346,7 @@ func VerifyHandler(ctx *context.Context) {
348346
return
349347
}
350348

351-
if !ContentMatcher(ctx.Req) {
349+
if !MetaMatcher(ctx.Req) {
352350
writeStatus(ctx, 400)
353351
return
354352
}
@@ -385,7 +383,6 @@ func Represent(rv *RequestVars, meta *models.LFSMetaObject, download, upload boo
385383
}
386384

387385
header := make(map[string]string)
388-
header["Accept"] = contentMediaType
389386

390387
if rv.Authorization == "" {
391388
//https://github.com/github/git-lfs/issues/1088
@@ -404,20 +401,20 @@ func Represent(rv *RequestVars, meta *models.LFSMetaObject, download, upload boo
404401

405402
if upload && !download {
406403
// Force client side verify action while gitea lacks proper server side verification
407-
rep.Actions["verify"] = &link{Href: rv.VerifyLink(), Header: header}
404+
verifyHeader := make(map[string]string)
405+
for k, v := range header {
406+
verifyHeader[k] = v
407+
}
408+
409+
// This is only needed to workaround https://github.com/git-lfs/git-lfs/issues/3662
410+
verifyHeader["Accept"] = metaMediaType
411+
412+
rep.Actions["verify"] = &link{Href: rv.VerifyLink(), Header: verifyHeader}
408413
}
409414

410415
return rep
411416
}
412417

413-
// ContentMatcher provides a mux.MatcherFunc that only allows requests that contain
414-
// an Accept header with the contentMediaType
415-
func ContentMatcher(r macaron.Request) bool {
416-
mediaParts := strings.Split(r.Header.Get("Accept"), ";")
417-
mt := mediaParts[0]
418-
return mt == contentMediaType
419-
}
420-
421418
// MetaMatcher provides a mux.MatcherFunc that only allows requests that contain
422419
// an Accept header with the metaMediaType
423420
func MetaMatcher(r macaron.Request) bool {

0 commit comments

Comments
 (0)