From 607971b80ff7f191eca6563f4fdd5fc7a2397f03 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 28 Jul 2019 19:11:28 +0100 Subject: [PATCH 1/5] Explode out mirror username and password --- models/repo_mirror.go | 26 +++++++++++++++++++++++++- modules/auth/repo_form.go | 16 +++++++++------- options/locale/locale_en-US.ini | 2 +- routers/repo/setting.go | 11 +++++++++++ templates/repo/settings/options.tmpl | 19 ++++++++++++++++++- 5 files changed, 64 insertions(+), 10 deletions(-) diff --git a/models/repo_mirror.go b/models/repo_mirror.go index 528e9daa8b9f..e784f08ab22c 100644 --- a/models/repo_mirror.go +++ b/models/repo_mirror.go @@ -7,6 +7,7 @@ package models import ( "fmt" + "net/url" "strings" "time" @@ -118,7 +119,7 @@ func sanitizeOutput(output, repoPath string) (string, error) { return util.SanitizeMessage(output, remoteAddr), nil } -// Address returns mirror address from Git repository config without credentials. +// Address returns mirror address from Git repository config with credentials censored. func (m *Mirror) Address() string { m.readAddress() return util.SanitizeURLCredentials(m.address, false) @@ -130,6 +131,29 @@ func (m *Mirror) FullAddress() string { return m.address } +// AddressNoCredentials returns mirror address from Git repository config without credentials. +func (m *Mirror) AddressNoCredentials() string { + m.readAddress() + u, err := url.Parse(m.address) + if err != nil { + // this shouldn't happen but just return it unsanitised + return m.address + } + u.User = nil + return u.String() +} + +// Username returns the mirror address username +func (m *Mirror) Username() string { + m.readAddress() + u, err := url.Parse(m.address) + if err != nil { + // this shouldn't happen but just return it unsanitised + return "" + } + return u.User.Username() +} + // SaveAddress writes new address to Git repository config. func (m *Mirror) SaveAddress(addr string) error { repoPath := m.Repo.RepoPath() diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index 0333c3c92614..cdac210ddeaf 100644 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -98,13 +98,15 @@ func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) { // RepoSettingForm form for changing repository settings type RepoSettingForm struct { - RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"` - Description string `binding:"MaxSize(255)"` - Website string `binding:"ValidUrl;MaxSize(255)"` - Interval string - MirrorAddress string - Private bool - EnablePrune bool + RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"` + Description string `binding:"MaxSize(255)"` + Website string `binding:"ValidUrl;MaxSize(255)"` + Interval string + MirrorAddress string + MirrorUsername string + MirrorPassword string + Private bool + EnablePrune bool // Advanced settings EnableWiki bool diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index e01d0b67dcc4..951bc3cf01de 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -584,7 +584,7 @@ mirror_prune_desc = Remove obsolete remote-tracking references mirror_interval = Mirror Interval (valid time units are 'h', 'm', 's'). 0 to disable automatic sync. mirror_interval_invalid = The mirror interval is not valid. mirror_address = Clone From URL -mirror_address_desc = Include any required authorization credentials in the URL. These must be url escaped as appropriate +mirror_address_desc = Put any required credentials in the Clone Authorization section. mirror_address_url_invalid = The provided url is invalid. You must escape all components of the url correctly. mirror_address_protocol_invalid = The provided url is invalid. Only http(s):// or git:// locations can be mirrored from. mirror_last_synced = Last Synchronized diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 757295069e90..4a2f9aa096ad 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -155,6 +155,9 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { } } + // Get previous address - error can be ignored, as we will be updating anyway. + oldURL, _ := url.Parse(ctx.Repo.Mirror.Address()) + // Validate the form.MirrorAddress u, err := url.Parse(form.MirrorAddress) if err != nil { @@ -169,6 +172,14 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { return } + if form.MirrorUsername != "" { + password := form.MirrorPassword + if password == "" && oldURL != nil && u.Hostname() == oldURL.Hostname() { + password, _ = oldURL.User.Password() + } + u.User = url.UserPassword(form.MirrorUsername, password) + } + // Now use xurls address := validFormAddress.FindString(form.MirrorAddress) if address != form.MirrorAddress && form.MirrorAddress != "" { diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index c6d715acbee8..8bbbb033613f 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -80,9 +80,26 @@
- +

{{.i18n.Tr "repo.mirror_address_desc"}}

+
+
From 9705ff397cb133e8a8b59348b62ccbb21a4692a0 Mon Sep 17 00:00:00 2001 From: zeripath Date: Sun, 28 Jul 2019 22:40:15 +0100 Subject: [PATCH 2/5] Update models/repo_mirror.go --- models/repo_mirror.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/repo_mirror.go b/models/repo_mirror.go index e784f08ab22c..b185debc4897 100644 --- a/models/repo_mirror.go +++ b/models/repo_mirror.go @@ -148,7 +148,7 @@ func (m *Mirror) Username() string { m.readAddress() u, err := url.Parse(m.address) if err != nil { - // this shouldn't happen but just return it unsanitised + // this shouldn't happen but if it does return "" return "" } return u.User.Username() From 216d58de41179c5eb7c47e7df9fee328abf3b0ae Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 29 Jul 2019 17:11:20 +0100 Subject: [PATCH 3/5] Just roundtrip the password --- models/repo_mirror.go | 12 ++++++++++++ routers/repo/setting.go | 8 ++------ templates/repo/settings/options.tmpl | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/models/repo_mirror.go b/models/repo_mirror.go index b185debc4897..6dac032a72f2 100644 --- a/models/repo_mirror.go +++ b/models/repo_mirror.go @@ -154,6 +154,18 @@ func (m *Mirror) Username() string { return u.User.Username() } +// Password returns the mirror address password +func (m *Mirror) Password() string { + m.readAddress() + u, err := url.Parse(m.address) + if err != nil { + // this shouldn't happen but if it does return "" + return "" + } + password, _ := u.User.Password() + return password +} + // SaveAddress writes new address to Git repository config. func (m *Mirror) SaveAddress(addr string) error { repoPath := m.Repo.RepoPath() diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 4a2f9aa096ad..4f649f586a5f 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -172,12 +172,8 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { return } - if form.MirrorUsername != "" { - password := form.MirrorPassword - if password == "" && oldURL != nil && u.Hostname() == oldURL.Hostname() { - password, _ = oldURL.User.Password() - } - u.User = url.UserPassword(form.MirrorUsername, password) + if form.MirrorUsername != "" || form.MirrorPassword != "" { + u.User = url.UserPassword(form.MirrorUsername, form.MirrorPassword) } // Now use xurls diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index 8bbbb033613f..d65a46e9696f 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -96,7 +96,7 @@
- +
From a4b021fddf42ad77162c9eafecca4ec42d6e7581 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 29 Jul 2019 17:36:31 +0100 Subject: [PATCH 4/5] remove unused declaration --- routers/repo/setting.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 4f649f586a5f..cf13bc039641 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -155,9 +155,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { } } - // Get previous address - error can be ignored, as we will be updating anyway. - oldURL, _ := url.Parse(ctx.Repo.Mirror.Address()) - // Validate the form.MirrorAddress u, err := url.Parse(form.MirrorAddress) if err != nil { From b1473ebdd4486923d4610e47029af1a58f888ce2 Mon Sep 17 00:00:00 2001 From: zeripath Date: Tue, 6 Aug 2019 06:41:52 +0100 Subject: [PATCH 5/5] Update templates/repo/settings/options.tmpl --- templates/repo/settings/options.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index d65a46e9696f..87e23b693700 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -86,7 +86,7 @@