Skip to content

Commit 17a0136

Browse files
authored
fix: encode paths when loading XML schemas (#1028)
Signed-off-by: Shurong Cao <[email protected]> Co-authored-by: Shurong Cao <[email protected]>
1 parent 8edee5c commit 17a0136

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎cyclonedx/validation/xml.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from abc import ABC
2222
from collections.abc import Iterable
23+
from pathlib import Path
2324
from typing import TYPE_CHECKING, Literal, Optional, Union, overload
2425

2526
from ..exception import MissingOptionalDependencyException
@@ -125,7 +126,7 @@ def _validator(self) -> 'XMLSchema':
125126
schema_file = self._schema_file
126127
if schema_file is None:
127128
raise NotImplementedError('missing schema file')
128-
self.__validator = XMLSchema(file=schema_file)
129+
self.__validator = XMLSchema(file=Path(schema_file).absolute().as_uri())
129130
return self.__validator
130131

131132

‎tests/test_validation_xml.py‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
from collections.abc import Generator
1919
from glob import iglob
2020
from itertools import chain
21-
from os.path import join
21+
from os.path import basename, dirname, join
22+
from shutil import copytree
23+
from tempfile import TemporaryDirectory
2224
from unittest import TestCase
25+
from unittest.mock import patch
2326

2427
from ddt import ddt, idata, unpack
2528

2629
from cyclonedx.exception import MissingOptionalDependencyException
2730
from cyclonedx.schema import OutputFormat, SchemaVersion
31+
from cyclonedx.schema._res import BOM_XML
2832
from cyclonedx.validation.xml import XmlValidator
2933
from tests import OWN_DATA_DIRECTORY, SCHEMA_TESTDATA_DIRECTORY, DpTuple
3034

@@ -51,6 +55,23 @@ def _dp_sv_own(valid: bool) -> Generator:
5155
@ddt
5256
class TestXmlValidator(TestCase):
5357

58+
def test_validate_schema_from_path_with_fragment_character(self) -> None:
59+
schema_version = SchemaVersion.V1_6
60+
schema_file = BOM_XML[schema_version]
61+
self.assertIsNotNone(schema_file)
62+
63+
with TemporaryDirectory(prefix='cyclonedx#') as temp_directory:
64+
schema_directory = join(temp_directory, 'schemas')
65+
copytree(dirname(schema_file), schema_directory)
66+
copied_schema_file = join(schema_directory, basename(schema_file))
67+
68+
with patch.dict('cyclonedx.validation.xml._S_BOM', {schema_version: copied_schema_file}):
69+
validation_error = XmlValidator(schema_version).validate_str(
70+
'<bom xmlns="http://cyclonedx.org/schema/bom/1.6" version="1"/>'
71+
)
72+
73+
self.assertIsNone(validation_error)
74+
5475
@idata(sv for sv in SchemaVersion if sv not in UNSUPPORTED_SCHEMA_VERSIONS)
5576
def test_validator_as_expected(self, schema_version: SchemaVersion) -> None:
5677
validator = XmlValidator(schema_version)

0 commit comments

Comments
 (0)