Skip to content

encoding/xml: prevent multiple XMLNS attributes #42808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/encoding/xml/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ func (p *printer) writeStart(start *StartElement) error {
// Attributes
for _, attr := range start.Attr {
name := attr.Name
if name.Local == "" {
if name.Local == "" || (start.Name.Space != "" && name.Local == "xmlns" && name.Space == "") {
continue
}
p.WriteByte(' ')
Expand Down
32 changes: 25 additions & 7 deletions src/encoding/xml/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,20 @@ func (m *MyMarshalerTest) MarshalXML(e *Encoder, start StartElement) error {
return nil
}

type MarshalMultipleXMLNS struct{}

func (MarshalMultipleXMLNS) MarshalXML(e *Encoder, _ StartElement) error {
start := StartElement{
Name: Name{Space: "space", Local: "MarshalMultipleXMLNS"},
Attr: []Attr{{Name: Name{Local: "xmlns"}, Value: "attrSpace"}},
}
err := e.EncodeToken(start)
if err != nil {
return err
}
return e.EncodeToken(start.End())
}

type MyMarshalerAttrTest struct {
}

Expand Down Expand Up @@ -1651,6 +1665,10 @@ var marshalTests = []struct {
Value: &DirectAny{Any: string("")},
UnmarshalOnly: true,
},
{
ExpectXML: `<MarshalMultipleXMLNS xmlns="space"></MarshalMultipleXMLNS>`,
Value: &MarshalMultipleXMLNS{},
},
}

func TestMarshal(t *testing.T) {
Expand Down Expand Up @@ -2144,7 +2162,7 @@ var encodeTokenTests = []struct {
{Name{"space", "a"}, "value"},
}},
},
want: `<foo xmlns="space" xmlns="space"><foo xmlns="space" xmlns:_xmlns="xmlns" _xmlns:y="space" xmlns:space="space" space:a="value">`,
Copy link
Member Author

@SamWhited SamWhited Nov 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a little concerned that this was intentional behavior when I saw that existing tests had this, but couldn't see why this would be the case so I went ahead and "fixed" them hoping this was just an oversight while copy/pasting some output for these individual tests. If this is not so, please advise.

want: `<foo xmlns="space"><foo xmlns="space" xmlns:_xmlns="xmlns" _xmlns:y="space" xmlns:space="space" space:a="value">`,
}, {
desc: "nested element defines default name space with existing prefix",
toks: []Token{
Expand All @@ -2156,7 +2174,7 @@ var encodeTokenTests = []struct {
{Name{"space", "a"}, "value"},
}},
},
want: `<foo xmlns:_xmlns="xmlns" _xmlns:x="space"><foo xmlns="space" xmlns="space" xmlns:space="space" space:a="value">`,
want: `<foo xmlns:_xmlns="xmlns" _xmlns:x="space"><foo xmlns="space" xmlns:space="space" space:a="value">`,
}, {
desc: "nested element uses empty attribute name space when default ns defined",
toks: []Token{
Expand All @@ -2167,7 +2185,7 @@ var encodeTokenTests = []struct {
{Name{"", "attr"}, "value"},
}},
},
want: `<foo xmlns="space" xmlns="space"><foo xmlns="space" attr="value">`,
want: `<foo xmlns="space"><foo xmlns="space" attr="value">`,
}, {
desc: "redefine xmlns",
toks: []Token{
Expand Down Expand Up @@ -2228,7 +2246,7 @@ var encodeTokenTests = []struct {
{Name{"space", "x"}, "value"},
}},
},
want: `<foo xmlns="space" xmlns="space"><foo xmlns="" x="value" xmlns:space="space" space:x="value">`,
want: `<foo xmlns="space"><foo xmlns="" x="value" xmlns:space="space" space:x="value">`,
}, {
desc: "nested element requires empty default name space",
toks: []Token{
Expand All @@ -2237,7 +2255,7 @@ var encodeTokenTests = []struct {
}},
StartElement{Name{"", "foo"}, nil},
},
want: `<foo xmlns="space" xmlns="space"><foo>`,
want: `<foo xmlns="space"><foo>`,
}, {
desc: "attribute uses name space from xmlns",
toks: []Token{
Expand All @@ -2259,7 +2277,7 @@ var encodeTokenTests = []struct {
EndElement{Name{"space", "baz"}},
EndElement{Name{"space", "foo"}},
},
want: `<foo xmlns="space" xmlns="space" xmlns:_xmlns="xmlns" _xmlns:bar="space" xmlns:space="space" space:baz="foo"><baz xmlns="space"></baz></foo>`,
want: `<foo xmlns="space" xmlns:_xmlns="xmlns" _xmlns:bar="space" xmlns:space="space" space:baz="foo"><baz xmlns="space"></baz></foo>`,
}, {
desc: "default name space not used by attributes, not explicitly defined",
toks: []Token{
Expand All @@ -2271,7 +2289,7 @@ var encodeTokenTests = []struct {
EndElement{Name{"space", "baz"}},
EndElement{Name{"space", "foo"}},
},
want: `<foo xmlns="space" xmlns="space" xmlns:space="space" space:baz="foo"><baz xmlns="space"></baz></foo>`,
want: `<foo xmlns="space" xmlns:space="space" space:baz="foo"><baz xmlns="space"></baz></foo>`,
}, {
desc: "impossible xmlns declaration",
toks: []Token{
Expand Down