Skip to content

Commit 3fdbb4a

Browse files
committed
Fix schema ordering when adding tailoring to ARF files
Now the tailoring is added just before the extended-components (i.e. SCE). Created in part by Claude Code. Fixes #2260
1 parent 0cdf676 commit 3fdbb4a

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/DS/rds.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,16 @@ static int _ds_rds_create_from_dom(xmlDocPtr *ret, xmlDocPtr sds_doc,
737737
xmlSetProp(tailoring_component, BAD_CAST "id", BAD_CAST tailoring_component_id);
738738
xmlSetProp(tailoring_component, BAD_CAST "timestamp", BAD_CAST tailoring_doc_timestamp);
739739
xmlAddChild(tailoring_component, tailoring_res_node);
740-
xmlAddChild(sds_res_node, tailoring_component);
740+
741+
// Insert tailoring component after regular components but before extended-components
742+
// to maintain proper schema ordering (all components must come before extended-components)
743+
xmlNodePtr first_extended_component = node_get_child_element(sds_res_node, "extended-component");
744+
if (first_extended_component == NULL) {
745+
// no extended component yet, add to the end
746+
xmlAddChild(sds_res_node, tailoring_component);
747+
} else {
748+
xmlAddPrevSibling(first_extended_component, tailoring_component);
749+
}
741750

742751
xmlNodePtr checklists_element = NULL;
743752
xmlNodePtr datastream_element = node_get_child_element(sds_res_node, "data-stream");

tests/API/XCCDF/tailoring/all.sh

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,38 @@ function test_api_xccdf_tailoring_profile_generate_guide {
169169
rm -f $guide
170170
}
171171

172+
function test_api_xccdf_tailoring_with_extended_component_ordering {
173+
# Regression test for the fix ensuring tailoring extended-component is inserted
174+
# before existing extended-components (e.g. SCE scripts) to maintain schema ordering
175+
# See https://github.com/OpenSCAP/openscap/issues/2260 for more details
176+
177+
local INPUT=$srcdir/$1
178+
local TAILORING=$srcdir/$2
179+
180+
result=`mktemp`
181+
stderr=`mktemp`
182+
183+
# Generate ARF with tailoring
184+
$OSCAP xccdf eval --tailoring-file $TAILORING --profile "xccdf_com.example.www_profile_customized" --results-arf $result $INPUT 2>$stderr || [ "$?" == "2" ]
185+
186+
# Validate the ARF against schema - this would fail if ordering is wrong
187+
$OSCAP ds rds-validate $result 2>$stderr
188+
189+
# Verify that tailoring extended-component exists
190+
assert_exists 1 '/arf:asset-report-collection/arf:report-requests/arf:report-request/arf:content/ds:data-stream-collection/ds:component/xccdf:Tailoring'
191+
192+
# Additional check: If we have multiple extended-components, verify tailoring comes before others
193+
# This uses xmllint to check the position - tailoring should come before any other extended-component
194+
extended_comp_count=$($XPATH $result 'count(//ds:extended-component)')
195+
if [ "$extended_comp_count" -gt "1" ]; then
196+
# Get the first extended-component and verify it contains Tailoring
197+
first_ext_comp_has_tailoring=$($XPATH $result 'count(//ds:extended-component[1]/xccdf:Tailoring)')
198+
[ "$first_ext_comp_has_tailoring" == "1" ] || return 1
199+
fi
200+
201+
rm -f "$result" "$stderr"
202+
}
203+
172204
# Testing.
173205

174206
test_init "test_api_xccdf_tailoring.log"
@@ -191,6 +223,6 @@ test_run "test_api_xccdf_tailoring_simple_include_in_arf_xlink_namespace" test_a
191223
test_run "test_api_xccdf_tailoring_profile_include_in_arf" test_api_xccdf_tailoring_profile_include_in_arf baseline.xccdf.xml baseline.tailoring.xml
192224
test_run "test_api_xccdf_tailoring_profile_generate_fix" test_api_xccdf_tailoring_profile_generate_fix baseline.xccdf.xml baseline.tailoring.xml
193225
test_run "test_api_xccdf_tailoring_profile_generate_guide" test_api_xccdf_tailoring_profile_generate_guide baseline.xccdf.xml baseline.tailoring.xml
194-
226+
test_run "test_api_xccdf_tailoring_with_extended_component_ordering" test_api_xccdf_tailoring_with_extended_component_ordering baseline.xccdf.xml baseline.tailoring.xml
195227

196228
test_exit

0 commit comments

Comments
 (0)