Skip to content

Commit

Permalink
repo_editor: prohibit move files to to .git directory (#6986)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed May 31, 2022
1 parent 519aeef commit 90bc752
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -22,6 +22,9 @@ All notable changes to Gogs are documented in this file.

### Fixed

- _Security:_ XSS in cookies. [#6953](https://github.com/gogs/gogs/issues/6953)
- _Security:_ OS Command Injection in file uploading. [#6968](https://github.com/gogs/gogs/issues/6968)
- _Security:_ Remote Command Execution in file editing. [#6555](https://github.com/gogs/gogs/issues/6555)
- Unable to use LDAP authentication on ARM machines. [#6761](https://github.com/gogs/gogs/issues/6761)

### Removed
Expand Down
12 changes: 9 additions & 3 deletions internal/db/repo_editor.go
Expand Up @@ -121,6 +121,11 @@ type UpdateRepoFileOptions struct {

// UpdateRepoFile adds or updates a file in repository.
func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (err error) {
// 🚨 SECURITY: Prevent uploading files into the ".git" directory
if isRepositoryGitPath(opts.NewTreeName) {
return errors.Errorf("bad tree path %q", opts.NewTreeName)
}

repoWorkingPool.CheckIn(com.ToStr(repo.ID))
defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))

Expand Down Expand Up @@ -458,7 +463,8 @@ type UploadRepoFileOptions struct {
Files []string // In UUID format
}

// isRepositoryGitPath returns true if given path is or resides inside ".git" path of the repository.
// isRepositoryGitPath returns true if given path is or resides inside ".git"
// path of the repository.
func isRepositoryGitPath(path string) bool {
return strings.HasSuffix(path, ".git") ||
strings.Contains(path, ".git"+string(os.PathSeparator)) ||
Expand All @@ -472,7 +478,7 @@ func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions)
return nil
}

// Prevent uploading files into the ".git" directory
// 🚨 SECURITY: Prevent uploading files into the ".git" directory
if isRepositoryGitPath(opts.TreePath) {
return errors.Errorf("bad tree path %q", opts.TreePath)
}
Expand Down Expand Up @@ -512,7 +518,7 @@ func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions)

upload.Name = pathutil.Clean(upload.Name)

// Prevent uploading files into the ".git" directory
// 🚨 SECURITY: Prevent uploading files into the ".git" directory
if isRepositoryGitPath(upload.Name) {
continue
}
Expand Down

0 comments on commit 90bc752

Please sign in to comment.