Skip to content

Commit

Permalink
Disable strict XML parsing by default #1155
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Mar 28, 2022
1 parent 3a1e2c7 commit bbeae22
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 14 deletions.
21 changes: 21 additions & 0 deletions acceptance_tests/inputs-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ EOM
assertEquals "$expected" "$X"
}


testInputXmlStrict() {
cat >test.yml <<EOL
<?xml version="1.0"?>
<!DOCTYPE root [
<!ENTITY writer "Catherine.">
<!ENTITY copyright "(r) Great">
]>
<root>
<item>&writer;&copyright;</item>
</root>
EOL

X=$(./yq -p=xml --xml-strict-mode test.yml 2>&1)
assertEquals 1 $?
assertEquals "Error: bad file 'test.yml': XML syntax error on line 7: invalid character entity &writer;" "$X"

X=$(./yq ea -p=xml --xml-strict-mode test.yml 2>&1)
assertEquals "Error: bad file 'test.yml': XML syntax error on line 7: invalid character entity &writer;" "$X"
}

testInputXmlGithubAction() {
cat >test.yml <<EOL
<cat legs="4">BiBi</cat>
Expand Down
1 change: 1 addition & 0 deletions cmd/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var inputFormat = "yaml"

var xmlAttributePrefix = "+"
var xmlContentName = "+content"
var xmlStrictMode = false

var exitStatus = false
var forceColor = false
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ yq -P sample.json
yqlib.InitExpressionParser()
yqlib.XMLPreferences.AttributePrefix = xmlAttributePrefix
yqlib.XMLPreferences.ContentName = xmlContentName
yqlib.XMLPreferences.StrictMode = xmlStrictMode
},
}

Expand All @@ -70,6 +71,7 @@ yq -P sample.json

rootCmd.PersistentFlags().StringVar(&xmlAttributePrefix, "xml-attribute-prefix", "+", "prefix for xml attributes")
rootCmd.PersistentFlags().StringVar(&xmlContentName, "xml-content-name", "+content", "name for xml content (if no attribute name is present).")
rootCmd.PersistentFlags().BoolVar(&xmlStrictMode, "xml-strict-mode", false, "enables strict parsing of XML. See https://pkg.go.dev/encoding/xml for more details.")

rootCmd.PersistentFlags().BoolVarP(&nullInput, "null-input", "n", false, "Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.")
rootCmd.PersistentFlags().BoolVarP(&noDocSeparators, "no-doc", "N", false, "Don't print document separators (---)")
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func configureDecoder() (yqlib.Decoder, error) {
}
switch yqlibInputFormat {
case yqlib.XMLInputFormat:
return yqlib.NewXMLDecoder(xmlAttributePrefix, xmlContentName), nil
return yqlib.NewXMLDecoder(xmlAttributePrefix, xmlContentName, xmlStrictMode), nil
case yqlib.PropertiesInputFormat:
return yqlib.NewPropertiesDecoder(), nil
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/yqlib/decoder_xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package yqlib

import (
"encoding/xml"
"errors"
"io"
"strings"
"unicode"
Expand All @@ -14,14 +15,15 @@ type xmlDecoder struct {
reader io.Reader
attributePrefix string
contentName string
strictMode bool
finished bool
}

func NewXMLDecoder(attributePrefix string, contentName string) Decoder {
func NewXMLDecoder(attributePrefix string, contentName string, strictMode bool) Decoder {
if contentName == "" {
contentName = "content"
}
return &xmlDecoder{attributePrefix: attributePrefix, contentName: contentName, finished: false}
return &xmlDecoder{attributePrefix: attributePrefix, contentName: contentName, finished: false, strictMode: strictMode}
}

func (dec *xmlDecoder) Init(reader io.Reader) {
Expand Down Expand Up @@ -189,7 +191,7 @@ type element struct {
// of the map keys.
func (dec *xmlDecoder) decodeXML(root *xmlNode) error {
xmlDec := xml.NewDecoder(dec.reader)

xmlDec.Strict = dec.strictMode
// That will convert the charset if the provided XML is non-UTF-8
xmlDec.CharsetReader = charset.NewReaderLabel

Expand All @@ -201,7 +203,7 @@ func (dec *xmlDecoder) decodeXML(root *xmlNode) error {

for {
t, e := xmlDec.Token()
if e != nil {
if e != nil && !errors.Is(e, io.EOF) {
return e
}
if t == nil {
Expand Down
25 changes: 25 additions & 0 deletions pkg/yqlib/doc/usage/xml.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,31 @@ cat:
+legs: "4"
```

## Parse xml: custom dtd
DTD entities are ignored.

Given a sample.xml file of:
```xml
<?xml version="1.0"?>
<!DOCTYPE root [
<!ENTITY writer "Blah.">
<!ENTITY copyright "Blah">
]>
<root>
<item>&writer;&copyright;</item>
</root>
```
then
```bash
yq -p=xml '.' sample.xml
```
will output
```yaml
root:
item: '&writer;&copyright;'
```

## Parse xml: with comments
A best attempt is made to preserve comments.

Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/encoder_xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
yaml "gopkg.in/yaml.v3"
)

var XMLPreferences = xmlPreferences{AttributePrefix: "+", ContentName: "+content"}
var XMLPreferences = xmlPreferences{AttributePrefix: "+", ContentName: "+content", StrictMode: false}

type xmlEncoder struct {
attributePrefix string
Expand Down
6 changes: 3 additions & 3 deletions pkg/yqlib/expression_tokeniser.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@ func initLexer() (*lex.Lexer, error) {

lexer.Add([]byte(`load`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewYamlDecoder()}))

lexer.Add([]byte(`xmlload`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName)}))
lexer.Add([]byte(`load_xml`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName)}))
lexer.Add([]byte(`loadxml`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName)}))
lexer.Add([]byte(`xmlload`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName, XMLPreferences.StrictMode)}))
lexer.Add([]byte(`load_xml`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName, XMLPreferences.StrictMode)}))
lexer.Add([]byte(`loadxml`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName, XMLPreferences.StrictMode)}))

lexer.Add([]byte(`load_base64`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewBase64Decoder()}))

Expand Down
1 change: 1 addition & 0 deletions pkg/yqlib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func InitExpressionParser() {
type xmlPreferences struct {
AttributePrefix string
ContentName string
StrictMode bool
}

var log = logging.MustGetLogger("yq-lib")
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_encoder_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
case YamlInputFormat:
decoder = NewYamlDecoder()
case XMLInputFormat:
decoder = NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName)
decoder = NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName, XMLPreferences.StrictMode)
case Base64InputFormat:
decoder = NewBase64Decoder()
}
Expand Down
28 changes: 24 additions & 4 deletions pkg/yqlib/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ var expectedXMLWithComments = `<!-- above_cat inline_cat --><cat><!-- above_arra
</cat><!-- below_cat -->
`

var xmlWithCustomDtd = `
<?xml version="1.0"?>
<!DOCTYPE root [
<!ENTITY writer "Blah.">
<!ENTITY copyright "Blah">
]>
<root>
<item>&writer;&copyright;</item>
</root>`

var expectedDtd = `root:
item: '&writer;&copyright;'
`

var xmlScenarios = []formatScenario{
{
description: "Parse xml: simple",
Expand Down Expand Up @@ -185,6 +199,12 @@ var xmlScenarios = []formatScenario{
input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat legs=\"4\">meow</cat>",
expected: "cat:\n +content: meow\n +legs: \"4\"\n",
},
{
description: "Parse xml: custom dtd",
subdescription: "DTD entities are ignored.",
input: xmlWithCustomDtd,
expected: expectedDtd,
},
{
description: "Parse xml: with comments",
subdescription: "A best attempt is made to preserve comments.",
Expand Down Expand Up @@ -286,9 +306,9 @@ func testXMLScenario(t *testing.T, s formatScenario) {
if s.scenarioType == "encode" {
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewXMLEncoder(2, "+", "+content")), s.description)
} else if s.scenarioType == "roundtrip" {
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content"), NewXMLEncoder(2, "+", "+content")), s.description)
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content", false), NewXMLEncoder(2, "+", "+content")), s.description)
} else {
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content"), NewYamlEncoder(4, false, true, true)), s.description)
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content", false), NewYamlEncoder(4, false, true, true)), s.description)
}
}

Expand Down Expand Up @@ -327,7 +347,7 @@ func documentXMLDecodeScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=xml '%v' sample.xml\n```\n", expression))
writeOrPanic(w, "will output\n")

writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder("+", "+content"), NewYamlEncoder(2, false, true, true))))
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder("+", "+content", false), NewYamlEncoder(2, false, true, true))))
}

func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) {
Expand Down Expand Up @@ -363,7 +383,7 @@ func documentXMLRoundTripScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, "```bash\nyq -p=xml -o=xml '.' sample.xml\n```\n")
writeOrPanic(w, "will output\n")

writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder("+", "+content"), NewXMLEncoder(2, "+", "+content"))))
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder("+", "+content", false), NewXMLEncoder(2, "+", "+content"))))
}

func TestXMLScenarios(t *testing.T) {
Expand Down

0 comments on commit bbeae22

Please sign in to comment.