Skip to content
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

encoding/xml: Require whitespace between attributes #69199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 20 additions & 6 deletions src/encoding/xml/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,13 +794,30 @@ func (d *Decoder) rawToken() (Token, error) {
}

attr = []Attr{}
for {
d.space()
Outer: for {
if b, ok = d.mustgetc(); !ok {
return nil, d.err
}
if b == '/' {
switch b {
case ' ', '\t', '\r', '\n':
// Skip subsequent spaces
d.space()
if b, ok = d.mustgetc(); !ok {
return nil, d.err
}
if b == '>' {
break Outer
}
empty = b == '/'
case '>':
break Outer
case '/':
empty = true
default:
d.err = d.syntaxError("expected whitespace, />, or > following element name or attribute value")
return nil, d.err
}
if empty {
if b, ok = d.mustgetc(); !ok {
return nil, d.err
}
Expand All @@ -810,9 +827,6 @@ func (d *Decoder) rawToken() (Token, error) {
}
break
}
if b == '>' {
break
}
d.ungetc(b)

a := Attr{}
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 @@ -265,6 +265,8 @@ var xmlInput = []string{
"<t a>",
"<t a=>",
"<t a=v>",
// Issue 68385
"<a b='c'c='d'/>",
// "<![CDATA[d]]>", // let the Token() caller handle
"<t></e>",
"<t></>",
Expand Down Expand Up @@ -1122,15 +1124,15 @@ func TestIssue7113(t *testing.T) {
}

func TestIssue20396(t *testing.T) {

var attrError = UnmarshalError("XML syntax error on line 1: expected attribute name in element")
var attrError = UnmarshalError("XML syntax error on line 1: expected whitespace, />, or > following element name or attribute value")

testCases := []struct {
s string
wantErr error
}{
{`<a:te:st xmlns:a="abcd"/>`, // Issue 20396
UnmarshalError("XML syntax error on line 1: expected element name after <")},
UnmarshalError("XML syntax error on line 1: colon after prefixed XML name a:te")},
{`<a test='d'xmlns:a="abcd"/>`, attrError},
{`<a:te=st xmlns:a="abcd"/>`, attrError},
{`<a:te&st xmlns:a="abcd"/>`, attrError},
{`<a:test xmlns:a="abcd"/>`, nil},
Expand Down