From 8b544b0a435f0e12dcaedd3ba5519b830b82c4a2 Mon Sep 17 00:00:00 2001 From: Umputun Date: Sat, 12 May 2018 11:42:05 -0500 Subject: [PATCH] move comment size validation to raw comment #3 --- app/rest/api/rest.go | 7 ++++++- app/store/service.go | 7 ------- app/store/service_test.go | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index 70684735..1460c450 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -137,9 +137,14 @@ func (s *Rest) createCommentCtrl(w http.ResponseWriter, r *http.Request) { return } - comment.PrepareUntrusted() // clean all fields user not suppoed to set + comment.PrepareUntrusted() // clean all fields user not supposed to set comment.User = user comment.User.IP = strings.Split(r.RemoteAddr, ":")[0] + if err = s.DataService.ValidateComment(&comment); err != nil { + rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "invalid comment") + return + } + comment.Text = string(blackfriday.Run([]byte(comment.Text), blackfriday.WithExtensions(mdExt))) log.Printf("[DEBUG] create comment %+v", comment) diff --git a/app/store/service.go b/app/store/service.go index c373f5ef..7d621ff0 100644 --- a/app/store/service.go +++ b/app/store/service.go @@ -31,9 +31,6 @@ func (s *Service) Create(comment Comment) (commentID string, err error) { comment.Votes = make(map[string]bool) } - if err = s.ValidateComment(&comment); err != nil { - return "", err - } comment.Sanitize() // clear potentially dangerous js from all parts of comment comment.User.hashIP(s.Secret) // replace ip by hash @@ -111,10 +108,6 @@ func (s *Service) EditComment(locator Locator, commentID string, text string, ed comment.Edit = &edit comment.Edit.Timestamp = time.Now() - if err = s.ValidateComment(&comment); err != nil { - return comment, err - } - comment.Sanitize() err = s.Put(locator, comment) return comment, err diff --git a/app/store/service_test.go b/app/store/service_test.go index 84ec4fb6..fab06b62 100644 --- a/app/store/service_test.go +++ b/app/store/service_test.go @@ -85,7 +85,7 @@ func TestService_Vote(t *testing.T) { assert.Equal(t, map[string]bool{"user1": true}, c.Votes, "user voted +") c, err = b.Vote(Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "user", true) - assert.NotNil(t, "self-voting not allowed") + assert.NotNil(t, err, "self-voting not allowed") _, err = b.Vote(Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "user1", true) assert.NotNil(t, err, "double-voting rejected") @@ -178,7 +178,7 @@ func TestService_EditCommentDurationFailed(t *testing.T) { time.Sleep(time.Second) - comment, err = b.EditComment(Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "xxx", + _, err = b.EditComment(Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "xxx", Edit{Summary: "my edit"}) assert.NotNil(t, err) }