Skip to content

Commit 81ac3ff

Browse files
committed
encoding/xml: (*Decoder).nsname now strictly parses namespaces
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.
1 parent 7731cb6 commit 81ac3ff

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/encoding/xml/xml.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -1186,17 +1186,18 @@ func (d *Decoder) nsname() (name Name, ok bool) {
11861186
if !ok {
11871187
return
11881188
}
1189-
if strings.Count(s, ":") > 1 {
1189+
n := strings.Count(s, ":")
1190+
if n == 0 { // No colons, no namespace. OK.
11901191
name.Local = s
1191-
} else if i := strings.Index(s, ":"); i < 1 || i > len(s)-2 {
1192+
} else if n > 1 { // More than one colon, not OK.
11921193
name.Local = s
1194+
return name, false
1195+
} else if i := strings.Index(s, ":"); i < 1 || i > len(s)-2 { // Leading or trailing colon, not OK.
1196+
name.Local = s
1197+
return name, false
11931198
} else {
11941199
name.Space = s[0:i]
1195-
if strings.Contains(s[i+1:], ":") {
1196-
return name, false
1197-
} else {
1198-
name.Local = s[i+1:]
1199-
}
1200+
name.Local = s[i+1:]
12001201
}
12011202
return name, true
12021203
}

src/encoding/xml/xml_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1755,9 +1755,11 @@ func testRoundTrip(t *testing.T, input string) {
17551755

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

0 commit comments

Comments
 (0)