Browse Source

Fix formatting of contact messages

master
Jan-Lukas Else 3 months ago
parent
commit
286c0f821a
  1. 12
      contact.go
  2. 4
      markdown.go
  3. 3
      posts.go
  4. 4
      search.go
  5. 10
      utils.go
  6. 5
      utils_test.go

12
contact.go

@ -7,10 +7,7 @@ import (
"net/http"
"net/smtp"
"strconv"
"strings"
"time"
"github.com/microcosm-cc/bluemonday"
)
const defaultContactPath = "/contact"
@ -30,15 +27,14 @@ func (a *goBlog) serveContactForm(w http.ResponseWriter, r *http.Request) {
func (a *goBlog) sendContactSubmission(w http.ResponseWriter, r *http.Request) {
// Get form values
strict := bluemonday.StrictPolicy()
// Name
formName := strings.TrimSpace(strict.Sanitize(r.FormValue("name")))
formName := cleanHTMLText(r.FormValue("name"))
// Email
formEmail := strings.TrimSpace(strict.Sanitize(r.FormValue("email")))
formEmail := cleanHTMLText(r.FormValue("email"))
// Website
formWebsite := strings.TrimSpace(strict.Sanitize(r.FormValue("website")))
formWebsite := cleanHTMLText(r.FormValue("website"))
// Message
formMessage := strings.TrimSpace(strict.Sanitize(r.FormValue("message")))
formMessage := cleanHTMLText(r.FormValue("message"))
if formMessage == "" {
a.serveError(w, r, "Message is empty", http.StatusBadRequest)
return

4
markdown.go

@ -90,7 +90,7 @@ func (a *goBlog) renderText(s string) string {
if err != nil {
return ""
}
return htmlText(h)
return htmlText(string(h))
}
func (a *goBlog) renderMdTitle(s string) string {
@ -99,7 +99,7 @@ func (a *goBlog) renderMdTitle(s string) string {
if err != nil {
return ""
}
return htmlText(buffer.Bytes())
return htmlText(buffer.String())
}
// Extensions etc...

3
posts.go

@ -12,7 +12,6 @@ import (
"sync"
"github.com/go-chi/chi/v5"
"github.com/microcosm-cc/bluemonday"
"github.com/vcraescu/go-paginator"
)
@ -220,7 +219,7 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
search := chi.URLParam(r, "search")
if search != "" {
// Decode and sanitize search
search = htmlText([]byte(bluemonday.StrictPolicy().Sanitize(searchDecode(search))))
search = cleanHTMLText(searchDecode(search))
}
pageNoString := chi.URLParam(r, "page")
pageNo, _ := strconv.Atoi(pageNoString)

4
search.go

@ -7,8 +7,6 @@ import (
"net/url"
"path"
"strings"
"github.com/microcosm-cc/bluemonday"
)
const defaultSearchPath = "/search"
@ -24,7 +22,7 @@ func (a *goBlog) serveSearch(w http.ResponseWriter, r *http.Request) {
}
if q := r.Form.Get("q"); q != "" {
// Clean query
q = htmlText([]byte(bluemonday.StrictPolicy().Sanitize(q)))
q = cleanHTMLText(q)
// Redirect to results
http.Redirect(w, r, path.Join(servePath, searchEncode(q)), http.StatusFound)
return

10
utils.go

@ -1,7 +1,6 @@
package main
import (
"bytes"
"crypto/sha256"
"fmt"
"html/template"
@ -16,6 +15,7 @@ import (
"github.com/PuerkitoBio/goquery"
"github.com/araddon/dateparse"
"github.com/c2h5oh/datasize"
"github.com/microcosm-cc/bluemonday"
"github.com/thoas/go-funk"
)
@ -233,14 +233,18 @@ func mBytesString(size int64) string {
return fmt.Sprintf("%.2f MB", datasize.ByteSize(size).MBytes())
}
func htmlText(b []byte) string {
d, err := goquery.NewDocumentFromReader(bytes.NewReader(b))
func htmlText(s string) string {
d, err := goquery.NewDocumentFromReader(strings.NewReader(s))
if err != nil {
return ""
}
return strings.TrimSpace(d.Text())
}
func cleanHTMLText(s string) string {
return htmlText(bluemonday.StrictPolicy().Sanitize(s))
}
func defaultIfEmpty(s, d string) string {
if s != "" {
return s

5
utils_test.go

@ -72,3 +72,8 @@ func Test_urlHasExt(t *testing.T) {
assert.Equal(t, "jpg", ext)
})
}
func Test_cleanHTMLText(t *testing.T) {
assert.Equal(t, `"This is a 'test'" 😁`, cleanHTMLText(`"This is a 'test'" 😁`))
assert.Equal(t, `Test`, cleanHTMLText(`<b>Test</b>`))
}

Loading…
Cancel
Save