]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: fix reserved namespace check to be case-insensitive
authorIan Lance Taylor <iant@golang.org>
Tue, 20 Oct 2020 22:19:36 +0000 (15:19 -0700)
committerIan Lance Taylor <iant@golang.org>
Wed, 21 Oct 2020 23:51:30 +0000 (23:51 +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/

This is a roll forward of CL 203417, which was rolled back by CL 240179.
We've decided that the roll back was incorrect, and any broken tests
should be fixed.

The original CL 203417 was by Tamás Gulácsi.

Fixes #35151
For #39876

Change-Id: I2e6daa7aeb252531fba0b8a56086613e13059528
Reviewed-on: https://go-review.googlesource.com/c/go/+/264024
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
doc/go1.16.html
src/encoding/xml/marshal.go
src/encoding/xml/marshal_test.go

index b6df0487ca96b8a0c9d51923b4b30a43c338773f..1239217eeda98575b26a6379c9f084b0c5640afa 100644 (file)
@@ -302,6 +302,20 @@ Do not send CLs removing the interior tags from such phrases.
   </dd>
 </dl><!-- crypto/x509 -->
 
+<dl id="encoding/xml"><dt><a href="/pkg/encoding/xml/">encoding/xml</a></dt>
+  <dd>
+    <p><!-- CL 264024 -->
+      The encoder has always taken care to avoid using namespace prefixes
+      beginning with <code>xml</code>, which are reserved by the XML
+      specification.
+      Now, following the specification more closely, that check is
+      case-insensitive, so that prefixes beginning
+      with <code>XML</code>, <code>XmL</code>, and so on are also
+      avoided.
+    </p>
+  </dd>
+</dl><!-- encoding/xml -->
+
 <dl id="net/http"><dt><a href="/pkg/net/http/">net/http</a></dt>
   <dd>
     <p><!-- CL 233637 -->
index 0554b0d204e40948309643204405935f0f661a81..d8a04a95a2520fa39b8028dd97f43bef70804aba 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 31309ef2ca8c5a6895e3d6e728ff48e38280bada..d2e5137afd7c05a7ad0ad1414e227f168ee4bf54 100644 (file)
@@ -2283,6 +2283,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) {