Skip to content

Commit

Permalink
encoding/xml: (*Decoder).nsname now strictly parses namespaces
Browse files Browse the repository at this point in the history
encoding/xml: disable 3/4 tests for golang#43168

I’m not sure what the right fix is for this. Malformed namespaces
(leading or trailing colons, more than 1 colon) should result in an error.
  • Loading branch information
ydnar committed Oct 14, 2021
1 parent ac54bfc commit d3ba102
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/encoding/xml/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,17 +1182,18 @@ func (d *Decoder) nsname() (name Name, ok bool) {
if !ok {
return
}
if strings.Count(s, ":") > 1 {
n := strings.Count(s, ":")
if n == 0 { // No colons, no namespace. OK.
name.Local = s
} else if i := strings.Index(s, ":"); i < 1 || i > len(s)-2 {
} else if n > 1 { // More than one colon, not OK.
name.Local = s
return name, false
} else if i := strings.Index(s, ":"); i < 1 || i > len(s)-2 { // Leading or trailing colon, not OK.
name.Local = s
return name, false
} else {
name.Space = s[0:i]
if strings.Contains(s[i+1:], ":") {
return name, false
} else {
name.Local = s[i+1:]
}
name.Local = s[i+1:]
}
return name, true
}
Expand Down
8 changes: 5 additions & 3 deletions src/encoding/xml/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1755,9 +1755,11 @@ func testRoundTrip(t *testing.T, input string) {

func TestRoundTrip(t *testing.T) {
tests := map[string]string{
"leading colon": `<::Test ::foo="bar"><:::Hello></:::Hello><Hello></Hello></::Test>`,
"trailing colon": `<foo abc:="x"></foo>`,
"double colon": `<x:y:foo></x:y:foo>`,
// Disabling these tests because the parser now treats malformed namespaces as an error.
// See https://github.com/golang/go/issues/43168.
// "leading colon": `<::Test ::foo="bar"><:::Hello></:::Hello><Hello></Hello></::Test>`,
// "trailing colon": `<foo abc:="x"></foo>`,
// "double colon": `<x:y:foo></x:y:foo>`,
"comments in directives": `<!ENTITY x<!<!-- c1 [ " -->--x --> > <e></e> <!DOCTYPE xxx [ x<!-- c2 " -->--x ]>`,
}
for name, input := range tests {
Expand Down

0 comments on commit d3ba102

Please sign in to comment.