]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: fix reserved namespace check to be case-insensitive
authorTamás Gulácsi <tgulacsi78@gmail.com>
Fri, 25 Oct 2019 11:48:02 +0000 (13:48 +0200)
committerEmmanuel Odeke <emm.odeke@gmail.com>
Thu, 9 Apr 2020 02:03:40 +0000 (02:03 +0000)
Fixes the check for the reserved namespace prefix
"xml" to be case insensitive, so as to match all variants of:

    (('X'|'x')('M'|'m')('L'|'l'))

as mandated by Section 2.3 of https://www.w3.org/TR/REC-xml/

Fixes #35151.

Change-Id: Id5a98e5f9d69d3741dc16f567c4320f1ad0b3c70
Reviewed-on: https://go-review.googlesource.com/c/go/+/203417
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/encoding/xml/marshal.go
src/encoding/xml/marshal_test.go

index e325f3137438ff6227b6399d0c3097959bf588f3..2440a51e205b70b0771366533e01619baae7f909 100644 (file)
@@ -345,8 +345,11 @@ func (p *printer) createAttrPrefix(url string) string {
        if prefix == "" || !isName([]byte(prefix)) || strings.Contains(prefix, ":") {
                prefix = "_"
        }
-       if strings.HasPrefix(prefix, "xml") {
-               // xmlanything is reserved.
+       // xmlanything is reserved and any variant of it regardless of
+       // case should be matched, so:
+       //    (('X'|'x') ('M'|'m') ('L'|'l'))
+       // See Section 2.3 of https://www.w3.org/TR/REC-xml/
+       if len(prefix) >= 3 && strings.EqualFold(prefix[:3], "xml") {
                prefix = "_" + prefix
        }
        if p.attrNS[prefix] != "" {
index 8473158bcd031e06a55ea79ccdf3eedb5b258de4..6085ddbba2300d22305686cf2960248d6a023e70 100644 (file)
@@ -2266,6 +2266,30 @@ var encodeTokenTests = []struct {
                }},
        },
        want: `<foo xmlns="space"><bar xmlns="space" xmlns:space="space" space:attr="value">`,
+}, {
+       desc: "reserved namespace prefix -- all lower case",
+       toks: []Token{
+               StartElement{Name{"", "foo"}, []Attr{
+                       {Name{"http://www.w3.org/2001/xmlSchema-instance", "nil"}, "true"},
+               }},
+       },
+       want: `<foo xmlns:_xmlSchema-instance="http://www.w3.org/2001/xmlSchema-instance" _xmlSchema-instance:nil="true">`,
+}, {
+       desc: "reserved namespace prefix -- all upper case",
+       toks: []Token{
+               StartElement{Name{"", "foo"}, []Attr{
+                       {Name{"http://www.w3.org/2001/XMLSchema-instance", "nil"}, "true"},
+               }},
+       },
+       want: `<foo xmlns:_XMLSchema-instance="http://www.w3.org/2001/XMLSchema-instance" _XMLSchema-instance:nil="true">`,
+}, {
+       desc: "reserved namespace prefix -- all mixed case",
+       toks: []Token{
+               StartElement{Name{"", "foo"}, []Attr{
+                       {Name{"http://www.w3.org/2001/XmLSchema-instance", "nil"}, "true"},
+               }},
+       },
+       want: `<foo xmlns:_XmLSchema-instance="http://www.w3.org/2001/XmLSchema-instance" _XmLSchema-instance:nil="true">`,
 }}
 
 func TestEncodeToken(t *testing.T) {