@@ -483,6 +483,9 @@ var routes = []route{
483
483
{regexp .MustCompile (`(.*?)/objects/pack/pack-[0-9a-f]{40}\.idx$` ), "GET" , getIdxFile },
484
484
}
485
485
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
+
486
489
func getGitConfig (option , dir string ) string {
487
490
out , err := git .NewCommand ("config" , option ).RunInDir (dir )
488
491
if err != nil {
@@ -553,14 +556,16 @@ func serviceRPC(h serviceHandler, service string) {
553
556
// set this for allow pre-receive and post-receive execute
554
557
h .environ = append (h .environ , "SSH_ORIGINAL_COMMAND=" + service )
555
558
559
+ if protocol := h .r .Header .Get ("Git-Protocol" ); protocol != "" && safeGitProtocolHeader .MatchString (protocol ) {
560
+ h .environ = append (h .environ , "GIT_PROTOCOL=" + protocol )
561
+ }
562
+
556
563
ctx , cancel := gocontext .WithCancel (git .DefaultContext )
557
564
defer cancel ()
558
565
var stderr bytes.Buffer
559
566
cmd := exec .CommandContext (ctx , git .GitExecutable , service , "--stateless-rpc" , h .dir )
560
567
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 ... )
564
569
cmd .Stdout = h .w
565
570
cmd .Stdin = reqBody
566
571
cmd .Stderr = & stderr
@@ -610,7 +615,13 @@ func getInfoRefs(h serviceHandler) {
610
615
h .setHeaderNoCache ()
611
616
if hasAccess (getServiceType (h .r ), h , false ) {
612
617
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 )
614
625
if err != nil {
615
626
log .Error (fmt .Sprintf ("%v - %s" , err , string (refs )))
616
627
}
0 commit comments