diff options
author | zeripath <art27@cantab.net> | 2019-01-24 14:12:17 +0000 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-01-24 16:12:17 +0200 |
commit | 44371b96f56d408ed9af487d482ea021bfabeafa (patch) | |
tree | ef1255b481b05eba285f1267920097d194ca8cc9 /models/user.go | |
parent | cd83c2ca051f9d6a3f7b2842e19aaa2c069cf769 (diff) | |
download | gitea-44371b96f56d408ed9af487d482ea021bfabeafa.tar.gz gitea-44371b96f56d408ed9af487d482ea021bfabeafa.zip |
Ensure valid git author names passed in signatures (#5774)
* Ensure valid git author names passed in signatures
Fix #5772 - Git author names are not allowed to include `\n` `<` or `>` and
must not be empty. Ensure that the name passed in a signature is valid.
* Account for pathologically named external users
LDAP and the like usernames are not checked in the same way that users who signup are.
Therefore just ensure that user names are also git safe and if totally pathological -
Set them to "user-$UID"
* Add Tests and adjust test users
Make our testcases a little more pathological so that we be sure that integration
tests have a chance to spot these cases.
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'models/user.go')
-rw-r--r-- | models/user.go | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/models/user.go b/models/user.go index 0d8f608861..4ab78ec04e 100644 --- a/models/user.go +++ b/models/user.go @@ -417,7 +417,7 @@ func (u *User) GetFollowing(page int) ([]*User, error) { // NewGitSig generates and returns the signature of given user. func (u *User) NewGitSig() *git.Signature { return &git.Signature{ - Name: u.DisplayName(), + Name: u.GitName(), Email: u.getEmail(), When: time.Now(), } @@ -630,12 +630,33 @@ func (u *User) GetOrganizations(all bool) error { // DisplayName returns full name if it's not empty, // returns username otherwise. func (u *User) DisplayName() string { - if len(u.FullName) > 0 { - return u.FullName + trimmed := strings.TrimSpace(u.FullName) + if len(trimmed) > 0 { + return trimmed } return u.Name } +func gitSafeName(name string) string { + return strings.TrimSpace(strings.NewReplacer("\n", "", "<", "", ">", "").Replace(name)) +} + +// GitName returns a git safe name +func (u *User) GitName() string { + gitName := gitSafeName(u.FullName) + if len(gitName) > 0 { + return gitName + } + // Although u.Name should be safe if created in our system + // LDAP users may have bad names + gitName = gitSafeName(u.Name) + if len(gitName) > 0 { + return gitName + } + // Totally pathological name so it's got to be: + return fmt.Sprintf("user-%d", u.ID) +} + // ShortName ellipses username to length func (u *User) ShortName(length int) string { return base.EllipsisString(u.Name, length) |