Correctly escape strings if needed and check for it

This commit is contained in:
Jason Streifling 2024-10-16 16:51:39 +02:00
parent c200d5bf73
commit f27116930a
4 changed files with 26 additions and 1 deletions

12
atom.go
View File

@ -28,6 +28,18 @@ func isValidURI(uri URI) bool {
return isValidURL(uri) || isValidURN(uri)
}
func isCorrectlyEscaped(text string) bool {
relevantEntities := []string{"&", "<", ">", """, "'"}
for _, entity := range relevantEntities {
if strings.Contains(text, entity) {
return false
}
}
return true
}
func isCompositeMediaType(mediaType string) bool {
mediaType, _, err := mime.ParseMediaType(mediaType)
if err != nil {

View File

@ -3,6 +3,7 @@ package atomfeed
import (
"errors"
"fmt"
"html"
)
type Category struct {
@ -22,6 +23,10 @@ func NewCategory(term string) (*Category, error) {
return &Category{Term: term, Content: content}, nil
}
func (c *Category) SetLabel(label string) {
c.Label = html.UnescapeString(label)
}
func (c *Category) Check() error {
if c.Term == "" {
return errors.New("term attribute of category empty")
@ -33,6 +38,10 @@ func (c *Category) Check() error {
}
}
if !isCorrectlyEscaped(c.Label) {
return fmt.Errorf("label attribute of category %v not correctly escaped", c.Label)
}
if c.Content == nil {
return errors.New("no content element of category")
} else {

View File

@ -15,6 +15,10 @@ func (p *PlainText) Check() error {
return errors.New("type attribute of plain text must be text or html if not omitted")
}
if p.Type == "html" && !isCorrectlyEscaped(p.Text) {
return errors.New("text element of plain text not correctly escaped")
}
if p.Text == "" {
return errors.New("text element of plain text empty")
}

View File

@ -15,7 +15,7 @@ func NewText(textType, content string) (Text, error) {
case "text", "":
return &PlainText{Type: textType, Text: content}, nil
case "html":
return &PlainText{Type: textType, Text: html.EscapeString(content)}, nil
return &PlainText{Type: textType, Text: html.UnescapeString(content)}, nil
case "xhtml":
return &XHTMLText{
Type: textType,