Skip to content

Commit dc4b591

Browse files
committed
Merge pull request #40 from CybOXProject/cdata_commas
Wrap , entities with CDATA
2 parents 377cbea + 3c7a268 commit dc4b591

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

cybox/test/common/properties_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ def test_string(self):
2525
self.assertTrue(s.datatype, "String")
2626
self.assertTrue(s.value, "test_string")
2727

28+
def test_string_with_comma(self):
29+
s = String("test_string,")
30+
s2 = cybox.test.round_trip(s)
31+
self.assertEqual(s, s2)
32+
33+
def test_list_of_strings_with_comma(self):
34+
s = String([u"string,1", u"string,1", u"string,3"])
35+
s2 = cybox.test.round_trip(s)
36+
self.assertEqual(s, s2)
37+
2838
def test_integer(self):
2939
i = Integer(42)
3040
self.assertTrue(i.datatype, "Integer")

cybox/test/utils_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ def test_encode_decode_lists(self):
111111
c = ["A long", "long", "time ago"]
112112
d = "A long,long,time ago"
113113

114-
self.assertEqual(cybox.utils.normalize_to_xml(a), b)
114+
self.assertEqual(cybox.utils.normalize_to_xml(a),
115+
cybox.utils.wrap_cdata(b))
115116
self.assertEqual(cybox.utils.normalize_to_xml(c), d)
116117
self.assertEqual(cybox.utils.denormalize_from_xml(a), c)
117118
self.assertEqual(cybox.utils.denormalize_from_xml(b), a)
@@ -122,7 +123,8 @@ def test_email_address(self):
122123
self._test_escape_unescape(escaped, unescaped)
123124

124125
def test_subject(self):
125-
escaped = "Oh, the perils of <script> & <frame>"
126+
escaped = cybox.utils.wrap_cdata(
127+
"Oh, the perils of <script> & <frame>")
126128
unescaped = "Oh, the perils of <script> & <frame>"
127129
self._test_escape_unescape(escaped, unescaped)
128130

cybox/utils/__init__.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,45 @@ def get_class_for_object_type(object_type):
1919

2020

2121
def denormalize_from_xml(value):
22+
# This is probably not necessary since the parser will have removed
23+
# the CDATA already.
24+
value = unwrap_cdata(value)
25+
2226
if ',' in value:
23-
return [xml.sax.saxutils.unescape(x, UNESCAPE_DICT).strip()
24-
for x in value.split(',')]
27+
return [unescape(x).strip() for x in value.split(',')]
2528
else:
26-
return xml.sax.saxutils.unescape(unicode(value), UNESCAPE_DICT)
29+
return unescape(value)
2730

2831

2932
def normalize_to_xml(value):
3033
if isinstance(value, list):
31-
return ",".join([xml.sax.saxutils.escape(x, ESCAPE_DICT)
32-
for x in value])
34+
value = ",".join([escape(x) for x in value])
35+
else:
36+
value = escape(unicode(value))
37+
38+
if '&comma;' in value:
39+
value = wrap_cdata(value)
40+
return value
41+
42+
43+
def escape(value):
44+
return xml.sax.saxutils.escape(value, ESCAPE_DICT)
45+
46+
47+
def unescape(value):
48+
return xml.sax.saxutils.unescape(value, UNESCAPE_DICT)
49+
50+
51+
def wrap_cdata(value):
52+
return "<![CDATA[" + value + "]]>"
53+
54+
55+
def unwrap_cdata(value):
56+
"""Remove CDATA wrapping from `value` if present"""
57+
if value.startswith("<![CDATA[") and value.endswith("]]>"):
58+
return value[9:-3]
3359
else:
34-
return xml.sax.saxutils.escape(unicode(value), ESCAPE_DICT)
60+
return value
3561

3662

3763
def test_value(value):

0 commit comments

Comments
 (0)