Skip to content

Commit 360b3fd

Browse files
authored
Include username in email headers (#28981)
Emails from Gitea comments do not contain the username of the commenter anywhere, only their display name, so it is not possible to verify who made a comment from the email itself: From: "Alice" <email@gitea> X-Gitea-Sender: Alice X-Gitea-Recipient: Bob X-GitHub-Sender: Alice X-GitHub-Recipient: Bob This comment looks like it's from @alice. The X-Gitea/X-GitHub headers also use display names, which is not very reliable for filtering, and inconsistent with GitHub's behavior: X-GitHub-Sender: lunny X-GitHub-Recipient: gwymor This change includes both the display name and username in the From header, and switches the other headers from display name to username: From: "Alice (@fakeAlice)" <email@gitea> X-Gitea-Sender: fakealice X-Gitea-Recipient: bob X-GitHub-Sender: fakealice X-GitHub-Recipient: bob This comment looks like it's from @alice.
1 parent a6cea59 commit 360b3fd

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

models/user/user.go

+11
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,17 @@ func (u *User) GetDisplayName() string {
443443
return u.Name
444444
}
445445

446+
// GetCompleteName returns the the full name and username in the form of
447+
// "Full Name (@username)" if full name is not empty, otherwise it returns
448+
// "@username".
449+
func (u *User) GetCompleteName() string {
450+
trimmedFullName := strings.TrimSpace(u.FullName)
451+
if len(trimmedFullName) > 0 {
452+
return fmt.Sprintf("%s (@%s)", trimmedFullName, u.Name)
453+
}
454+
return fmt.Sprintf("@%s", u.Name)
455+
}
456+
446457
func gitSafeName(name string) string {
447458
return strings.TrimSpace(strings.NewReplacer("\n", "", "<", "", ">", "").Replace(name))
448459
}

services/mailer/mail.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,13 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipient
310310

311311
msgs := make([]*Message, 0, len(recipients))
312312
for _, recipient := range recipients {
313-
msg := NewMessageFrom(recipient.Email, ctx.Doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
313+
msg := NewMessageFrom(
314+
recipient.Email,
315+
ctx.Doer.GetCompleteName(),
316+
setting.MailService.FromEmail,
317+
subject,
318+
mailBody.String(),
319+
)
314320
msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
315321

316322
msg.SetHeader("Message-ID", msgID)
@@ -394,8 +400,8 @@ func generateAdditionalHeaders(ctx *mailCommentContext, reason string, recipient
394400

395401
"X-Mailer": "Gitea",
396402
"X-Gitea-Reason": reason,
397-
"X-Gitea-Sender": ctx.Doer.DisplayName(),
398-
"X-Gitea-Recipient": recipient.DisplayName(),
403+
"X-Gitea-Sender": ctx.Doer.Name,
404+
"X-Gitea-Recipient": recipient.Name,
399405
"X-Gitea-Recipient-Address": recipient.Email,
400406
"X-Gitea-Repository": repo.Name,
401407
"X-Gitea-Repository-Path": repo.FullName(),
@@ -404,8 +410,8 @@ func generateAdditionalHeaders(ctx *mailCommentContext, reason string, recipient
404410
"X-Gitea-Issue-Link": ctx.Issue.HTMLURL(),
405411

406412
"X-GitHub-Reason": reason,
407-
"X-GitHub-Sender": ctx.Doer.DisplayName(),
408-
"X-GitHub-Recipient": recipient.DisplayName(),
413+
"X-GitHub-Sender": ctx.Doer.Name,
414+
"X-GitHub-Recipient": recipient.Name,
409415
"X-GitHub-Recipient-Address": recipient.Email,
410416

411417
"X-GitLab-NotificationReason": reason,

services/mailer/mail_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,16 @@ func TestGenerateAdditionalHeaders(t *testing.T) {
239239
doer, _, issue, _ := prepareMailerTest(t)
240240

241241
ctx := &mailCommentContext{Context: context.TODO() /* TODO: use a correct context */, Issue: issue, Doer: doer}
242-
recipient := &user_model.User{Name: "Test", Email: "[email protected]"}
242+
recipient := &user_model.User{Name: "test", Email: "[email protected]"}
243243

244244
headers := generateAdditionalHeaders(ctx, "dummy-reason", recipient)
245245

246246
expected := map[string]string{
247247
"List-ID": "user2/repo1 <repo1.user2.localhost>",
248248
"List-Archive": "<https://try.gitea.io/user2/repo1>",
249249
"X-Gitea-Reason": "dummy-reason",
250-
"X-Gitea-Sender": "< U<se>r Tw<o > ><",
251-
"X-Gitea-Recipient": "Test",
250+
"X-Gitea-Sender": "user2",
251+
"X-Gitea-Recipient": "test",
252252
"X-Gitea-Recipient-Address": "[email protected]",
253253
"X-Gitea-Repository": "repo1",
254254
"X-Gitea-Repository-Path": "user2/repo1",

0 commit comments

Comments
 (0)