diff options
author | Ethan Koenig <ethantkoenig@gmail.com> | 2017-12-08 07:12:47 -0800 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2017-12-08 17:12:47 +0200 |
commit | ec6718ef403e0b89f8b649aef225b94b2369725e (patch) | |
tree | d0eab9810c5f5bd85252351f1cf8e0166adf1f4b /models | |
parent | 8f7054a8645e56575435d66ffa8d82d40620165b (diff) | |
download | gitea-ec6718ef403e0b89f8b649aef225b94b2369725e.tar.gz gitea-ec6718ef403e0b89f8b649aef225b94b2369725e.zip |
Sanitize logs for mirror sync (#3057, #3082) (#3078)
* Sanitize logs for mirror sync
* Fix error message sanitiziation (#3082)
Diffstat (limited to 'models')
-rw-r--r-- | models/repo.go | 7 | ||||
-rw-r--r-- | models/repo_mirror.go | 70 |
2 files changed, 48 insertions, 29 deletions
diff --git a/models/repo.go b/models/repo.go index 8d57ae51a5..c570acd6f8 100644 --- a/models/repo.go +++ b/models/repo.go @@ -605,9 +605,14 @@ func (repo *Repository) RepoPath() string { return repo.repoPath(x) } +// GitConfigPath returns the path to a repository's git config/ directory +func GitConfigPath(repoPath string) string { + return filepath.Join(repoPath, "config") +} + // GitConfigPath returns the repository git config path func (repo *Repository) GitConfigPath() string { - return filepath.Join(repo.RepoPath(), "config") + return GitConfigPath(repo.RepoPath()) } // RelLink returns the repository relative link diff --git a/models/repo_mirror.go b/models/repo_mirror.go index 77cd98faa8..f52b3eb452 100644 --- a/models/repo_mirror.go +++ b/models/repo_mirror.go @@ -6,18 +6,18 @@ package models import ( "fmt" - "strings" "time" - "github.com/Unknwon/com" - "github.com/go-xorm/xorm" - "gopkg.in/ini.v1" - "code.gitea.io/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/sync" + "code.gitea.io/gitea/modules/util" + + "github.com/Unknwon/com" + "github.com/go-xorm/xorm" + "gopkg.in/ini.v1" ) // MirrorQueue holds an UniqueQueue object of the mirror @@ -76,41 +76,41 @@ func (m *Mirror) ScheduleNextUpdate() { m.NextUpdate = time.Now().Add(m.Interval) } +func remoteAddress(repoPath string) (string, error) { + cfg, err := ini.Load(GitConfigPath(repoPath)) + if err != nil { + return "", err + } + return cfg.Section("remote \"origin\"").Key("url").Value(), nil +} + func (m *Mirror) readAddress() { if len(m.address) > 0 { return } - - cfg, err := ini.Load(m.Repo.GitConfigPath()) + var err error + m.address, err = remoteAddress(m.Repo.RepoPath()) if err != nil { - log.Error(4, "Load: %v", err) - return + log.Error(4, "remoteAddress: %v", err) } - m.address = cfg.Section("remote \"origin\"").Key("url").Value() } -// HandleCloneUserCredentials replaces user credentials from HTTP/HTTPS URL -// with placeholder <credentials>. -// It will fail for any other forms of clone addresses. -func HandleCloneUserCredentials(url string, mosaics bool) string { - i := strings.Index(url, "@") - if i == -1 { - return url - } - start := strings.Index(url, "://") - if start == -1 { - return url - } - if mosaics { - return url[:start+3] + "<credentials>" + url[i:] +// sanitizeOutput sanitizes output of a command, replacing occurrences of the +// repository's remote address with a sanitized version. +func sanitizeOutput(output, repoPath string) (string, error) { + remoteAddr, err := remoteAddress(repoPath) + if err != nil { + // if we're unable to load the remote address, then we're unable to + // sanitize. + return "", err } - return url[:start+3] + url[i+1:] + return util.SanitizeMessage(output, remoteAddr), nil } // Address returns mirror address from Git repository config without credentials. func (m *Mirror) Address() string { m.readAddress() - return HandleCloneUserCredentials(m.address, false) + return util.SanitizeURLCredentials(m.address, false) } // FullAddress returns mirror address from Git repository config. @@ -145,7 +145,14 @@ func (m *Mirror) runSync() bool { if _, stderr, err := process.GetManager().ExecDir( timeout, repoPath, fmt.Sprintf("Mirror.runSync: %s", repoPath), "git", gitArgs...); err != nil { - desc := fmt.Sprintf("Failed to update mirror repository '%s': %s", repoPath, stderr) + // sanitize the output, since it may contain the remote address, which may + // contain a password + message, err := sanitizeOutput(stderr, repoPath) + if err != nil { + log.Error(4, "sanitizeOutput: %v", err) + return false + } + desc := fmt.Sprintf("Failed to update mirror repository '%s': %s", repoPath, message) log.Error(4, desc) if err = CreateRepositoryNotice(desc); err != nil { log.Error(4, "CreateRepositoryNotice: %v", err) @@ -170,7 +177,14 @@ func (m *Mirror) runSync() bool { if _, stderr, err := process.GetManager().ExecDir( timeout, wikiPath, fmt.Sprintf("Mirror.runSync: %s", wikiPath), "git", "remote", "update", "--prune"); err != nil { - desc := fmt.Sprintf("Failed to update mirror wiki repository '%s': %s", wikiPath, stderr) + // sanitize the output, since it may contain the remote address, which may + // contain a password + message, err := sanitizeOutput(stderr, wikiPath) + if err != nil { + log.Error(4, "sanitizeOutput: %v", err) + return false + } + desc := fmt.Sprintf("Failed to update mirror wiki repository '%s': %s", wikiPath, message) log.Error(4, desc) if err = CreateRepositoryNotice(desc); err != nil { log.Error(4, "CreateRepositoryNotice: %v", err) |