Skip to content

Commit

Permalink
Fix path cleanup in multiple places (#3871)
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriks authored and appleboy committed May 1, 2018
1 parent fff022e commit 181b3a8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ type CreateRepoOptions struct {
}

func getRepoInitFile(tp, name string) ([]byte, error) {
cleanedName := strings.TrimLeft(name, "./")
cleanedName := strings.TrimLeft(path.Clean("/"+name), "/")
relPath := path.Join("options", tp, cleanedName)

// Use custom file when available.
Expand Down
18 changes: 14 additions & 4 deletions modules/lfs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type link struct {
ExpiresAt time.Time `json:"expires_at,omitempty"`
}

var oidRegExp = regexp.MustCompile(`^[A-Fa-f0-9]+$`)

// ObjectOidHandler is the main request routing entry point into LFS server functions
func ObjectOidHandler(ctx *context.Context) {

Expand Down Expand Up @@ -217,6 +219,12 @@ func PostHandler(ctx *context.Context) {

if !authenticate(ctx, repository, rv.Authorization, true) {
requireAuth(ctx)
return
}

if !oidRegExp.MatchString(rv.Oid) {
writeStatus(ctx, 404)
return
}

meta, err := models.NewLFSMetaObject(&models.LFSMetaObject{Oid: rv.Oid, Size: rv.Size, RepositoryID: repository.ID})
Expand Down Expand Up @@ -284,10 +292,12 @@ func BatchHandler(ctx *context.Context) {
continue
}

// Object is not found
meta, err = models.NewLFSMetaObject(&models.LFSMetaObject{Oid: object.Oid, Size: object.Size, RepositoryID: repository.ID})
if err == nil {
responseObjects = append(responseObjects, Represent(object, meta, meta.Existing, !contentStore.Exists(meta)))
if oidRegExp.MatchString(object.Oid) {
// Object is not found
meta, err = models.NewLFSMetaObject(&models.LFSMetaObject{Oid: object.Oid, Size: object.Size, RepositoryID: repository.ID})
if err == nil {
responseObjects = append(responseObjects, Represent(object, meta, meta.Existing, !contentStore.Exists(meta)))
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions routers/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bo
branchName = form.NewBranchName
}

form.TreePath = strings.Trim(form.TreePath, " /")
form.TreePath = strings.Trim(path.Clean("/"+form.TreePath), " /")
treeNames, treePaths := getParentTreeFields(form.TreePath)

ctx.Data["TreePath"] = form.TreePath
Expand Down Expand Up @@ -477,7 +477,7 @@ func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) {
branchName = form.NewBranchName
}

form.TreePath = strings.Trim(form.TreePath, " /")
form.TreePath = strings.Trim(path.Clean("/"+form.TreePath), " /")
treeNames, treePaths := getParentTreeFields(form.TreePath)
if len(treeNames) == 0 {
// We must at least have one element for user to input.
Expand Down

0 comments on commit 181b3a8

Please sign in to comment.