Skip to content

Commit c585c3f

Browse files
author
root
committed
test: add XML parsing security tests
1 parent f0d1e54 commit c585c3f

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

config/config_xml_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
)
6+
7+
type TestConfig struct {
8+
Name string `xml:"name"`
9+
Port int `xml:"port"`
10+
}
11+
12+
func TestUnmarshalXML(t *testing.T) {
13+
// Test normal XML parsing
14+
xmlData := []byte(`<config><name>test</name><port>8080</port></config>`)
15+
var config TestConfig
16+
err := UnmarshalXML(xmlData, &config)
17+
if err != nil {
18+
t.Errorf("UnmarshalXML failed: %v", err)
19+
}
20+
if config.Name != "test" {
21+
t.Errorf("Expected name 'test', got '%s'", config.Name)
22+
}
23+
if config.Port != 8080 {
24+
t.Errorf("Expected port 8080, got %d", config.Port)
25+
}
26+
27+
// Test empty XML
28+
var emptyConfig TestConfig
29+
err = UnmarshalXML([]byte(""), &emptyConfig)
30+
if err != nil {
31+
t.Logf("Empty XML returns error: %v", err)
32+
}
33+
34+
// Test malformed XML (should not cause security issues)
35+
malformedXML := []byte("<config><name>test</name>")
36+
var malformed TestConfig
37+
err = UnmarshalXML(malformedXML, &malformed)
38+
if err == nil {
39+
t.Log("Malformed XML parsed without error")
40+
}
41+
}
42+
43+
func TestUnmarshalXMLSecurity(t *testing.T) {
44+
// Test XXE attack payload - should be safely handled
45+
xxePayload := `<?xml version="1.0" encoding="UTF-8"?>
46+
<!DOCTYPE config [
47+
<!ENTITY xxe SYSTEM "file:///etc/passwd">
48+
]>
49+
<config><name>&xxe;</name></config>`
50+
51+
var config TestConfig
52+
err := UnmarshalXML([]byte(xxePayload), &config)
53+
if err != nil {
54+
t.Logf("XXE payload rejected: %v", err)
55+
} else {
56+
// If parsed, should not contain external entity content
57+
t.Logf("XXE payload parsed, name: %s", config.Name)
58+
}
59+
60+
// Test billion laughs attack - should not cause DoS
61+
billionLaughs := `<?xml version="1.0"?><!ENTITY lol "lol"><!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;"><!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;"><config><name>&lol3;</name></config>`
62+
63+
var config2 TestConfig
64+
err = UnmarshalXML([]byte(billionLaughs), &config2)
65+
if err != nil {
66+
t.Logf("Billion laughs attack rejected: %v", err)
67+
} else {
68+
t.Logf("Billion laughs parsed, name length: %d", len(config2.Name))
69+
}
70+
}
71+
72+
func TestMarshalXML(t *testing.T) {
73+
config := TestConfig{Name: "test", Port: 8080}
74+
data, err := MarshalXML(config)
75+
if err != nil {
76+
t.Errorf("MarshalXML failed: %v", err)
77+
}
78+
if len(data) == 0 {
79+
t.Error("MarshalXML returned empty data")
80+
}
81+
}
82+
83+
func TestMarshalXMLString(t *testing.T) {
84+
config := TestConfig{Name: "test", Port: 8080}
85+
str := MarshalXMLString(config)
86+
if str == "" {
87+
t.Error("MarshalXMLString returned empty string")
88+
}
89+
}

0 commit comments

Comments
 (0)