Skip to content

Commit 95013fd

Browse files
authored
Fix push-create SSH bugs (#10145)
* Attempt to fix push-create SSH bugs Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix binding Signed-off-by: jolheiser <john.olheiser@gmail.com> * Invalid ctx Signed-off-by: jolheiser <john.olheiser@gmail.com>
1 parent 7dcd305 commit 95013fd

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

cmd/serv.go

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/url"
1313
"os"
1414
"os/exec"
15+
"regexp"
1516
"strconv"
1617
"strings"
1718
"time"
@@ -72,6 +73,7 @@ var (
7273
"git-receive-pack": models.AccessModeWrite,
7374
lfsAuthenticateVerb: models.AccessModeNone,
7475
}
76+
alphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`)
7577
)
7678

7779
func fail(userMessage, logMessage string, args ...interface{}) {
@@ -147,6 +149,10 @@ func runServ(c *cli.Context) error {
147149
username := strings.ToLower(rr[0])
148150
reponame := strings.ToLower(strings.TrimSuffix(rr[1], ".git"))
149151

152+
if alphaDashDotPattern.MatchString(reponame) {
153+
fail("Invalid repo name", "Invalid repo name: %s", reponame)
154+
}
155+
150156
if setting.EnablePprof || c.Bool("enable-pprof") {
151157
if err := os.MkdirAll(setting.PprofDataPath, os.ModePerm); err != nil {
152158
fail("Error while trying to create PPROF_DATA_PATH", "Error while trying to create PPROF_DATA_PATH: %v", err)

integrations/git_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@ func doPushCreate(ctx APITestContext, u *url.URL) func(t *testing.T) {
422422
tmpDir, err := ioutil.TempDir("", ctx.Reponame)
423423
assert.NoError(t, err)
424424

425+
_, err = git.NewCommand("clone", u.String()).RunInDir(tmpDir)
426+
assert.Error(t, err)
427+
425428
err = git.InitRepository(tmpDir, false)
426429
assert.NoError(t, err)
427430

@@ -449,13 +452,26 @@ func doPushCreate(ctx APITestContext, u *url.URL) func(t *testing.T) {
449452
_, err = git.NewCommand("remote", "add", "origin", u.String()).RunInDir(tmpDir)
450453
assert.NoError(t, err)
451454

455+
invalidCtx := ctx
456+
invalidCtx.Reponame = fmt.Sprintf("invalid/repo-tmp-push-create-%s", u.Scheme)
457+
u.Path = invalidCtx.GitPath()
458+
459+
_, err = git.NewCommand("remote", "add", "invalid", u.String()).RunInDir(tmpDir)
460+
assert.NoError(t, err)
461+
452462
// Push to create disabled
453463
setting.Repository.EnablePushCreateUser = false
454464
_, err = git.NewCommand("push", "origin", "master").RunInDir(tmpDir)
455465
assert.Error(t, err)
456466

457467
// Push to create enabled
458468
setting.Repository.EnablePushCreateUser = true
469+
470+
// Invalid repo
471+
_, err = git.NewCommand("push", "invalid", "master").RunInDir(tmpDir)
472+
assert.Error(t, err)
473+
474+
// Valid repo
459475
_, err = git.NewCommand("push", "origin", "master").RunInDir(tmpDir)
460476
assert.NoError(t, err)
461477

routers/private/serv.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ func ServNoCommand(ctx *macaron.Context) {
6868

6969
// ServCommand returns information about the provided keyid
7070
func ServCommand(ctx *macaron.Context) {
71-
// Although we provide the verbs we don't need them at present they're just for logging purposes
7271
keyID := ctx.ParamsInt64(":keyid")
7372
ownerName := ctx.Params(":owner")
7473
repoName := ctx.Params(":repo")
@@ -105,6 +104,17 @@ func ServCommand(ctx *macaron.Context) {
105104
if err != nil {
106105
if models.IsErrRepoNotExist(err) {
107106
repoExist = false
107+
for _, verb := range ctx.QueryStrings("verb") {
108+
if "git-upload-pack" == verb {
109+
// User is fetching/cloning a non-existent repository
110+
ctx.JSON(http.StatusNotFound, map[string]interface{}{
111+
"results": results,
112+
"type": "ErrRepoNotExist",
113+
"err": fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
114+
})
115+
return
116+
}
117+
}
108118
} else {
109119
log.Error("Unable to get repository: %s/%s Error: %v", results.OwnerName, results.RepoName, err)
110120
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{

0 commit comments

Comments
 (0)