diff --git a/src/basic_xml_grammar.ipp b/src/basic_xml_grammar.ipp index ee9eced64..61cd5a2a9 100644 --- a/src/basic_xml_grammar.ipp +++ b/src/basic_xml_grammar.ipp @@ -22,7 +22,7 @@ #endif #include // errno -#include // strerror(errno) +#include // strerror(errno), strlen // spirit stuff #include @@ -461,10 +461,13 @@ void basic_xml_grammar::init(IStream & is){ boost::serialization::throw_exception( xml_archive_exception(xml_archive_exception::xml_archive_parsing_error) ); - if(! std::equal(rv.class_name.begin(), rv.class_name.end(), BOOST_ARCHIVE_SIGNATURE())) + const char * const signature = BOOST_ARCHIVE_SIGNATURE(); + if(rv.class_name.size() != std::strlen(signature) + || ! std::equal(rv.class_name.begin(), rv.class_name.end(), signature)){ boost::serialization::throw_exception( archive_exception(archive_exception::invalid_signature) ); + } } template diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index ec76185fe..b934441a0 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -181,6 +181,7 @@ if ! $(BOOST_ARCHIVE_LIST) { [ test-bsl-run test_xml_escape_boundary : : ../build//boost_wserialization : [ requires std_wstreambuf ] ] [ test-bsl-run test_xml_trailing_whitespace ] [ test-bsl-run test_xml_missing_nvp ] + [ test-bsl-run test_xml_signature_check ] [ test-bsl-run test_mult_archive_types : : : [ requires std_wstreambuf ] ] [ test-bsl-run test_iterators : : : [ requires std_wstreambuf ] ] [ test-bsl-run test_iterators_base64 ] diff --git a/test/test_xml_signature_check.cpp b/test/test_xml_signature_check.cpp new file mode 100644 index 000000000..4001a59b0 --- /dev/null +++ b/test/test_xml_signature_check.cpp @@ -0,0 +1,60 @@ +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// test_xml_signature_check.cpp + +// Copyright 2026 Gennaro Prota. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// An XML input archive must accept only the exact signature in the header. +// The check used to compare just as many characters as the archive gave, so +// it accepted a prefix of the signature, and read past the end of it for a +// longer one. + +// Reported by jhtrace in +// https://github.com/boostorg/serialization/issues/392. Thanks! + +#include +#include + +#include +#include +#include + +#include "test_tools.hpp" + +// The header of a valid archive, with the signature replaced. +std::string header_with_signature(const std::string & signature){ + std::ostringstream os; + { + boost::archive::xml_oarchive oa(os); + } + std::string header = os.str(); + const std::string attribute = "signature=\""; + const std::size_t first = header.find(attribute) + attribute.size(); + const std::size_t last = header.find('"', first); + header.replace(first, last - first, signature); + return header; +} + +bool is_accepted(const std::string & signature){ + bool accepted = true; + std::istringstream is(header_with_signature(signature)); + try{ + boost::archive::xml_iarchive ia(is); + } + catch(const boost::archive::archive_exception & e){ + BOOST_CHECK( + e.code == boost::archive::archive_exception::invalid_signature + ); + accepted = false; + } + return accepted; +} + +int test_main(int /* argc */, char * /* argv */ []){ + BOOST_CHECK(is_accepted("serialization::archive")); + BOOST_CHECK(! is_accepted("serialization::archive_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); + BOOST_CHECK(! is_accepted("serialization")); + return EXIT_SUCCESS; +}