Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/XMLDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include "util/memory.h"

#include <libxml/parser.h>
#ifndef _WIN32
#include <libxml/uri.h>
#endif
#include <libxml/xmlschemas.h>
#include <libxml/c14n.h> // needs to be last to workaround old libxml2 errors

Expand Down Expand Up @@ -583,7 +586,19 @@ struct XMLSchema
auto parser(std::string &&path)
{
std::replace(path.begin(), path.end(), '\\', '/');
auto parser = make_unique_ptr<xmlSchemaFreeParserCtxt>(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<const char*>(uri.get());
#else
const auto *schemaPath = path.c_str();
#endif
auto parser = make_unique_ptr<xmlSchemaFreeParserCtxt>(
xmlSchemaNewParserCtxt(schemaPath));
if(!parser)
THROW("Failed to create schema parser context %s", path.c_str());
xmlSchemaSetParserErrors(parser.get(), schemaValidationError, schemaValidationWarning, nullptr);
Expand Down
20 changes: 20 additions & 0 deletions test/libdigidocpp_boost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading