Skip to content

Commit

Permalink
add MIN_COMMENT_SIZE parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed Dec 2, 2023
1 parent dcc7613 commit 0efd697
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions backend/app/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions backend/app/rest/api/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions backend/app/store/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type DataStore struct {
Engine engine.Interface
EditDuration time.Duration
AdminStore admin.Store
MinCommentSize int
MaxCommentSize int
MaxVotes int
RestrictSameIPVotes struct {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion backend/app/store/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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://"},
Expand Down
1 change: 1 addition & 0 deletions site/src/docs/configuration/parameters/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down

0 comments on commit 0efd697

Please sign in to comment.