- add a content textarea (size huge) after description, with en and pl phrases - add content to Category.Fillable - export classes.CheckHTML and run it from FormBeforeCreate and FormBeforeUpdate: unsafe category content answers 422 on content, blank content is stored as NULL
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package classes
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/yuin/goldmark"
|
|
"github.com/yuin/goldmark/extension"
|
|
"github.com/yuin/goldmark/parser"
|
|
)
|
|
|
|
var (
|
|
markdownEngine = goldmark.New(
|
|
goldmark.WithExtensions(extension.Footnote, extension.Table),
|
|
goldmark.WithParserOptions(parser.WithAttribute()),
|
|
)
|
|
|
|
markdownUnsafeTag = regexp.MustCompile(`(?i)<(?:script|iframe|object|embed)\b`)
|
|
markdownEventHandler = regexp.MustCompile(`(?i)\son[a-z]+\s*=`)
|
|
markdownDangerousURL = regexp.MustCompile(`(?i)(?:javascript|vbscript|data):`)
|
|
)
|
|
|
|
// FormatHTML converts journal post markdown to stored HTML using goldmark
|
|
// footnote, table, and attribute extensions, then the same rejectUnsafe gate
|
|
// as cabana.RenderMarkdown. It does not call cabana.RenderMarkdown.
|
|
func FormatHTML(src string) (string, error) {
|
|
var buf bytes.Buffer
|
|
if err := markdownEngine.Convert([]byte(strings.TrimSpace(src)), &buf); err != nil {
|
|
return "", fmt.Errorf("journal: markdown: %w", err)
|
|
}
|
|
html := buf.String()
|
|
if err := rejectUnsafe(html); err != nil {
|
|
return "", err
|
|
}
|
|
return html, nil
|
|
}
|
|
|
|
// CheckHTML applies the FormatHTML safety gate to HTML that is stored as is,
|
|
// such as category content. It refuses raw script, iframe, object and embed
|
|
// tags, event handler attributes and javascript, vbscript or data URL schemes.
|
|
func CheckHTML(html string) error {
|
|
return rejectUnsafe(html)
|
|
}
|
|
|
|
func rejectUnsafe(html string) error {
|
|
if markdownUnsafeTag.MatchString(html) {
|
|
return fmt.Errorf("journal: rendered HTML contains raw unsafe tags")
|
|
}
|
|
if markdownEventHandler.MatchString(html) {
|
|
return fmt.Errorf("journal: rendered HTML contains event handlers")
|
|
}
|
|
if markdownDangerousURL.MatchString(html) {
|
|
return fmt.Errorf("journal: rendered HTML contains a dangerous URL scheme")
|
|
}
|
|
return nil
|
|
}
|