#include #include "libslic3r/Config.hpp" #include "libslic3r/Format/AMF.hpp" #include "libslic3r/Model.hpp" #include "libslic3r/PrintConfig.hpp" #include "test_utils.hpp" #include #include #include using namespace Slic3r; namespace { // The smallest AMF the loader accepts: one object holding one volume, a tetrahedron. The metadata // element of the object is dropped in verbatim, so a test can hand the parser a malformed one. std::string amf_with_object_metadata(const std::string &object_metadata) { return "\n" "\n" " \n" " " + object_metadata + "\n" " \n" " \n" " 000\n" " 100\n" " 010\n" " 001\n" " \n" " \n" " 021\n" " 013\n" " 032\n" " 123\n" " \n" " \n" " \n" "\n"; } void write_file(const std::string &path, const std::string &content) { boost::nowide::ofstream f(path, std::ios::binary); f << content; } bool load(const std::string &path, Model &model) { DynamicPrintConfig config; ConfigSubstitutionContext substitutions(ForwardCompatibilitySubstitutionRule::Disable); return load_amf(path.c_str(), &config, &substitutions, &model, nullptr); } } // namespace TEST_CASE("An AMF object metadata element with no type attribute is rejected", "[AMF]") { ScopedTemporaryFile tmp(".amf"); SECTION("with the attribute the file loads") { write_file(tmp.string(), amf_with_object_metadata("tetra")); Model model; REQUIRE(load(tmp.string(), model)); CHECK(model.objects.size() == 1); } SECTION("without it the load fails instead of reading a null attribute") { write_file(tmp.string(), amf_with_object_metadata("tetra")); Model model; CHECK_FALSE(load(tmp.string(), model)); } } namespace { // An internal DTD subset has to precede the root element, so splice it in ahead of the root tag. std::string amf_with_dtd(const std::string &declarations, const std::string &object_metadata) { std::string xml = amf_with_object_metadata(object_metadata); xml.insert(xml.find("\n"); return xml; } } // namespace TEST_CASE("AMF rejects an XML entity amplification bomb", "[AMF][Security]") { ScopedTemporaryFile tmp(".amf"); // Seven levels expand to 10^7 characters, past expat's 8 MiB output and 100x amplification thresholds, yet a // build without the guard still parses that in well under a second instead of exhausting CI memory as ten would. std::string declarations = ""; for (int level = 2; level <= 7; ++level) { std::string body; for (int repeat = 0; repeat < 10; ++repeat) body += "&e" + std::to_string(level - 1) + ";"; declarations += "\n"; } write_file(tmp.string(), amf_with_dtd(declarations, "&e7;")); Model model; CHECK_FALSE(load(tmp.string(), model)); } TEST_CASE("AMF still expands an ordinary internal entity", "[AMF][Security]") { // The amplification cap must not turn entity support off: ordinary AMF files keep parsing. ScopedTemporaryFile tmp(".amf"); write_file(tmp.string(), amf_with_dtd("", "&name;")); Model model; REQUIRE(load(tmp.string(), model)); REQUIRE(model.objects.size() == 1); CHECK(model.objects.front()->name == "hello"); }