aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/xml/xml.go
diff options
context:
space:
mode:
authorGiulio Iotti <dullgiulio@gmail.com>2015-04-15 16:59:49 +0300
committerRuss Cox <rsc@golang.org>2015-06-18 18:06:01 +0000
commit9490fbf7557f3d1d12051f79d033e2a4fd8fcbcb (patch)
treefb1aadea9c652530fdd553940ac3f3dff3772d6b /src/encoding/xml/xml.go
parenta13606e6196e0c4ef3f54542f9ae6fade0a9c19b (diff)
downloadgo-9490fbf7557f3d1d12051f79d033e2a4fd8fcbcb.tar.xz
xml: add check of version in document declaration
Check that if a version is declared, for example in '<?xml version="XX" ?>', version must be '1.0'. Change-Id: I16ba9f78873a5f31977dcf75ac8e671fe6c08280 Reviewed-on: https://go-review.googlesource.com/8961 Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/encoding/xml/xml.go')
-rw-r--r--src/encoding/xml/xml.go18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/encoding/xml/xml.go b/src/encoding/xml/xml.go
index 00792c4f27..3090750c48 100644
--- a/src/encoding/xml/xml.go
+++ b/src/encoding/xml/xml.go
@@ -576,7 +576,6 @@ func (d *Decoder) rawToken() (Token, error) {
case '?':
// <?: Processing instruction.
- // TODO(rsc): Should parse the <?xml declaration to make sure the version is 1.0.
var target string
if target, ok = d.name(); !ok {
if d.err == nil {
@@ -601,7 +600,13 @@ func (d *Decoder) rawToken() (Token, error) {
data = data[0 : len(data)-2] // chop ?>
if target == "xml" {
- enc := procInstEncoding(string(data))
+ content := string(data)
+ ver := procInst("version", content)
+ if ver != "" && ver != "1.0" {
+ d.err = fmt.Errorf("xml: unsupported version %q; only version 1.0 is supported", ver)
+ return nil, d.err
+ }
+ enc := procInst("encoding", content)
if enc != "" && enc != "utf-8" && enc != "UTF-8" {
if d.CharsetReader == nil {
d.err = fmt.Errorf("xml: encoding %q declared but Decoder.CharsetReader is nil", enc)
@@ -1962,16 +1967,17 @@ func Escape(w io.Writer, s []byte) {
EscapeText(w, s)
}
-// procInstEncoding parses the `encoding="..."` or `encoding='...'`
+// procInst parses the `param="..."` or `param='...'`
// value out of the provided string, returning "" if not found.
-func procInstEncoding(s string) string {
+func procInst(param, s string) string {
// TODO: this parsing is somewhat lame and not exact.
// It works for all actual cases, though.
- idx := strings.Index(s, "encoding=")
+ param = param + "="
+ idx := strings.Index(s, param)
if idx == -1 {
return ""
}
- v := s[idx+len("encoding="):]
+ v := s[idx+len(param):]
if v == "" {
return ""
}