Skip to content

Commit 2b9daab

Browse files
committed
Unified media type check in router.
1 parent e5bcd0d commit 2b9daab

File tree

2 files changed

+15
-36
lines changed

2 files changed

+15
-36
lines changed

routers/routes/web.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,13 +1101,13 @@ func RegisterRoutes(m *web.Route) {
11011101

11021102
m.Group("/{reponame}", func() {
11031103
m.Group("/info/lfs", func() {
1104-
m.Post("/objects/batch", lfs.BatchHandler)
1104+
m.Post("/objects/batch", lfs.CheckAcceptMediaType, lfs.BatchHandler)
11051105
m.Get("/objects/{oid}/{filename}", lfs.DownloadHandler)
11061106
m.Get("/objects/{oid}", lfs.DownloadHandler)
11071107
m.Put("/objects/{oid}", lfs.UploadHandler)
1108-
m.Any("/objects/{oid}", lfs.LegacyMetaHandler)
1109-
m.Post("/objects", lfs.LegacyPostHandler)
1110-
m.Post("/verify/{oid}", lfs.VerifyHandler)
1108+
m.Any("/objects/{oid}", lfs.CheckAcceptMediaType, lfs.LegacyMetaHandler)
1109+
m.Post("/objects", lfs.CheckAcceptMediaType, lfs.LegacyPostHandler)
1110+
m.Post("/verify/{oid}", lfs.CheckAcceptMediaType, lfs.VerifyHandler)
11111111
m.Group("/locks", func() {
11121112
m.Get("/", lfs.GetListLockHandler)
11131113
m.Post("/", lfs.PostLockHandler)

services/lfs/server.go

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ func (rc *requestContext) VerifyLink(oid string) string {
4949
return setting.AppURL + path.Join(rc.User, rc.Repo+".git", "info/lfs/verify", oid)
5050
}
5151

52+
// CheckAcceptMediaType checks if the client accepts the LFS media type.
53+
func CheckAcceptMediaType(ctx *context.Context) {
54+
mediaParts := strings.Split(r.Header.Get("Accept"), ";")
55+
56+
if mediaParts[0] != lfs_module.MediaType {
57+
log.Info("Calling a LFS method without accepting the correct media type: %s", lfs_module.MediaType)
58+
writeStatus(ctx, http.StatusBadRequest)
59+
return
60+
}
61+
}
62+
5263
func getAuthenticatedRepoAndMeta(ctx *context.Context, rc *requestContext, p lfs_module.Pointer, requireWrite bool) (*models.LFSMetaObject, *models.Repository) {
5364
if !p.IsValid() {
5465
log.Info("Attempt to access invalid LFS OID[%s] in %s/%s", p.Oid, rc.User, rc.Repo)
@@ -157,12 +168,6 @@ func DownloadHandler(ctx *context.Context) {
157168

158169
// LegacyMetaHandler retrieves metadata about the object
159170
func LegacyMetaHandler(ctx *context.Context) {
160-
if !isValidAccept(ctx.Req) {
161-
log.Info("Attempt to call without accepting the correct media type: %s", lfs_module.MediaType)
162-
writeStatus(ctx, http.StatusBadRequest)
163-
return
164-
}
165-
166171
rc, p := unpack(ctx)
167172

168173
meta, _ := getAuthenticatedRepoAndMeta(ctx, rc, p, false)
@@ -186,12 +191,6 @@ func LegacyMetaHandler(ctx *context.Context) {
186191

187192
// LegacyPostHandler instructs the client how to upload data
188193
func LegacyPostHandler(ctx *context.Context) {
189-
if !isValidAccept(ctx.Req) {
190-
log.Info("Attempt to POST without accepting the correct media type: %s", lfs_module.MediaType)
191-
writeStatus(ctx, http.StatusBadRequest)
192-
return
193-
}
194-
195194
rc, p := unpack(ctx)
196195

197196
repository, err := models.GetRepositoryByOwnerAndName(rc.User, rc.Repo)
@@ -250,12 +249,6 @@ func LegacyPostHandler(ctx *context.Context) {
250249

251250
// BatchHandler provides the batch api
252251
func BatchHandler(ctx *context.Context) {
253-
if !isValidAccept(ctx.Req) {
254-
log.Info("Attempt to BATCH without accepting the correct media type: %s", lfs_module.MediaType)
255-
writeStatus(ctx, http.StatusBadRequest)
256-
return
257-
}
258-
259252
bv := unpackbatch(ctx)
260253

261254
var isUpload bool
@@ -383,12 +376,6 @@ func UploadHandler(ctx *context.Context) {
383376

384377
// VerifyHandler verify oid and its size from the content store
385378
func VerifyHandler(ctx *context.Context) {
386-
if !isValidAccept(ctx.Req) {
387-
log.Info("Attempt to VERIFY without accepting the correct media type: %s", lfs_module.MediaType)
388-
writeStatus(ctx, http.StatusBadRequest)
389-
return
390-
}
391-
392379
rc, p := unpack(ctx)
393380

394381
meta, _ := getAuthenticatedRepoAndMeta(ctx, rc, p, true)
@@ -449,14 +436,6 @@ func buildObjectResponse(rc *requestContext, pointer lfs_module.Pointer, downloa
449436
return rep
450437
}
451438

452-
// isValidAccept provides a mux.MatcherFunc that only allows requests that contain
453-
// an Accept header with the lfs_module.MediaType
454-
func isValidAccept(r *http.Request) bool {
455-
mediaParts := strings.Split(r.Header.Get("Accept"), ";")
456-
mt := mediaParts[0]
457-
return mt == lfs_module.MediaType
458-
}
459-
460439
func unpack(ctx *context.Context) (*requestContext, lfs_module.Pointer) {
461440
rc := getRequestContext(ctx)
462441
p := lfs_module.Pointer{Oid: ctx.Params("oid")}

0 commit comments

Comments
 (0)