Fix formatting of contact messages

This commit is contained in:
Jan-Lukas Else 2021-09-01 11:14:49 +02:00
parent 116b9eb8cc
commit 286c0f821a
6 changed files with 20 additions and 18 deletions

View File

@ -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

View File

@ -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...

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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>`))
}