Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/xml: CDATA section allows invalid characters #53728

Open
firodj opened this issue Jul 7, 2022 · 4 comments
Open

encoding/xml: CDATA section allows invalid characters #53728

firodj opened this issue Jul 7, 2022 · 4 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@firodj
Copy link

firodj commented Jul 7, 2022

What version of Go are you using (go version)?

go version go1.16.15 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

A simple main.go program:

package main

import (
	"encoding/xml"
	"os"
)

func main() {
	type Person struct {
		Data    string `xml:",cdata"`
	}

	v := &Person{}
	v.Data = "\x1b[1;33mWARN\x1b[0m I dont know what to do"

	output, _ := xml.MarshalIndent(v, "  ", "    ")
	os.Stdout.Write(output)
}

Run and store the output:

$ go run main.go > output.xml

Try validate the xml:

$ xmllint output.xml

What did you expect to see?

encode error, or
no error with invalid character being replaced

What did you see instead?

encode success, but xmllint report parse error

Reference:

@heschi heschi added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 7, 2022
@heschi heschi added this to the Backlog milestone Jul 7, 2022
@heschi
Copy link
Contributor

heschi commented Jul 7, 2022

cc @rsc

@alexxed
Copy link

alexxed commented Apr 26, 2023

This is a general issue, not just with cdata:

package main

import (
	"encoding/xml"
	"fmt"
)

func main() {
	data := "\x1b[31m; some colour\x1b[0m;"
	fmt.Printf("original data: %s\n", data)
	out, _ := xml.MarshalIndent(data, "", "  ")
	fmt.Printf("data after Marshal: %s\n", string(out))
	xml.Unmarshal(out, &data)
	fmt.Printf("data after Unmarshal:%v\n", data)

}

produces a broken string losing the original value
image

@alexxed
Copy link

alexxed commented Apr 26, 2023

looks like this old commit did the breakage f74eb6d#diff-0526795219b1a2003e16773ef66844f384410205af46abac3edd309b22b88aeeR704-R705 as a fix to #4235

@alexxed
Copy link

alexxed commented Apr 26, 2023

my expectation would be that \x1b gets encoded to  in xml, then on unmarshal it gets back to \x1b maintaining the original value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants