Skip to content

Commit 9c2052f

Browse files
committed
Don't emit items if XPath doesn't match
This commit fixes the behavior of the xmlfilecontent probe in situation when the XPath query in xmlfilecontent_object doesn't match any node in the given XML file and the query returns an empty node set. Currently, in this situation, we emit an item in which we add an empty value_of element. However, this value_of element has its datatype attribute set to an empty string, which is invalid according to the OVAL schema. When we try to make the OVAL results valid, we face the problem that it isn't clear what should be the value of the datatype attribute for empty elements. But as we can realize the XPath doesn't match anything means that the requested object doesn't exist on the system, so a better behavior would be to not produce a xmlfilecontent54_item. That is consistent with eg. situation when a regular expression matched nothing in textfilecontent54_object. This commit therefore stops the item generation in this situation. This commit also extends the existing test to cover the situation of XPath queries for nonexistent element and nonexistent attribute. Fixes: #1890, rhbz#2138884, rhbz#2139060
1 parent 211b596 commit 9c2052f

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/OVAL/probes/independent/xmlfilecontent_probe.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,8 @@ static int process_file(const char *prefix, const char *path, const char *filena
297297
node_cnt = nodes->nodeNr;
298298
dD("node_cnt: %d.", node_cnt);
299299
if (node_cnt <= 0) {
300-
probe_item_setstatus(item, SYSCHAR_STATUS_DOES_NOT_EXIST);
301-
probe_item_ent_add(item, "value_of", NULL, NULL);
302-
probe_itement_setstatus(item, "value_of", 1, SYSCHAR_STATUS_DOES_NOT_EXIST);
300+
ret = -5;
301+
goto cleanup;
303302
} else {
304303
node_tab = nodes->nodeTab;
305304
for (i = 0; i < node_cnt; ++i) {

tests/probes/xmlfilecontent/test_xmlfilecontent_probe.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ set -e -o pipefail
66
cp $srcdir/example.xml /tmp/
77
result=$(mktemp)
88
$OSCAP oval eval --results $result $srcdir/test_xmlfilecontent_probe.xml
9+
# Even if OSCAP_FULL_VALIDATION is set, an invalid OVAL result doesn't cause
10+
# the "oscap oval eval" to return a non-zero value, so let's run validation
11+
# as a separate command
12+
$OSCAP oval validate "$result"
913
assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:1" and @result="true"]'
1014
assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:2" and @result="true"]'
1115
assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:3" and @result="true"]'
1216
assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:4" and @result="true"]'
1317
assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:5" and @result="true"]'
18+
assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:6" and @result="true"]'
19+
assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:7" and @result="true"]'
1420
rm -f $result

tests/probes/xmlfilecontent/test_xmlfilecontent_probe.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@
6666
<criterion test_ref="oval:x:tst:5" comment="test"/>
6767
</criteria>
6868
</definition>
69+
<definition class="compliance" version="1" id="oval:x:def:6">
70+
<metadata>
71+
<title>A simple test OVAL for xmlfilecontent test - check nonexisting attribute</title>
72+
<description>x</description>
73+
<affected family="unix">
74+
<platform>x</platform>
75+
</affected>
76+
</metadata>
77+
<criteria>
78+
<criterion test_ref="oval:x:tst:6" comment="test"/>
79+
</criteria>
80+
</definition>
81+
<definition class="compliance" version="1" id="oval:x:def:7">
82+
<metadata>
83+
<title>A simple test OVAL for xmlfilecontent test - check nonexisting element</title>
84+
<description>x</description>
85+
<affected family="unix">
86+
<platform>x</platform>
87+
</affected>
88+
</metadata>
89+
<criteria>
90+
<criterion test_ref="oval:x:tst:7" comment="test"/>
91+
</criteria>
92+
</definition>
6993
</definitions>
7094

7195
<tests>
@@ -89,6 +113,12 @@
89113
<ind:object object_ref="oval:x:obj:5"/>
90114
<ind:state state_ref="oval:x:ste:5"/>
91115
</ind:xmlfilecontent_test>
116+
<ind:xmlfilecontent_test id="oval:x:tst:6" version="1" comment="test an xpath expression" check="all" check_existence="none_exist">
117+
<ind:object object_ref="oval:x:obj:6"/>
118+
</ind:xmlfilecontent_test>
119+
<ind:xmlfilecontent_test id="oval:x:tst:7" version="1" comment="test an xpath expression" check="all" check_existence="none_exist">
120+
<ind:object object_ref="oval:x:obj:7"/>
121+
</ind:xmlfilecontent_test>
92122
</tests>
93123

94124
<objects>
@@ -112,6 +142,14 @@
112142
<ind:filepath>/tmp/example.xml</ind:filepath>
113143
<ind:xpath>//*[@regid="mycoyote.com"]/@name</ind:xpath>
114144
</ind:xmlfilecontent_object>
145+
<ind:xmlfilecontent_object id="oval:x:obj:6" version="1" comment="xpath query">
146+
<ind:filepath>/tmp/example.xml</ind:filepath>
147+
<ind:xpath>/SoftwareIdentity/@thisattributedoesnotexist</ind:xpath>
148+
</ind:xmlfilecontent_object>
149+
<ind:xmlfilecontent_object id="oval:x:obj:7" version="1" comment="xpath query">
150+
<ind:filepath>/tmp/example.xml</ind:filepath>
151+
<ind:xpath>/SoftwareIdentity/thiselementdoesnotexist</ind:xpath>
152+
</ind:xmlfilecontent_object>
115153
</objects>
116154

117155
<states>

0 commit comments

Comments
 (0)