Correctly escape strings if needed and check for it
This commit is contained in:
parent
c200d5bf73
commit
f27116930a
12
atom.go
12
atom.go
@ -28,6 +28,18 @@ func isValidURI(uri URI) bool {
|
|||||||
return isValidURL(uri) || isValidURN(uri)
|
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 {
|
func isCompositeMediaType(mediaType string) bool {
|
||||||
mediaType, _, err := mime.ParseMediaType(mediaType)
|
mediaType, _, err := mime.ParseMediaType(mediaType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -3,6 +3,7 @@ package atomfeed
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Category struct {
|
type Category struct {
|
||||||
@ -22,6 +23,10 @@ func NewCategory(term string) (*Category, error) {
|
|||||||
return &Category{Term: term, Content: content}, nil
|
return &Category{Term: term, Content: content}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Category) SetLabel(label string) {
|
||||||
|
c.Label = html.UnescapeString(label)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Category) Check() error {
|
func (c *Category) Check() error {
|
||||||
if c.Term == "" {
|
if c.Term == "" {
|
||||||
return errors.New("term attribute of category empty")
|
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 {
|
if c.Content == nil {
|
||||||
return errors.New("no content element of category")
|
return errors.New("no content element of category")
|
||||||
} else {
|
} else {
|
||||||
|
@ -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")
|
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 == "" {
|
if p.Text == "" {
|
||||||
return errors.New("text element of plain text empty")
|
return errors.New("text element of plain text empty")
|
||||||
}
|
}
|
||||||
|
2
text.go
2
text.go
@ -15,7 +15,7 @@ func NewText(textType, content string) (Text, error) {
|
|||||||
case "text", "":
|
case "text", "":
|
||||||
return &PlainText{Type: textType, Text: content}, nil
|
return &PlainText{Type: textType, Text: content}, nil
|
||||||
case "html":
|
case "html":
|
||||||
return &PlainText{Type: textType, Text: html.EscapeString(content)}, nil
|
return &PlainText{Type: textType, Text: html.UnescapeString(content)}, nil
|
||||||
case "xhtml":
|
case "xhtml":
|
||||||
return &XHTMLText{
|
return &XHTMLText{
|
||||||
Type: textType,
|
Type: textType,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user