package testutil import ( "fmt" "io" "net" "net/http" "net/http/cgi" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "testing" "time" ) // ServePrivateGit routes HTTPS Git traffic to a local authenticated smart-HTTP // backend serving repo at /org/repo.git. It never resolves or dials the requested // hostname, so configured and lookalike origins both remain entirely local. func ServePrivateGit(t *testing.T, repo, username, password string) { t.Helper() root := t.TempDir() if err := os.Mkdir(filepath.Join(root, "org"), 0o755); err != nil { t.Fatal(err) } RunGit(t, root, "clone", "--bare", repo, filepath.Join(root, "org", "repo.git")) gitPath, err := exec.LookPath("git") if err != nil { t.Fatal(err) } backend := &cgi.Handler{ Path: gitPath, Args: []string{"http-backend"}, Env: []string{"GIT_PROJECT_ROOT=" + root, "GIT_HTTP_EXPORT_ALL=1"}, } remote := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { user, pass, ok := r.BasicAuth() if !ok || user != username || pass != password { w.Header().Set("WWW-Authenticate", `Basic realm="private repo"`) w.WriteHeader(http.StatusUnauthorized) return } backend.ServeHTTP(w, r) })) t.Cleanup(remote.Close) proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodConnect { http.Error(w, "CONNECT required", http.StatusMethodNotAllowed) return } upstream, err := net.DialTimeout("tcp", strings.TrimPrefix(remote.URL, "https://"), 5*time.Second) if err != nil { http.Error(w, err.Error(), http.StatusBadGateway) return } defer upstream.Close() conn, buffered, err := w.(http.Hijacker).Hijack() if err != nil { return } defer conn.Close() deadline := time.Now().Add(10 * time.Second) _ = conn.SetDeadline(deadline) _ = upstream.SetDeadline(deadline) if _, err := fmt.Fprint(conn, "HTTP/1.1 200 Connection Established\r\n\r\n"); err != nil { return } go func() { _, _ = io.Copy(upstream, buffered) }() _, _ = io.Copy(conn, upstream) })) t.Cleanup(proxy.Close) global := filepath.Join(t.TempDir(), "gitconfig") RunGit(t, root, "config", "--file", global, "http.proxy", proxy.URL) t.Setenv("GIT_CONFIG_GLOBAL", global) t.Setenv("GIT_CONFIG_NOSYSTEM", "1") t.Setenv("GIT_CONFIG_COUNT", "0") t.Setenv("GIT_SSL_NO_VERIFY", "true") t.Setenv("GIT_TERMINAL_PROMPT", "0") t.Setenv("GIT_ASKPASS", "") t.Setenv("NO_PROXY", "") t.Setenv("no_proxy", "") t.Setenv("SKILLSHARE_GIT_TOKEN", "") }