From d1439329244c8339944fd48ffc751c9769836297 Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Sat, 2 Dec 2023 13:28:21 +0100 Subject: [PATCH] add MIN_COMMENT_SIZE parameter --- backend/app/cmd/server.go | 2 ++ backend/app/rest/api/rest.go | 2 ++ backend/app/store/service/service.go | 11 ++++++++--- backend/app/store/service/service_test.go | 3 ++- site/src/docs/configuration/parameters/index.md | 1 + 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index 442c420e..5944eb2c 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -65,6 +65,7 @@ type ServerCommand struct { BackupLocation string `long:"backup" env:"BACKUP_PATH" default:"./var/backup" description:"backups location"` MaxBackupFiles int `long:"max-back" env:"MAX_BACKUP_FILES" default:"10" description:"max backups to keep"` LegacyImageProxy bool `long:"img-proxy" env:"IMG_PROXY" description:"[deprecated, use image-proxy.http2https] enable image proxy"` + MinCommentSize int `long:"min-comment" env:"MIN_COMMENT_SIZE" default:"0" description:"min comment size"` MaxCommentSize int `long:"max-comment" env:"MAX_COMMENT_SIZE" default:"2048" description:"max comment size"` MaxVotes int `long:"max-votes" env:"MAX_VOTES" default:"-1" description:"maximum number of votes per comment"` RestrictVoteIP bool `long:"votes-ip" env:"VOTES_IP" description:"restrict votes from the same ip"` @@ -502,6 +503,7 @@ func (s *ServerCommand) newServerApp(ctx context.Context) (*serverApp, error) { EditDuration: s.EditDuration, AdminEdits: s.AdminEdit, AdminStore: adminStore, + MinCommentSize: s.MinCommentSize, MaxCommentSize: s.MaxCommentSize, MaxVotes: s.MaxVotes, PositiveScore: s.PositiveScore, diff --git a/backend/app/rest/api/rest.go b/backend/app/rest/api/rest.go index 40ef95cd..2e667ab0 100644 --- a/backend/app/rest/api/rest.go +++ b/backend/app/rest/api/rest.go @@ -419,6 +419,7 @@ func (s *Rest) configCtrl(w http.ResponseWriter, r *http.Request) { Version string `json:"version"` EditDuration int `json:"edit_duration"` AdminEdit bool `json:"admin_edit"` + MinCommentSize int `json:"min_comment_size"` MaxCommentSize int `json:"max_comment_size"` Admins []string `json:"admins"` AdminEmail string `json:"admin_email"` @@ -439,6 +440,7 @@ func (s *Rest) configCtrl(w http.ResponseWriter, r *http.Request) { Version: s.Version, EditDuration: int(s.DataService.EditDuration.Seconds()), AdminEdit: s.DataService.AdminEdits, + MinCommentSize: s.DataService.MinCommentSize, MaxCommentSize: s.DataService.MaxCommentSize, Admins: admins, AdminEmail: emails, diff --git a/backend/app/store/service/service.go b/backend/app/store/service/service.go index d811602b..b6a8a045 100644 --- a/backend/app/store/service/service.go +++ b/backend/app/store/service/service.go @@ -27,6 +27,7 @@ type DataStore struct { Engine engine.Interface EditDuration time.Duration AdminStore admin.Store + MinCommentSize int MaxCommentSize int MaxVotes int RestrictSameIPVotes struct { @@ -69,7 +70,7 @@ type PostMetaData struct { ReadOnly bool `json:"read_only"` } -const defaultCommentMaxSize = 2000 +const defaultCommentMaxSize = 2048 const maxLastCommentsReply = 5000 // UnlimitedVotes doesn't restrict MaxVotes @@ -639,8 +640,12 @@ func (s *DataStore) ValidateComment(c *store.Comment) error { if c.Orig == "" { return fmt.Errorf("empty comment text") } - if len([]rune(c.Orig)) > maxSize { - return fmt.Errorf("comment text exceeded max allowed size %d (%d)", maxSize, len([]rune(c.Orig))) + commentLength := len([]rune(c.Orig)) + if commentLength > maxSize { + return fmt.Errorf("comment text exceeded max allowed size %d (%d)", maxSize, commentLength) + } + if s.MinCommentSize > 0 && commentLength < s.MinCommentSize { + return fmt.Errorf("comment text is smaller than min allowed size %d (%d)", s.MinCommentSize, commentLength) } if c.User.ID == "" || c.User.Name == "" { return fmt.Errorf("empty user info") diff --git a/backend/app/store/service/service_test.go b/backend/app/store/service/service_test.go index 195b6b3c..6fe75b39 100644 --- a/backend/app/store/service/service_test.go +++ b/backend/app/store/service/service_test.go @@ -924,7 +924,7 @@ func TestService_EditCommentAdmin(t *testing.T) { } func TestService_ValidateComment(t *testing.T) { - b := DataStore{MaxCommentSize: 2000, AdminStore: admin.NewStaticKeyStore("secret 123")} + b := DataStore{MinCommentSize: 6, MaxCommentSize: 2000, AdminStore: admin.NewStaticKeyStore("secret 123")} longText := fmt.Sprintf("%4000s", "X") tbl := []struct { @@ -934,6 +934,7 @@ func TestService_ValidateComment(t *testing.T) { {inp: store.Comment{}, err: "empty comment text"}, {inp: store.Comment{Orig: "something blah", User: store.User{ID: "myid", Name: "name"}}, err: ""}, {inp: store.Comment{Orig: "something blah", User: store.User{ID: "myid"}}, err: "empty user info"}, + {inp: store.Comment{Orig: "short", User: store.User{ID: "myid", Name: "name"}}, err: "comment text is smaller than min allowed size 6 (5)"}, {inp: store.Comment{Orig: longText, User: store.User{ID: "myid", Name: "name"}}, err: "comment text exceeded max allowed size 2000 (4000)"}, {inp: store.Comment{Orig: "here is a link with relative URL: [google.com](url)", User: store.User{ID: "myid", Name: "name"}}, err: "links should start with mailto:, http:// or https://"}, {inp: store.Comment{Orig: "here is a link with relative URL: [google.com](url)", User: store.User{ID: "myid", Name: "name"}}, err: "links should start with mailto:, http:// or https://"}, diff --git a/site/src/docs/configuration/parameters/index.md b/site/src/docs/configuration/parameters/index.md index 12c8262a..8b46daa4 100644 --- a/site/src/docs/configuration/parameters/index.md +++ b/site/src/docs/configuration/parameters/index.md @@ -135,6 +135,7 @@ services: | ssl.acme-location | SSL_ACME_LOCATION | `./var/acme` | dir where obtained le-certs will be stored | | ssl.acme-email | SSL_ACME_EMAIL | | admin email for receiving notifications from LE | | max-comment | MAX_COMMENT_SIZE | `2048` | comment's size limit | +| min-comment | MIN_COMMENT_SIZE | `0` | comment's minimal size limit, `0` - unlimited | | max-votes | MAX_VOTES | `-1` | votes limit per comment, `-1` - unlimited | | votes-ip | VOTES_IP | `false` | restrict votes from the same IP | | anon-vote | ANON_VOTE | `false` | allow voting for anonymous users, require VOTES_IP to be enabled as well |