From 16fb41e5cc1a67c33311166c1f8f6c2793baed41 Mon Sep 17 00:00:00 2001 From: Raul Metsma Date: Tue, 21 Jul 2026 09:17:12 +0300 Subject: [PATCH] Handle spaces in path IB-9030 Signed-off-by: Raul Metsma --- src/XMLDocument.h | 17 ++++++++++++++++- test/libdigidocpp_boost.cpp | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/XMLDocument.h b/src/XMLDocument.h index 6c046149d..1aa795e75 100644 --- a/src/XMLDocument.h +++ b/src/XMLDocument.h @@ -25,6 +25,9 @@ #include "util/memory.h" #include +#ifndef _WIN32 +#include +#endif #include #include // needs to be last to workaround old libxml2 errors @@ -583,7 +586,19 @@ struct XMLSchema auto parser(std::string &&path) { std::replace(path.begin(), path.end(), '\\', '/'); - auto parser = make_unique_ptr(xmlSchemaNewParserCtxt(path.c_str())); +#ifndef _WIN32 + if(path.starts_with('/')) + path.insert(0, "file://"); + auto uri = make_unique_ptr(xmlURIEscapeStr(BAD_CAST path.c_str(), BAD_CAST "/:"), + [](xmlChar *value) { xmlFree(value); }); + if(!uri) + THROW("Failed to construct schema URI %s", path.c_str()); + const auto *schemaPath = reinterpret_cast(uri.get()); +#else + const auto *schemaPath = path.c_str(); +#endif + auto parser = make_unique_ptr( + xmlSchemaNewParserCtxt(schemaPath)); if(!parser) THROW("Failed to create schema parser context %s", path.c_str()); xmlSchemaSetParserErrors(parser.get(), schemaValidationError, schemaValidationWarning, nullptr); diff --git a/test/libdigidocpp_boost.cpp b/test/libdigidocpp_boost.cpp index 9b0d9674a..0918dcbd1 100644 --- a/test/libdigidocpp_boost.cpp +++ b/test/libdigidocpp_boost.cpp @@ -738,6 +738,26 @@ BOOST_AUTO_TEST_CASE(WrapContainerWithExpiredSignatures) BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(XMLTestSuite) +BOOST_AUTO_TEST_CASE(SchemaPathWithSpaces) +{ + struct TempSchemaDir + { + fs::path path; + ~TempSchemaDir() + { + error_code ec; + fs::remove_all(path, ec); + } + } schemaDir{util::File::tempFileName().concat(" schema path")}; + + fs::remove(schemaDir.path); + fs::copy(DIGIDOCPPCONF, schemaDir.path, fs::copy_options::recursive); + + XMLSchema schema((schemaDir.path / "ts_119612v020201_201601xsd.xsd").string()); + XMLDocument doc("TSL.xml"); + BOOST_CHECK_NO_THROW(schema.validate(doc)); +} + BOOST_AUTO_TEST_CASE(XMLBomb) { BOOST_CHECK_THROW(XMLDocument("xml-bomb-attr.xml"), Exception);