@@ -483,6 +483,9 @@ var routes = []route{
483483 {regexp .MustCompile (`(.*?)/objects/pack/pack-[0-9a-f]{40}\.idx$` ), "GET" , getIdxFile },
484484}
485485
486+ // one or more key=value pairs separated by colons
487+ var safeGitProtocolHeader = regexp .MustCompile (`^[0-9a-zA-Z]+=[0-9a-zA-Z]+(:[0-9a-zA-Z]+=[0-9a-zA-Z]+)*$` )
488+
486489func getGitConfig (option , dir string ) string {
487490 out , err := git .NewCommand ("config" , option ).RunInDir (dir )
488491 if err != nil {
@@ -553,14 +556,16 @@ func serviceRPC(h serviceHandler, service string) {
553556 // set this for allow pre-receive and post-receive execute
554557 h .environ = append (h .environ , "SSH_ORIGINAL_COMMAND=" + service )
555558
559+ if protocol := h .r .Header .Get ("Git-Protocol" ); protocol != "" && safeGitProtocolHeader .MatchString (protocol ) {
560+ h .environ = append (h .environ , "GIT_PROTOCOL=" + protocol )
561+ }
562+
556563 ctx , cancel := gocontext .WithCancel (git .DefaultContext )
557564 defer cancel ()
558565 var stderr bytes.Buffer
559566 cmd := exec .CommandContext (ctx , git .GitExecutable , service , "--stateless-rpc" , h .dir )
560567 cmd .Dir = h .dir
561- if service == "receive-pack" {
562- cmd .Env = append (os .Environ (), h .environ ... )
563- }
568+ cmd .Env = append (os .Environ (), h .environ ... )
564569 cmd .Stdout = h .w
565570 cmd .Stdin = reqBody
566571 cmd .Stderr = & stderr
@@ -610,7 +615,13 @@ func getInfoRefs(h serviceHandler) {
610615 h .setHeaderNoCache ()
611616 if hasAccess (getServiceType (h .r ), h , false ) {
612617 service := getServiceType (h .r )
613- refs , err := git .NewCommand (service , "--stateless-rpc" , "--advertise-refs" , "." ).RunInDirBytes (h .dir )
618+
619+ if protocol := h .r .Header .Get ("Git-Protocol" ); protocol != "" && safeGitProtocolHeader .MatchString (protocol ) {
620+ h .environ = append (h .environ , "GIT_PROTOCOL=" + protocol )
621+ }
622+ h .environ = append (os .Environ (), h .environ ... )
623+
624+ refs , err := git .NewCommand (service , "--stateless-rpc" , "--advertise-refs" , "." ).RunInDirTimeoutEnv (h .environ , - 1 , h .dir )
614625 if err != nil {
615626 log .Error (fmt .Sprintf ("%v - %s" , err , string (refs )))
616627 }
0 commit comments