Skip to content

Commit bef5eca

Browse files
iwdgogopherbot
authored andcommitted
encoding/xml: disallow empty namespace when prefix is set
Non-regression tests are added. Fixes #8068 Change-Id: Icb36c910bbf4955743b7aa8382002b2d9246fadc Reviewed-on: https://go-review.googlesource.com/c/go/+/105636 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent e70f74b commit bef5eca

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/encoding/xml/xml.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ func (d *Decoder) Token() (Token, error) {
302302
// the translations first.
303303
for _, a := range t1.Attr {
304304
if a.Name.Space == xmlnsPrefix {
305+
if a.Value == "" {
306+
d.err = d.syntaxError("empty namespace with prefix")
307+
return nil, d.err
308+
}
305309
v, ok := d.ns[a.Name.Local]
306310
d.pushNs(a.Name.Local, v, ok)
307311
d.ns[a.Name.Local] = a.Value

src/encoding/xml/xml_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,35 @@ func TestIssue5880(t *testing.T) {
916916
}
917917
}
918918

919+
func TestIssue8068(t *testing.T) {
920+
emptyError := SyntaxError{}
921+
noError := emptyError.Error()
922+
testCases := []struct {
923+
s string
924+
wantErr SyntaxError
925+
}{
926+
{`<foo xmlns:bar="a"></foo>`, SyntaxError{}},
927+
{`<foo xmlns:bar=""></foo>`, SyntaxError{Msg: "empty namespace with prefix", Line: 1}},
928+
{`<foo xmlns:="a"></foo>`, SyntaxError{}},
929+
{`<foo xmlns:""></foo>`, SyntaxError{Msg: "attribute name without = in element", Line: 1}},
930+
{`<foo xmlns:"a"></foo>`, SyntaxError{Msg: "attribute name without = in element", Line: 1}},
931+
}
932+
var dest string
933+
for _, tc := range testCases {
934+
if got, want := Unmarshal([]byte(tc.s), &dest), tc.wantErr.Error(); got == nil {
935+
if want != noError {
936+
t.Errorf("%q: got nil, want %s", tc.s, want)
937+
}
938+
} else {
939+
if want == "" {
940+
t.Errorf("%q: got %s, want nil", tc.s, got)
941+
} else if got.Error() != want {
942+
t.Errorf("%q: got %s, want %s", tc.s, got, want)
943+
}
944+
}
945+
}
946+
}
947+
919948
func TestIssue8535(t *testing.T) {
920949

921950
type ExampleConflict struct {

0 commit comments

Comments
 (0)