Lifecycle: Accept empty <Filter> tag in XML documents (#12039)

Follow S3 to accept an empty filter tag inside an XML document.

<Filter> needs to be specified but it doesn't have to contain any other
XML tags inside it.
This commit is contained in:
Anis Elleuch 2021-04-12 17:36:15 +01:00 committed by GitHub
parent 8ab111cfb6
commit f1bc857f66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View File

@ -28,6 +28,7 @@ var (
// Filter - a filter for a lifecycle configuration Rule.
type Filter struct {
XMLName xml.Name `xml:"Filter"`
set bool
Prefix Prefix
@ -68,6 +69,7 @@ func (f Filter) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
// UnmarshalXML - decodes XML data.
func (f *Filter) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
f.set = true
for {
// Read tokens from the XML document in a stream.
t, err := d.Token()
@ -111,12 +113,12 @@ func (f *Filter) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error
// IsEmpty returns true if Filter is not specified in the XML
func (f Filter) IsEmpty() bool {
return !f.Prefix.set && !f.andSet && !f.tagSet
return !f.set
}
// Validate - validates the filter element
func (f Filter) Validate() error {
if !f.Prefix.set && !f.andSet && !f.tagSet {
if f.IsEmpty() {
return errXMLNotWellFormed
}
// A Filter must have exactly one of Prefix, Tag, or And specified.

View File

@ -86,12 +86,18 @@ func TestParseAndValidateLifecycleConfig(t *testing.T) {
expectedParsingErr: nil,
expectedValidationErr: errXMLNotWellFormed,
},
// Legitimate lifecycle
// Lifecycle with the deprecated Prefix tag
{
inputConfig: `<LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Rule><ID>rule</ID><Prefix /><Status>Enabled</Status><Expiration><Days>1</Days></Expiration></Rule></LifecycleConfiguration>`,
expectedParsingErr: nil,
expectedValidationErr: nil,
},
// Lifecycle with empty Filter tag
{
inputConfig: `<LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Rule><ID>rule</ID><Filter></Filter><Status>Enabled</Status><Expiration><Days>1</Days></Expiration></Rule></LifecycleConfiguration>`,
expectedParsingErr: nil,
expectedValidationErr: nil,
},
}
for i, tc := range testCases {