Skip to content

Commit 239a217

Browse files
committed
improvements
1 parent 1a5bfff commit 239a217

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

routers/web/repo/githttp.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,22 +407,27 @@ func (h *serviceHandler) sendFile(ctx *context.Context, contentType, file string
407407
// one or more key=value pairs separated by colons
408408
var safeGitProtocolHeader = regexp.MustCompile(`^[0-9a-zA-Z]+=[0-9a-zA-Z]+(:[0-9a-zA-Z]+=[0-9a-zA-Z]+)*$`)
409409

410+
func isAllowedRPCServiceType(service string) bool {
411+
return service == ServiceTypeUploadPack || service == ServiceTypeReceivePack
412+
}
413+
410414
func serviceRPC(ctx *context.Context, h *serviceHandler, service string) {
411415
defer func() {
412416
if err := ctx.Req.Body.Close(); err != nil {
413417
log.Error("serviceRPC: Close: %v", err)
414418
}
415419
}()
416420

417-
expectedContentType := fmt.Sprintf("application/x-git-%s-request", service)
418-
if ctx.Req.Header.Get("Content-Type") != expectedContentType {
419-
log.Error("Content-Type (%q) doesn't match expected: %q", ctx.Req.Header.Get("Content-Type"), expectedContentType)
421+
if !isAllowedRPCServiceType(service) {
422+
log.Error("Invalid service: %q", service)
423+
// FIXME: why it's 401 if the service type doesn't supported?
420424
ctx.Resp.WriteHeader(http.StatusUnauthorized)
421425
return
422426
}
423427

424-
if service != "upload-pack" && service != "receive-pack" {
425-
log.Error("Invalid service: %q", service)
428+
expectedContentType := fmt.Sprintf("application/x-git-%s-request", service)
429+
if ctx.Req.Header.Get("Content-Type") != expectedContentType {
430+
log.Error("Content-Type (%q) doesn't match expected: %q", ctx.Req.Header.Get("Content-Type"), expectedContentType)
426431
ctx.Resp.WriteHeader(http.StatusUnauthorized)
427432
return
428433
}
@@ -503,7 +508,7 @@ func GetInfoRefs(ctx *context.Context) {
503508
}
504509
setHeaderNoCache(ctx)
505510
service := getServiceType(ctx)
506-
if service == "upload-pack" || service == "receive-pack" {
511+
if service == ServiceTypeUploadPack || service == ServiceTypeReceivePack {
507512
if protocol := ctx.Req.Header.Get("Git-Protocol"); protocol != "" && safeGitProtocolHeader.MatchString(protocol) {
508513
h.environ = append(h.environ, "GIT_PROTOCOL="+protocol)
509514
}

0 commit comments

Comments
 (0)