forked from tangled.org/core
Monorepo for Tangled — https://tangled.org

knotserver: allow downloading archives of refs with slashes in their names

This allows us to be more precise when downloading e.g. tags that have the
same name as branches (in which case Git defaults to the branch, not sure
what go-git does).

Signed-off-by: Winter <winter@winter.cafe>

Changed files
+11 -3
knotserver
+11 -3
knotserver/routes.go
···
ref := strings.TrimSuffix(file, ".tar.gz")
+
unescapedRef, err := url.PathUnescape(ref)
+
if err != nil {
+
notFound(w)
+
return
+
}
+
+
safeRefFilename := strings.ReplaceAll(plumbing.ReferenceName(unescapedRef).Short(), "/", "-")
+
// This allows the browser to use a proper name for the file when
// downloading
-
filename := fmt.Sprintf("%s-%s.tar.gz", name, ref)
+
filename := fmt.Sprintf("%s-%s.tar.gz", name, safeRefFilename)
setContentDisposition(w, filename)
setGZipMIME(w)
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
-
gr, err := git.Open(path, ref)
+
gr, err := git.Open(path, unescapedRef)
if err != nil {
notFound(w)
return
···
gw := gzip.NewWriter(w)
defer gw.Close()
-
prefix := fmt.Sprintf("%s-%s", name, ref)
+
prefix := fmt.Sprintf("%s-%s", name, safeRefFilename)
err = gr.WriteTar(gw, prefix)
if err != nil {
// once we start writing to the body we can't report error anymore