Skip to content

Commit

Permalink
Merge pull request #1991 from jan-cerny/issue1817
Browse files Browse the repository at this point in the history
Fix issues when parsing profiles
  • Loading branch information
evgenyz authored Jul 3, 2023
2 parents 2fb46d9 + 953ecd9 commit 9f83ba0
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/XCCDF/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,15 @@ struct xccdf_item *xccdf_profile_parse(xmlTextReaderPtr reader, struct xccdf_ite
}
case XCCDFE_SET_VALUE:{
oscap_list_add(prof->sub.profile.setvalues, xccdf_setvalue_new_parse(reader));
xmlTextReaderRead(reader);
break;
}
default:
if (!xccdf_item_process_element(prof, reader))
dW("Encountered an unknown element '%s' while parsing XCCDF profile.",
xmlTextReaderConstLocalName(reader));
xmlTextReaderRead(reader);
}
xmlTextReaderRead(reader);
}

return prof;
Expand Down
2 changes: 2 additions & 0 deletions tests/API/XCCDF/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,5 @@ add_oscap_test("test_generate_fix_ansible_vars.sh")
add_oscap_test("test_xccdf_requires_conflicts.sh")
add_oscap_test("test_results_hostname.sh")
add_oscap_test("test_skip_rule.sh")
add_oscap_test("test_no_newline_between_select_elements.sh")
add_oscap_test("test_single_line_tailoring.sh")
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
. $builddir/tests/test_common.sh

set -e
set -o pipefail

stdout=$(mktemp)

$OSCAP xccdf eval --progress --profile "xccdf_com.example.www_profile_test" "$srcdir/test_no_newline_between_select_elements.xml" > "$stdout" || true

# test if both rules selected in the profile were evaluated
grep -q "xccdf_com.example.www_rule_first:pass" "$stdout"
grep -q "xccdf_com.example.www_rule_second:fail" "$stdout"

rm -f "$stdout"
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<ds:data-stream-collection xmlns:ds="http://scap.nist.gov/schema/scap/source/1.2" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:cat="urn:oasis:names:tc:entity:xmlns:xml:catalog" id="scap_org.open-scap_collection_from_xccdf_test_single_rule.xccdf.xml" schematron-version="1.2">
<ds:data-stream id="scap_org.open-scap_datastream_from_xccdf_test_single_rule.xccdf.xml" scap-version="1.2" use-case="OTHER">
<ds:checklists>
<ds:component-ref id="scap_org.open-scap_cref_test_single_rule.xccdf.xml" xlink:href="#scap_org.open-scap_comp_test_single_rule.xccdf.xml">
<cat:catalog>
<cat:uri name="test_single_rule.oval.xml" uri="#scap_org.open-scap_cref_test_single_rule.oval.xml"/>
</cat:catalog>
</ds:component-ref>
</ds:checklists>
<ds:checks>
<ds:component-ref id="scap_org.open-scap_cref_test_single_rule.oval.xml" xlink:href="#scap_org.open-scap_comp_test_single_rule.oval.xml"/>
</ds:checks>
</ds:data-stream>
<ds:component id="scap_org.open-scap_comp_test_single_rule.xccdf.xml" timestamp="2017-06-09T09:15:45">
<Benchmark xmlns="http://checklists.nist.gov/xccdf/1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="xccdf_com.example.www_benchmark_dummy" xsi:schemaLocation="http://checklists.nist.gov/xccdf/1.1 xccdf-1.1.4.xsd" resolved="false" xml:lang="en-US">
<status>accepted</status>
<version>1.0</version>
<Profile id="xccdf_com.example.www_profile_test">
<title>xccdf_test_profile</title>
<description>This profile is for testing.</description>
<select idref="xccdf_com.example.www_rule_first" selected="true"/><select idref="xccdf_com.example.www_rule_second" selected="true"/>
</Profile>
<Rule selected="false" id="xccdf_com.example.www_rule_first">
<title>This rule always pass</title>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref href="test_single_rule.oval.xml" name="oval:x:def:1"/>
</check>
</Rule>
<Rule selected="false" id="xccdf_com.example.www_rule_second">
<title>This rule always fails</title>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref href="test_single_rule.oval.xml" name="oval:x:def:2"/>
</check>
</Rule>
</Benchmark>
</ds:component>
<ds:component id="scap_org.open-scap_comp_test_single_rule.oval.xml" timestamp="2017-06-09T07:07:38">
<oval_definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:ind-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent" xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:win-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#windows" xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#independent independent-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#windows windows-definitions-schema.xsd">
<generator>
<oval:schema_version>5.11</oval:schema_version>
<oval:timestamp>2009-01-12T10:41:00-05:00</oval:timestamp>
</generator>
<definitions>
<definition class="compliance" id="oval:x:def:1" version="1">
<metadata>
<title>PASS</title>
<description>pass</description>
</metadata>
<criteria>
<criterion comment="PASS test" test_ref="oval:x:tst:1"/>
</criteria>
</definition>
<definition class="compliance" id="oval:x:def:2" version="1">
<metadata>
<title>PASS</title>
<description>pass</description>
</metadata>
<criteria>
<criterion negate="true" comment="PASS test" test_ref="oval:x:tst:1"/>
</criteria>
</definition>
</definitions>
<tests>
<variable_test xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent" id="oval:x:tst:1" check="all" comment="always pass" version="1">
<object object_ref="oval:x:obj:1"/>
</variable_test>
</tests>
<objects>
<variable_object xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent" id="oval:x:obj:1" version="1" comment="x">
<var_ref>oval:x:var:1</var_ref>
</variable_object>
</objects>
<variables>
<constant_variable id="oval:x:var:1" version="1" comment="x" datatype="int">
<value>100</value>
</constant_variable>
</variables>
</oval_definitions>
</ds:component>
</ds:data-stream-collection>
16 changes: 16 additions & 0 deletions tests/API/XCCDF/unittests/test_single_line_tailoring.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
. $builddir/tests/test_common.sh

set -e
set -o pipefail

result=$(mktemp)

$OSCAP xccdf eval --profile "xccdf_com.example.www_profile_custom" --results "$result" --tailoring-file "$srcdir/test_single_line_tailoring.tailoring.xml" "$srcdir/test_single_line_tailoring.xml"

assert_exists 1 '//rule-result[@idref="xccdf_com.example.www_rule_R1"]/result[text()="pass"]'
assert_exists 1 '//rule-result[@idref="xccdf_com.example.www_rule_R2"]/result[text()="notselected"]'
assert_exists 1 '//rule-result[@idref="xccdf_com.example.www_rule_R3"]/result[text()="notselected"]'
assert_exists 1 '//rule-result[@idref="xccdf_com.example.www_rule_R4"]/result[text()="notselected"]'

rm -f "$result"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<xccdf-1.2:Tailoring xmlns:xccdf-1.2="http://checklists.nist.gov/xccdf/1.2" id="xccdf_auto_tailoring_default"><xccdf-1.2:benchmark href="file:///home/jcerny/work/git/openscap/tests/API/XCCDF/unittests/tests/API/XCCDF/unittests/test_single_line_tailoring.xml" /><xccdf-1.2:version time="2023-06-20T13:57:09.735106">1</xccdf-1.2:version><xccdf-1.2:Profile id="xccdf_com.example.www_profile_custom" extends="xccdf_com.example.www_profile_P1"><xccdf-1.2:title override="false" /><xccdf-1.2:select idref="xccdf_com.example.www_rule_R2" selected="false" /><xccdf-1.2:select idref="xccdf_com.example.www_rule_R3" selected="false" /><xccdf-1.2:select idref="xccdf_com.example.www_rule_R4" selected="false" /></xccdf-1.2:Profile></xccdf-1.2:Tailoring>
99 changes: 99 additions & 0 deletions tests/API/XCCDF/unittests/test_single_line_tailoring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="utf-8"?>
<ds:data-stream-collection xmlns:ds="http://scap.nist.gov/schema/scap/source/1.2" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:cat="urn:oasis:names:tc:entity:xmlns:xml:catalog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="scap_org.open-scap_collection_from_xccdf_test_single_rule.xccdf.xml" schematron-version="1.3" xsi:schemaLocation="http://scap.nist.gov/schema/scap/source/1.2 https://scap.nist.gov/schema/scap/1.3/scap-source-data-stream_1.3.xsd">
<ds:data-stream id="scap_org.open-scap_datastream_simple" scap-version="1.3" use-case="OTHER">
<ds:checklists>
<ds:component-ref id="scap_org.open-scap_cref_test_single_rule.xccdf.xml" xlink:href="#scap_org.open-scap_comp_test_single_rule.xccdf.xml">
<cat:catalog>
<cat:uri name="test_single_rule.oval.xml" uri="#scap_org.open-scap_cref_test_single_rule.oval.xml"/>
</cat:catalog>
</ds:component-ref>
</ds:checklists>
<ds:checks>
<ds:component-ref id="scap_org.open-scap_cref_test_single_rule.oval.xml" xlink:href="#scap_org.open-scap_comp_test_single_rule.oval.xml"/>
</ds:checks>
</ds:data-stream>
<ds:component id="scap_org.open-scap_comp_test_single_rule.oval.xml" timestamp="2021-02-01T08:07:06+01:00">
<oval_definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:ind-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent" xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:win-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#windows" xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#independent independent-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#windows windows-definitions-schema.xsd">
<generator>
<oval:schema_version>5.11.2</oval:schema_version>
<oval:timestamp>2021-02-01T08:07:06+01:00</oval:timestamp>
</generator>
<definitions>
<definition class="compliance" id="oval:x:def:1" version="1">
<metadata>
<title>PASS</title>
<description>pass</description>
</metadata>
<criteria>
<criterion comment="PASS test" test_ref="oval:x:tst:1"/>
</criteria>
</definition>
</definitions>
<tests>
<variable_test xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent" id="oval:x:tst:1" check="all" comment="always pass" version="1">
<object object_ref="oval:x:obj:1"/>
</variable_test>
</tests>
<objects>
<variable_object xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent" id="oval:x:obj:1" version="1" comment="x">
<var_ref>oval:x:var:1</var_ref>
</variable_object>
</objects>
<variables>
<constant_variable id="oval:x:var:1" version="1" comment="x" datatype="int">
<value>100</value>
</constant_variable>
</variables>
</oval_definitions>
</ds:component>
<ds:component id="scap_org.open-scap_comp_test_single_rule.xccdf.xml" timestamp="2021-02-01T08:07:06+01:00">
<Benchmark xmlns="http://checklists.nist.gov/xccdf/1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="xccdf_com.example.www_benchmark_dummy" xsi:schemaLocation="http://checklists.nist.gov/xccdf/1.1 xccdf-1.1.4.xsd" resolved="false" xml:lang="en-US">
<status date="2021-01-21">accepted</status>
<title>Test Benchmark</title>
<description>Description</description>
<version>1.0</version>
<metadata>
<dc:contributor xmlns:dc="http://purl.org/dc/elements/1.1/">OpenSCAP</dc:contributor>
<dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">OpenSCAP</dc:publisher>
<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">OpenSCAP</dc:creator>
<dc:source xmlns:dc="http://purl.org/dc/elements/1.1/">http://scap.nist.gov</dc:source>
</metadata>
<Profile id="xccdf_com.example.www_profile_P1">
<title>xccdf_test_profile</title>
<description>This profile is for testing.</description>
<select idref="xccdf_com.example.www_rule_R1" selected="true"/>
<select idref="xccdf_com.example.www_rule_R2" selected="true"/>
<select idref="xccdf_com.example.www_rule_R3" selected="true"/>
<select idref="xccdf_com.example.www_rule_R4" selected="true"/>
</Profile>
<Rule selected="false" id="xccdf_com.example.www_rule_R1">
<title>Rule R1</title>
<description>Description</description>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref href="test_single_rule.oval.xml" name="oval:x:def:1"/>
</check>
</Rule>
<Rule selected="false" id="xccdf_com.example.www_rule_R2">
<title>Rule R2</title>
<description>Description</description>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref href="test_single_rule.oval.xml" name="oval:x:def:1"/>
</check>
</Rule>
<Rule selected="false" id="xccdf_com.example.www_rule_R3">
<title>Rule R3</title>
<description>Description</description>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref href="test_single_rule.oval.xml" name="oval:x:def:1"/>
</check>
</Rule>
<Rule selected="false" id="xccdf_com.example.www_rule_R4">
<title>Rule R4</title>
<description>Description</description>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref href="test_single_rule.oval.xml" name="oval:x:def:1"/>
</check>
</Rule>
</Benchmark>
</ds:component>
</ds:data-stream-collection>

0 comments on commit 9f83ba0

Please sign in to comment.