]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: disallow empty namespace when prefix is set
authorConstantin Konstantinidis <constantinkonstantinidis@gmail.com>
Mon, 20 Aug 2018 18:29:30 +0000 (20:29 +0200)
committerGopher Robot <gobot@golang.org>
Wed, 9 Nov 2022 22:45:43 +0000 (22:45 +0000)
Non-regression tests are added.

Fixes #8068

Change-Id: Icb36c910bbf4955743b7aa8382002b2d9246fadc
Reviewed-on: https://go-review.googlesource.com/c/go/+/105636
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>

src/encoding/xml/xml.go
src/encoding/xml/xml_test.go

index d4509dfc851f55de9adf31237b2b375c870a8ec0..2e3232441553c160c46ad063cffaae081d7d339a 100644 (file)
@@ -302,6 +302,10 @@ func (d *Decoder) Token() (Token, error) {
                // the translations first.
                for _, a := range t1.Attr {
                        if a.Name.Space == xmlnsPrefix {
+                               if a.Value == "" {
+                                       d.err = d.syntaxError("empty namespace with prefix")
+                                       return nil, d.err
+                               }
                                v, ok := d.ns[a.Name.Local]
                                d.pushNs(a.Name.Local, v, ok)
                                d.ns[a.Name.Local] = a.Value
index e20dc781a1273bd1d30d8e6009fc4db0b18d6823..26c4a8a74bed15780144c6ece77a6d10c25384c0 100644 (file)
@@ -916,6 +916,35 @@ func TestIssue5880(t *testing.T) {
        }
 }
 
+func TestIssue8068(t *testing.T) {
+       emptyError := SyntaxError{}
+       noError := emptyError.Error()
+       testCases := []struct {
+               s       string
+               wantErr SyntaxError
+       }{
+               {`<foo xmlns:bar="a"></foo>`, SyntaxError{}},
+               {`<foo xmlns:bar=""></foo>`, SyntaxError{Msg: "empty namespace with prefix", Line: 1}},
+               {`<foo xmlns:="a"></foo>`, SyntaxError{}},
+               {`<foo xmlns:""></foo>`, SyntaxError{Msg: "attribute name without = in element", Line: 1}},
+               {`<foo xmlns:"a"></foo>`, SyntaxError{Msg: "attribute name without = in element", Line: 1}},
+       }
+       var dest string
+       for _, tc := range testCases {
+               if got, want := Unmarshal([]byte(tc.s), &dest), tc.wantErr.Error(); got == nil {
+                       if want != noError {
+                               t.Errorf("%q: got nil, want %s", tc.s, want)
+                       }
+               } else {
+                       if want == "" {
+                               t.Errorf("%q: got %s, want nil", tc.s, got)
+                       } else if got.Error() != want {
+                               t.Errorf("%q: got %s, want %s", tc.s, got, want)
+                       }
+               }
+       }
+}
+
 func TestIssue8535(t *testing.T) {
 
        type ExampleConflict struct {