forked from tangled.org/core
Monorepo for Tangled

appview,knotserver: pass repo handle to knot when proxying git ops

Adds 'x-tangled-repo-owner-handle' header when proxying git requests to
the knot. This allows knotserver to attempt to suggest the correct
git+ssh URL when rejecting a HTTP git push.

authored by tjh.dev and committed by anirudh.fi 4d5576b1 5139c88a

Changed files
+16
appview
state
knotserver
+3
appview/state/git_http.go
··· 73 73 // Copy original headers 74 74 proxyReq.Header = r.Header 75 75 76 + repoOwnerHandle := chi.URLParam(r, "user") 77 + proxyReq.Header.Add("x-tangled-repo-owner-handle", repoOwnerHandle) 78 + 76 79 // Execute request 77 80 resp, err := client.Do(proxyReq) 78 81 if err != nil {
+13
knotserver/git.go
··· 6 6 "io" 7 7 "net/http" 8 8 "path/filepath" 9 + "strings" 9 10 10 11 securejoin "github.com/cyphar/filepath-securejoin" 11 12 "github.com/go-chi/chi/v5" ··· 113 114 w.WriteHeader(http.StatusForbidden) 114 115 115 116 fmt.Fprintf(w, "Welcome to Tangled.sh!\n\nPushes are currently only supported over SSH.") 117 + 118 + // If the appview gave us the repository owner's handle we can attempt to 119 + // construct the correct ssh url. 120 + ownerHandle := r.Header.Get("x-tangled-repo-owner-handle") 121 + if ownerHandle != "" && !strings.ContainsAny(ownerHandle, ":") { 122 + hostname := d.c.Server.Hostname 123 + if strings.Contains(hostname, ":") { 124 + hostname = strings.Split(hostname, ":")[0] 125 + } 126 + 127 + fmt.Fprintf(w, " Try:\ngit remote set-url --push origin git@%s:%s/%s\n\n... and push again.", hostname, ownerHandle, unqualifiedRepoName) 128 + } 116 129 fmt.Fprintf(w, "\n\n") 117 130 } 118 131