From 953ecd912b30e2988744c236d786c6640a091e2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= Date: Tue, 20 Jun 2023 15:04:59 +0200 Subject: [PATCH] Fix issues when parsing profiles When there is no white space between `` elements in a profile in a benchmark, or in a custom profile in a tailoring, every odd element was ignored because the XML node was skipped by OpenSCAP when parsing. This is caused by the situation that `xmlTextReaderRead` in `oscap_to_start_element` in `xccdf_parse_remarks` jumps to the next node but then `xmlTextReaderRead` at the end of the `while loop` in `xccdf_profile_parse` jumps again to the next node. If there is a white space node in between the element nodes, this works fine, but if there is no node in between, an element gets skipped. The commit also adds some unit tests: - `test_no_newline_between_select_elements` checks if no elements are skipped when parsing `` elements within `` elements - `test_single_line_tailoring` checks if a tailoring file with no newlines between XML elements is processed correctly. This is related to #1817. --- src/XCCDF/profile.c | 3 +- tests/API/XCCDF/unittests/CMakeLists.txt | 2 + ...test_no_newline_between_select_elements.sh | 15 +++ ...est_no_newline_between_select_elements.xml | 81 +++++++++++++++ .../unittests/test_single_line_tailoring.sh | 16 +++ .../test_single_line_tailoring.tailoring.xml | 1 + .../unittests/test_single_line_tailoring.xml | 99 +++++++++++++++++++ 7 files changed, 216 insertions(+), 1 deletion(-) create mode 100755 tests/API/XCCDF/unittests/test_no_newline_between_select_elements.sh create mode 100644 tests/API/XCCDF/unittests/test_no_newline_between_select_elements.xml create mode 100755 tests/API/XCCDF/unittests/test_single_line_tailoring.sh create mode 100644 tests/API/XCCDF/unittests/test_single_line_tailoring.tailoring.xml create mode 100644 tests/API/XCCDF/unittests/test_single_line_tailoring.xml diff --git a/src/XCCDF/profile.c b/src/XCCDF/profile.c index 115434032f0..a50b838aca7 100644 --- a/src/XCCDF/profile.c +++ b/src/XCCDF/profile.c @@ -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; diff --git a/tests/API/XCCDF/unittests/CMakeLists.txt b/tests/API/XCCDF/unittests/CMakeLists.txt index a1c7da67ff4..c9c507791ce 100644 --- a/tests/API/XCCDF/unittests/CMakeLists.txt +++ b/tests/API/XCCDF/unittests/CMakeLists.txt @@ -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") diff --git a/tests/API/XCCDF/unittests/test_no_newline_between_select_elements.sh b/tests/API/XCCDF/unittests/test_no_newline_between_select_elements.sh new file mode 100755 index 00000000000..59b38e78ae1 --- /dev/null +++ b/tests/API/XCCDF/unittests/test_no_newline_between_select_elements.sh @@ -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" diff --git a/tests/API/XCCDF/unittests/test_no_newline_between_select_elements.xml b/tests/API/XCCDF/unittests/test_no_newline_between_select_elements.xml new file mode 100644 index 00000000000..ad8eaa95b4c --- /dev/null +++ b/tests/API/XCCDF/unittests/test_no_newline_between_select_elements.xml @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + accepted + 1.0 + + xccdf_test_profile + This profile is for testing. + + + + This rule always pass + + + + + + This rule always fails + + + + + + + + + + 5.11 + 2009-01-12T10:41:00-05:00 + + + + + PASS + pass + + + + + + + + PASS + pass + + + + + + + + + + + + + + oval:x:var:1 + + + + + 100 + + + + + diff --git a/tests/API/XCCDF/unittests/test_single_line_tailoring.sh b/tests/API/XCCDF/unittests/test_single_line_tailoring.sh new file mode 100755 index 00000000000..9e0f5a6c96a --- /dev/null +++ b/tests/API/XCCDF/unittests/test_single_line_tailoring.sh @@ -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" diff --git a/tests/API/XCCDF/unittests/test_single_line_tailoring.tailoring.xml b/tests/API/XCCDF/unittests/test_single_line_tailoring.tailoring.xml new file mode 100644 index 00000000000..3bada781428 --- /dev/null +++ b/tests/API/XCCDF/unittests/test_single_line_tailoring.tailoring.xml @@ -0,0 +1 @@ +1 diff --git a/tests/API/XCCDF/unittests/test_single_line_tailoring.xml b/tests/API/XCCDF/unittests/test_single_line_tailoring.xml new file mode 100644 index 00000000000..a9e112a8717 --- /dev/null +++ b/tests/API/XCCDF/unittests/test_single_line_tailoring.xml @@ -0,0 +1,99 @@ + + + + + + + + + + + + + + + + + + 5.11.2 + 2021-02-01T08:07:06+01:00 + + + + + PASS + pass + + + + + + + + + + + + + + oval:x:var:1 + + + + + 100 + + + + + + + accepted + Test Benchmark + Description + 1.0 + + OpenSCAP + OpenSCAP + OpenSCAP + http://scap.nist.gov + + + xccdf_test_profile + This profile is for testing. + + + + + Rule R1 + Description + + + + + + Rule R2 + Description + + + + + + Rule R3 + Description + + + + + + Rule R4 + Description + + + + + + +