Browse Source

Fixed Page model form accepting bad input.

master
Gisle Aune 7 years ago
parent
commit
def7189c35
  1. 2
      model/category.go
  2. 13
      model/page.go
  3. 22
      model/page_test.go

2
model/category.go

@ -32,7 +32,7 @@ var pageCategories []string
// init setups pageCategories
func init() {
pageCategories := make([]string, len(PageCategories))
pageCategories = make([]string, len(PageCategories))
for i, category := range PageCategories {
pageCategories[i] = category.Key
}

13
model/page.go

@ -208,7 +208,12 @@ func (page *Page) ParseForm(form url.Values) []error {
errors := make([]error, 0, 4)
page.cachedOutput = ""
err := formparser.Select(form.Get("category"), &page.Category, pageCategories, page.Category != "")
err := formparser.String(form.Get("name"), &page.Name, 2, 192)
if err != nil {
errors = append(errors, fmt.Errorf("Name: %s", err))
}
err = formparser.Select(form.Get("category"), &page.Category, pageCategories, page.Category != "")
if err != nil {
errors = append(errors, fmt.Errorf("Category: %s", err))
}
@ -231,15 +236,15 @@ func (page *Page) ParseForm(form url.Values) []error {
err = formparser.Select(form.Get("type"), &page.Type, PageTypes, page.Type != "")
if err != nil {
errors = append(errors, fmt.Errorf("Category: %s", err))
errors = append(errors, fmt.Errorf("Type: %s", err))
}
err = formparser.String(form.Get("source"), &page.Source, 0, 102400)
if err != nil {
errors = append(errors, fmt.Errorf("Content is too long, max: 100 KB (~17,000 words)"))
errors = append(errors, fmt.Errorf("Content is too long, max: 100 KB (~16k words)"))
}
if len(errors) > 0 {
if len(errors) == 0 {
errors = nil
}

22
model/page_test.go

@ -1,6 +1,7 @@
package model
import (
"net/url"
"testing"
"time"
@ -289,4 +290,25 @@ func TestPage(t *testing.T) {
}
t.Logf("Deleted %+v", extraPageTag)
})
t.Run("ParseForm", func(t *testing.T) {
page := Page{}
values := url.Values{}
values.Set("name", "Wenera's OSD")
values.Set("category", "Item")
values.Set("type", "Markdown")
values.Set("source", "# Wenera's OSD\n\nIt contains stuff and things")
values.Set("fictionalDate", "2185-10-28T00:00:00Z")
err := page.ParseForm(values)
if err != nil {
t.Errorf("1: %+v", err)
}
assertEquals(t, "page.Name", page.Name, values.Get("name"))
assertEquals(t, "page.Category", page.Category, values.Get("category"))
assertEquals(t, "page.Source", page.Source, values.Get("source"))
t.Log("page.FictionalDate =", page.FictionalDate)
})
}
Loading…
Cancel
Save