-
-
Notifications
You must be signed in to change notification settings - Fork 166
fix(xml): reject invalid XML element and attribute names #536
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,6 +260,76 @@ func TestXmlReader_Write(t *testing.T) { | |
| } | ||
| }) | ||
|
|
||
| t.Run("invalid element name", func(t *testing.T) { | ||
| w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) | ||
| if err != nil { | ||
| t.Fatalf("Unexpected error: %s", err) | ||
| } | ||
|
|
||
| toEncode := model.NewMapValue() | ||
| _ = toEncode.SetMapKey("<", model.NewStringValue("value")) | ||
| _, err = w.Write(toEncode) | ||
| if err == nil { | ||
| t.Fatal("Expected error for invalid XML element name, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), `"<"`) || !strings.Contains(err.Error(), "not a valid XML element name") { | ||
| t.Fatalf("Expected error to mention the offending key, got: %s", err) | ||
| } | ||
| }) | ||
|
Comment on lines
+263
to
+278
|
||
|
|
||
| t.Run("invalid element name ampersand", func(t *testing.T) { | ||
| w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) | ||
| if err != nil { | ||
| t.Fatalf("Unexpected error: %s", err) | ||
| } | ||
|
|
||
| toEncode := model.NewMapValue() | ||
| _ = toEncode.SetMapKey("&", model.NewStringValue("value")) | ||
| _, err = w.Write(toEncode) | ||
| if err == nil { | ||
| t.Fatal("Expected error for invalid XML element name, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), `"&"`) || !strings.Contains(err.Error(), "not a valid XML element name") { | ||
| t.Fatalf("Expected error to mention the offending key, got: %s", err) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("invalid element name with space", func(t *testing.T) { | ||
| w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) | ||
| if err != nil { | ||
| t.Fatalf("Unexpected error: %s", err) | ||
| } | ||
|
|
||
| toEncode := model.NewMapValue() | ||
| _ = toEncode.SetMapKey("foo bar", model.NewStringValue("value")) | ||
| _, err = w.Write(toEncode) | ||
| if err == nil { | ||
| t.Fatal("Expected error for invalid XML element name, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), `"foo bar"`) || !strings.Contains(err.Error(), "not a valid XML element name") { | ||
| t.Fatalf("Expected error to mention the offending key, got: %s", err) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("invalid attribute name", func(t *testing.T) { | ||
| w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) | ||
| if err != nil { | ||
| t.Fatalf("Unexpected error: %s", err) | ||
| } | ||
|
|
||
| toEncode := model.NewMapValue() | ||
| child := model.NewMapValue() | ||
| _ = child.SetMapKey("-<", model.NewStringValue("value")) | ||
| _ = toEncode.SetMapKey("foo", child) | ||
| _, err = w.Write(toEncode) | ||
| if err == nil { | ||
| t.Fatal("Expected error for invalid XML attribute name, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), "invalid XML attribute name") || !strings.Contains(err.Error(), `"-<"`) { | ||
| t.Fatalf("Expected error to mention the offending attribute key, got: %s", err) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("encode cdata", func(t *testing.T) { | ||
| w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) | ||
| if err != nil { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New behavior rejects invalid attribute names, but there isn’t a test that exercises this error path (e.g. a map containing an attribute key like "-<" or "-foo bar"). Adding an integration test for invalid attribute names would help prevent regressions and ensure the writer fails with a clear error when encountering invalid attribute keys.