Force undefined content of category and link to be empty or valid xml

This commit is contained in:
Jason Streifling 2024-10-17 18:39:57 +02:00
parent 73624eadd8
commit 5a82f1799f
3 changed files with 23 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package atom package atom
import ( import (
"encoding/xml"
"fmt" "fmt"
"mime" "mime"
"regexp" "regexp"
@ -86,3 +87,9 @@ func isValidLanguageTag(tag LanguageTag) bool {
func NewURN() IRI { func NewURN() IRI {
return IRI(fmt.Sprint("urn:uuid:", uuid.New())) return IRI(fmt.Sprint("urn:uuid:", uuid.New()))
} }
// isValidXML checks whether a string is valid XML. It returns a bool.
func isValidXML(input string) bool {
var v interface{}
return xml.Unmarshal([]byte(input), &v) == nil
}

View File

@ -16,6 +16,10 @@ type Category struct {
// NewCategory creates a new Category. It returns a *Category and an error. // NewCategory creates a new Category. It returns a *Category and an error.
func NewCategory(term, content string) (*Category, error) { func NewCategory(term, content string) (*Category, error) {
if !isValidXML(content) {
return nil, fmt.Errorf("%v not valid XML", content)
}
return &Category{Term: term, Content: content}, nil return &Category{Term: term, Content: content}, nil
} }
@ -41,8 +45,10 @@ func (c *Category) Check() error {
return fmt.Errorf("label attribute %v of category not correctly escaped", c.Label) return fmt.Errorf("label attribute %v of category not correctly escaped", c.Label)
} }
if c.Content == "" { if c.Content != "" {
return errors.New("content element of category empty") if !isValidXML(c.Content) {
return fmt.Errorf("content element %v of category not valid XML", c.Content)
}
} }
return nil return nil

10
link.go
View File

@ -19,6 +19,10 @@ type Link struct {
// NewLink creates a new Link. It returns a *Link and an error. // NewLink creates a new Link. It returns a *Link and an error.
func NewLink(href, content string) (*Link, error) { func NewLink(href, content string) (*Link, error) {
if !isValidXML(content) {
return nil, fmt.Errorf("%v not valid XML", content)
}
return &Link{Href: IRI(href), Content: content}, nil return &Link{Href: IRI(href), Content: content}, nil
} }
@ -51,8 +55,10 @@ func (l *Link) Check() error {
} }
} }
if l.Content == "" { if l.Content != "" {
return errors.New("content element of link empty") if !isValidXML(l.Content) {
return fmt.Errorf("content element %v of link not valid XML", l.Content)
}
} }
return nil return nil