Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix(smtp): Check From Name in SMTP is not a Email

LinkinStar48a54e81399c
diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml
index ab5c5141..e41188e0 100644
--- a/i18n/en_US.yaml
+++ b/i18n/en_US.yaml
@@ -111,6 +111,9 @@ backend:
other: "No permission to update."
cannot_set_synonym_as_itself:
other: "You cannot set the synonym of the current tag as itself."
+ smtp:
+ config_from_name_cannot_be_email:
+ other: "The From Name cannot be a email address."
theme:
not_found:
other: "Theme not found."
diff --git a/internal/base/reason/reason.go b/internal/base/reason/reason.go
index 915098e0..9d2880e0 100644
--- a/internal/base/reason/reason.go
+++ b/internal/base/reason/reason.go
@@ -60,4 +60,5 @@ const (
UserCannotUpdateYourRole = "error.user.cannot_update_your_role"
TagCannotSetSynonymAsItself = "error.tag.cannot_set_synonym_as_itself"
NotAllowedRegistration = "error.user.not_allowed_registration"
+ SMTPConfigFromNameCannotBeEmail = "error.smtp.config_from_name_cannot_be_email"
)
diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go
index 16ccd211..a05c10c4 100644
--- a/internal/schema/siteinfo_schema.go
+++ b/internal/schema/siteinfo_schema.go
@@ -3,10 +3,14 @@ package schema
import (
"context"
"fmt"
+ "net/mail"
"net/url"
"github.com/answerdev/answer/internal/base/handler"
+ "github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/base/translator"
+ "github.com/answerdev/answer/internal/base/validator"
+ "github.com/segmentfault/pacman/errors"
)
const PermaLinkQuestionIDAndTitle = 1
@@ -190,6 +194,17 @@ type UpdateSMTPConfigReq struct {
TestEmailRecipient string `validate:"omitempty,email" json:"test_email_recipient"`
}
+func (r *UpdateSMTPConfigReq) Check() (errField []*validator.FormErrorField, err error) {
+ _, err = mail.ParseAddress(r.FromName)
+ if err == nil {
+ return append(errField, &validator.FormErrorField{
+ ErrorField: "from_name",
+ ErrorMsg: reason.SMTPConfigFromNameCannotBeEmail,
+ }), errors.BadRequest(reason.SMTPConfigFromNameCannotBeEmail)
+ }
+ return nil, nil
+}
+
// GetSMTPConfigResp get smtp config response
type GetSMTPConfigResp struct {
FromEmail string `json:"from_email"`