Skip to content

Commit 1debe26

Browse files
committed
Handle spaces in path
IB-8215 Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent a62f525 commit 1debe26

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/XMLDocument.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include "util/memory.h"
2626

2727
#include <libxml/parser.h>
28+
#ifndef _WIN32
29+
#include <libxml/uri.h>
30+
#endif
2831
#include <libxml/xmlschemas.h>
2932
#include <libxml/c14n.h> // needs to be last to workaround old libxml2 errors
3033

@@ -583,7 +586,19 @@ struct XMLSchema
583586
auto parser(std::string &&path)
584587
{
585588
std::replace(path.begin(), path.end(), '\\', '/');
586-
auto parser = make_unique_ptr<xmlSchemaFreeParserCtxt>(xmlSchemaNewParserCtxt(path.c_str()));
589+
#ifndef _WIN32
590+
if(path.starts_with('/'))
591+
path.insert(0, "file://");
592+
auto uri = make_unique_ptr(xmlURIEscapeStr(BAD_CAST path.c_str(), BAD_CAST "/:"),
593+
[](xmlChar *value) { xmlFree(value); });
594+
if(!uri)
595+
THROW("Failed to construct schema URI %s", path.c_str());
596+
const auto *schemaPath = reinterpret_cast<const char*>(uri.get());
597+
#else
598+
const auto *schemaPath = path.c_str();
599+
#endif
600+
auto parser = make_unique_ptr<xmlSchemaFreeParserCtxt>(
601+
xmlSchemaNewParserCtxt(schemaPath));
587602
if(!parser)
588603
THROW("Failed to create schema parser context %s", path.c_str());
589604
xmlSchemaSetParserErrors(parser.get(), schemaValidationError, schemaValidationWarning, nullptr);

test/libdigidocpp_boost.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,26 @@ BOOST_AUTO_TEST_CASE(WrapContainerWithExpiredSignatures)
738738
BOOST_AUTO_TEST_SUITE_END()
739739

740740
BOOST_AUTO_TEST_SUITE(XMLTestSuite)
741+
BOOST_AUTO_TEST_CASE(SchemaPathWithSpaces)
742+
{
743+
struct TempSchemaDir
744+
{
745+
fs::path path;
746+
~TempSchemaDir()
747+
{
748+
error_code ec;
749+
fs::remove_all(path, ec);
750+
}
751+
} schemaDir{util::File::tempFileName().concat(" schema path")};
752+
753+
fs::remove(schemaDir.path);
754+
fs::copy(DIGIDOCPPCONF, schemaDir.path, fs::copy_options::recursive);
755+
756+
XMLSchema schema((schemaDir.path / "ts_119612v020201_201601xsd.xsd").string());
757+
XMLDocument doc("TSL.xml");
758+
BOOST_CHECK_NO_THROW(schema.validate(doc));
759+
}
760+
741761
BOOST_AUTO_TEST_CASE(XMLBomb)
742762
{
743763
BOOST_CHECK_THROW(XMLDocument("xml-bomb-attr.xml"), Exception);

0 commit comments

Comments
 (0)