Blame SOURCES/openscap-1.3.7-PR-1891-xmlfilecontent.patch

26940c
From 9c2052febe494ca5fe8e3fef7996fd2c2c736785 Mon Sep 17 00:00:00 2001
26940c
From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= <jcerny@redhat.com>
26940c
Date: Wed, 2 Nov 2022 09:04:25 +0100
26940c
Subject: [PATCH] Don't emit items if XPath doesn't match
26940c
26940c
This commit fixes the behavior of the xmlfilecontent probe in situation
26940c
when the XPath query in xmlfilecontent_object doesn't match any node in
26940c
the given XML file and the query returns an empty node set. Currently,
26940c
in this situation, we emit an item in which we add an empty value_of
26940c
element. However, this value_of element has its datatype attribute set
26940c
to an empty string, which is invalid according to the OVAL schema. When
26940c
we try to make the OVAL results valid, we face the problem that it isn't
26940c
clear what should be the value of the datatype attribute for empty
26940c
elements. But as we can realize the XPath doesn't match anything means
26940c
that the requested object doesn't exist on the system, so a better
26940c
behavior would be to not produce a xmlfilecontent54_item.  That is
26940c
consistent with eg. situation when a regular expression matched nothing
26940c
in textfilecontent54_object.  This commit therefore stops the item
26940c
generation in this situation.
26940c
26940c
This commit also extends the existing test to cover the situation
26940c
of XPath queries for nonexistent element and nonexistent attribute.
26940c
26940c
Fixes: #1890, rhbz#2138884, rhbz#2139060
26940c
---
26940c
 .../probes/independent/xmlfilecontent_probe.c |  5 +--
26940c
 .../test_xmlfilecontent_probe.sh              |  6 +++
26940c
 .../test_xmlfilecontent_probe.xml             | 38 +++++++++++++++++++
26940c
 3 files changed, 46 insertions(+), 3 deletions(-)
26940c
26940c
diff --git a/src/OVAL/probes/independent/xmlfilecontent_probe.c b/src/OVAL/probes/independent/xmlfilecontent_probe.c
26940c
index 6c70b359ba..5d56afa0d4 100644
26940c
--- a/src/OVAL/probes/independent/xmlfilecontent_probe.c
26940c
+++ b/src/OVAL/probes/independent/xmlfilecontent_probe.c
26940c
@@ -296,10 +296,9 @@ static int process_file(const char *prefix, const char *path, const char *filena
26940c
 
26940c
 		node_cnt = nodes->nodeNr;
26940c
 		dD("node_cnt: %d.", node_cnt);
26940c
-		if (node_cnt == 0) {
26940c
-			probe_item_setstatus(item, SYSCHAR_STATUS_DOES_NOT_EXIST);
26940c
-			probe_item_ent_add(item, "value_of", NULL, NULL);
26940c
-			probe_itement_setstatus(item, "value_of", 1, SYSCHAR_STATUS_DOES_NOT_EXIST);
26940c
+		if (node_cnt <= 0) {
26940c
+			ret = -5;
26940c
+			goto cleanup;
26940c
 		} else {
26940c
 			node_tab = nodes->nodeTab;
26940c
 			for (i = 0; i < node_cnt; ++i) {
26940c
diff --git a/tests/probes/xmlfilecontent/test_xmlfilecontent_probe.sh b/tests/probes/xmlfilecontent/test_xmlfilecontent_probe.sh
26940c
index e3c56a8606..68138dad75 100755
26940c
--- a/tests/probes/xmlfilecontent/test_xmlfilecontent_probe.sh
26940c
+++ b/tests/probes/xmlfilecontent/test_xmlfilecontent_probe.sh
26940c
@@ -6,9 +6,15 @@ set -e -o pipefail
26940c
 cp $srcdir/example.xml /tmp/
26940c
 result=$(mktemp)
26940c
 $OSCAP oval eval --results $result $srcdir/test_xmlfilecontent_probe.xml
26940c
+# Even if OSCAP_FULL_VALIDATION is set, an invalid OVAL result doesn't cause
26940c
+# the "oscap oval eval" to return a non-zero value, so let's run validation
26940c
+# as a separate command
26940c
+$OSCAP oval validate "$result"
26940c
 assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:1" and @result="true"]'
26940c
 assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:2" and @result="true"]'
26940c
 assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:3" and @result="true"]'
26940c
 assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:4" and @result="true"]'
26940c
 assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:5" and @result="true"]'
26940c
+assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:6" and @result="true"]'
26940c
+assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:7" and @result="true"]'
26940c
 rm -f $result
26940c
\ No newline at end of file
26940c
diff --git a/tests/probes/xmlfilecontent/test_xmlfilecontent_probe.xml b/tests/probes/xmlfilecontent/test_xmlfilecontent_probe.xml
26940c
index 3350df0c49..0a9708d4b6 100644
26940c
--- a/tests/probes/xmlfilecontent/test_xmlfilecontent_probe.xml
26940c
+++ b/tests/probes/xmlfilecontent/test_xmlfilecontent_probe.xml
26940c
@@ -66,6 +66,30 @@
26940c
         <criterion test_ref="oval:x:tst:5" comment="test"/>
26940c
       </criteria>
26940c
     </definition>
26940c
+    <definition class="compliance" version="1" id="oval:x:def:6">
26940c
+      <metadata>
26940c
+        <title>A simple test OVAL for xmlfilecontent test - check nonexisting attribute</title>
26940c
+        <description>x</description>
26940c
+        <affected family="unix">
26940c
+          <platform>x</platform>
26940c
+        </affected>
26940c
+      </metadata>
26940c
+      <criteria>
26940c
+        <criterion test_ref="oval:x:tst:6" comment="test"/>
26940c
+      </criteria>
26940c
+    </definition>
26940c
+    <definition class="compliance" version="1" id="oval:x:def:7">
26940c
+      <metadata>
26940c
+        <title>A simple test OVAL for xmlfilecontent test - check nonexisting element</title>
26940c
+        <description>x</description>
26940c
+        <affected family="unix">
26940c
+          <platform>x</platform>
26940c
+        </affected>
26940c
+      </metadata>
26940c
+      <criteria>
26940c
+        <criterion test_ref="oval:x:tst:7" comment="test"/>
26940c
+      </criteria>
26940c
+    </definition>
26940c
   </definitions>
26940c
 
26940c
   <tests>
26940c
@@ -89,6 +113,12 @@
26940c
       <ind:object object_ref="oval:x:obj:5"/>
26940c
       <ind:state state_ref="oval:x:ste:5"/>
26940c
     </ind:xmlfilecontent_test>
26940c
+    <ind:xmlfilecontent_test id="oval:x:tst:6" version="1" comment="test an xpath expression" check="all" check_existence="none_exist">
26940c
+      <ind:object object_ref="oval:x:obj:6"/>
26940c
+    </ind:xmlfilecontent_test>
26940c
+    <ind:xmlfilecontent_test id="oval:x:tst:7" version="1" comment="test an xpath expression" check="all" check_existence="none_exist">
26940c
+      <ind:object object_ref="oval:x:obj:7"/>
26940c
+    </ind:xmlfilecontent_test>
26940c
   </tests>
26940c
 
26940c
   <objects>
26940c
@@ -112,6 +142,14 @@
26940c
         <ind:filepath>/tmp/example.xml</ind:filepath>
26940c
         <ind:xpath>//*[@regid="mycoyote.com"]/@name</ind:xpath>
26940c
     </ind:xmlfilecontent_object>
26940c
+    <ind:xmlfilecontent_object id="oval:x:obj:6" version="1" comment="xpath query">
26940c
+        <ind:filepath>/tmp/example.xml</ind:filepath>
26940c
+        <ind:xpath>/SoftwareIdentity/@thisattributedoesnotexist</ind:xpath>
26940c
+    </ind:xmlfilecontent_object>
26940c
+    <ind:xmlfilecontent_object id="oval:x:obj:7" version="1" comment="xpath query">
26940c
+        <ind:filepath>/tmp/example.xml</ind:filepath>
26940c
+        <ind:xpath>/SoftwareIdentity/thiselementdoesnotexist</ind:xpath>
26940c
+    </ind:xmlfilecontent_object>
26940c
   </objects>
26940c
 
26940c
   <states>