Skip to content

Commit

Permalink
Add regexp template tag validation
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Nov 26, 2018
1 parent a7614eb commit 8a952c1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (s SubscriberAttribs) Scan(src interface{}) error {
}

// CompileTemplate compiles a campaign body template into its base
// template and sets the resultant template to Campaign.Tpl
// template and sets the resultant template to Campaign.Tpl.
func (c *Campaign) CompileTemplate(f template.FuncMap) error {
// Compile the base template.
t := regexpLinkTag.ReplaceAllString(c.TemplateBody, regexpLinkTagReplace)
Expand Down
10 changes: 7 additions & 3 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"

"github.com/asaskevich/govalidator"
"github.com/knadh/listmonk/models"
Expand All @@ -32,6 +32,10 @@ type dummyMessage struct {
UnsubscribeURL string
}

var (
regexpTplTag = regexp.MustCompile(`{{(\s+)?template\s+?"content"(\s+)?\.(\s+)?}}`)
)

// handleGetTemplates handles retrieval of templates.
func handleGetTemplates(c echo.Context) error {
var (
Expand Down Expand Up @@ -76,7 +80,7 @@ func handlePreviewTemplate(c echo.Context) error {
)

if body != "" {
if strings.Count(body, tplTag) != 1 {
if !regexpTplTag.MatchString(body) {
return echo.NewHTTPError(http.StatusBadRequest,
fmt.Sprintf("Template body should contain the %s placeholder exactly once", tplTag))
}
Expand Down Expand Up @@ -243,7 +247,7 @@ func validateTemplate(o models.Template) error {
return errors.New("invalid length for `name`")
}

if strings.Count(o.Body, tplTag) != 1 {
if !regexpTplTag.MatchString(o.Body) {
return fmt.Errorf("template body should contain the %s placeholder exactly once", tplTag)
}

Expand Down

0 comments on commit 8a952c1

Please sign in to comment.