sanitize on preview

This commit is contained in:
Umputun
2018-05-06 17:29:34 -05:00
parent 56faa40499
commit a78b3a2d6d
4 changed files with 7 additions and 5 deletions
+2
View File
@@ -164,6 +164,8 @@ func (s *Rest) previewCommentCtrl(w http.ResponseWriter, r *http.Request) {
rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't bind comment")
return
}
comment.Sanitize()
comment.Text = string(blackfriday.Run([]byte(comment.Text), blackfriday.WithExtensions(mdExt)))
render.HTML(w, r, comment.Text)
}
+2 -2
View File
@@ -84,8 +84,8 @@ func (c *Comment) SetDeleted() {
c.Deleted = true
}
// sanitize clean dangerous html/js from the comment
func (c *Comment) sanitize() {
// Sanitize clean dangerous html/js from the comment
func (c *Comment) Sanitize() {
p := bluemonday.UGCPolicy()
c.Text = p.Sanitize(c.Text)
c.User.ID = template.HTMLEscapeString(c.User.ID)
+1 -1
View File
@@ -27,7 +27,7 @@ func TestComment_Sanitize(t *testing.T) {
}
for n, tt := range tbl {
tt.inp.sanitize()
tt.inp.Sanitize()
assert.Equal(t, tt.out, tt.inp, "check #%d", n)
}
}
+2 -2
View File
@@ -28,7 +28,7 @@ func (s *Service) Create(comment Comment) (commentID string, err error) {
comment.Votes = make(map[string]bool)
}
comment.sanitize() // clear potentially dangerous js from all parts of comment
comment.Sanitize() // clear potentially dangerous js from all parts of comment
comment.User.hashIP(s.Secret) // replace ip by hash
return s.Interface.Create(comment)
@@ -100,7 +100,7 @@ func (s *Service) EditComment(locator Locator, commentID string, text string, ed
comment.Text = text
comment.Edit = &edit
comment.Edit.Timestamp = time.Now()
comment.sanitize()
comment.Sanitize()
err = s.Put(locator, comment)
return comment, err
}