Fixed base64/xml decode an empty string #1209

This commit is contained in:
Mike Farah 2022-05-27 11:18:38 +10:00
parent 8d32e6a82c
commit 294da55012
5 changed files with 34 additions and 5 deletions

View File

@ -11,6 +11,7 @@ import (
type base64Decoder struct {
reader io.Reader
finished bool
readAnything bool
encoding base64.Encoding
}
@ -20,6 +21,7 @@ func NewBase64Decoder() Decoder {
func (dec *base64Decoder) Init(reader io.Reader) {
dec.reader = reader
dec.readAnything = false
dec.finished = false
}
@ -35,7 +37,15 @@ func (dec *base64Decoder) Decode(rootYamlNode *yaml.Node) error {
}
if buf.Len() == 0 {
dec.finished = true
// if we've read _only_ an empty string, lets return that
// otherwise if we've already read some bytes, and now we get
// an empty string, then we are done.
if dec.readAnything {
return io.EOF
}
}
dec.readAnything = true
rootYamlNode.Kind = yaml.ScalarNode
rootYamlNode.Tag = "!!str"
rootYamlNode.Value = buf.String()

View File

@ -13,6 +13,7 @@ import (
type xmlDecoder struct {
reader io.Reader
readAnything bool
attributePrefix string
contentName string
strictMode bool
@ -28,6 +29,7 @@ func NewXMLDecoder(attributePrefix string, contentName string, strictMode bool)
func (dec *xmlDecoder) Init(reader io.Reader) {
dec.reader = reader
dec.readAnything = false
dec.finished = false
}
@ -137,8 +139,11 @@ func (dec *xmlDecoder) Decode(rootYamlNode *yaml.Node) error {
return err
} else if firstNode.Tag == "!!null" {
dec.finished = true
if dec.readAnything {
return io.EOF
}
}
dec.readAnything = true
rootYamlNode.Kind = yaml.DocumentNode
rootYamlNode.Content = []*yaml.Node{firstNode}
dec.finished = true

View File

@ -403,12 +403,18 @@ func initLexer() (*lex.Lexer, error) {
lexer.Add([]byte(`fromyaml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat}))
lexer.Add([]byte(`fromjson`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat}))
lexer.Add([]byte(`fromxml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: XMLInputFormat}))
lexer.Add([]byte(`fromprops`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: PropertiesInputFormat}))
lexer.Add([]byte(`from_yaml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat}))
lexer.Add([]byte(`from_json`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat}))
lexer.Add([]byte(`from_xml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: XMLInputFormat}))
lexer.Add([]byte(`from_props`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: PropertiesInputFormat}))
lexer.Add([]byte(`@yamld`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat}))
lexer.Add([]byte(`@jsond`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat}))
lexer.Add([]byte(`@xmld`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: XMLInputFormat}))
lexer.Add([]byte(`@propsd`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: PropertiesInputFormat}))
lexer.Add([]byte(`sortKeys`), opToken(sortKeysOpType))
lexer.Add([]byte(`sort_keys`), opToken(sortKeysOpType))

View File

@ -251,6 +251,14 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
"D0, P[], (!!str)::\n",
},
},
{
description: "empty xml decode",
skipDoc: true,
expression: `"" | @xmld`,
expected: []string{
"D0, P[], (!!null)::\n",
},
},
}
func TestEncoderDecoderOperatorScenarios(t *testing.T) {

View File

@ -216,7 +216,7 @@ var xmlScenarios = []formatScenario{
description: "Empty doc",
skipDoc: true,
input: "",
expected: "",
expected: "\n",
scenarioType: "decode",
},
{