Skip to content
Merged
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
7 changes: 5 additions & 2 deletions src/basic_xml_grammar.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#endif

#include <cerrno> // errno
#include <cstring> // strerror(errno)
#include <cstring> // strerror(errno), strlen

// spirit stuff
#include <boost/spirit/include/classic_operators.hpp>
Expand Down Expand Up @@ -461,10 +461,13 @@ void basic_xml_grammar<CharType>::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<class CharType>
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
60 changes: 60 additions & 0 deletions test/test_xml_signature_check.cpp
Original file line number Diff line number Diff line change
@@ -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 <sstream>
#include <string>

#include <boost/archive/archive_exception.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>

#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;
}
Loading