improves logic to handle slightly broken .torrent files and added tests

This commit is contained in:
Arvid Norberg 2008-01-06 07:35:32 +00:00
parent 1c816fa608
commit e6e051e7c5
2 changed files with 28 additions and 5 deletions

View File

@ -386,12 +386,23 @@ namespace libtorrent
fs::path tmp = m_name; fs::path tmp = m_name;
if (tmp.is_complete()) if (tmp.is_complete())
{ {
error = "torrent contains a file with an absolute path: '" + m_name + "'"; m_name = tmp.leaf();
return false;
} }
if (tmp.has_branch_path()) else if (tmp.has_branch_path())
{ {
error = "torrent contains name with directories: '" + m_name + "'"; fs::path p;
for (fs::path::iterator i = tmp.begin()
, end(tmp.end()); i != end; ++i)
{
if (*i == "." || *i == "..") continue;
p /= *i;
}
m_name = p.string();
}
if (m_name == ".." || m_name == ".")
{
error = "invalid 'name' of torrent (possible exploit attempt)";
return false; return false;
} }

View File

@ -286,9 +286,21 @@ int test_main()
torrent["info"] = info; torrent["info"] = info;
torrent_info ti(torrent); torrent_info ti(torrent);
std::cerr << ti.name() << std::endl;
TEST_CHECK(ti.name() == "test1"); TEST_CHECK(ti.name() == "test1");
info["name.utf-8"] = "/test1/test2/test3";
torrent["info"] = info;
torrent_info ti2(torrent);
std::cerr << ti2.name() << std::endl;
TEST_CHECK(ti2.name() == "test3");
info["name.utf-8"] = "test2/../test3/.././../../test4";
torrent["info"] = info;
torrent_info ti3(torrent);
std::cerr << ti3.name() << std::endl;
TEST_CHECK(ti3.name() == "test2/test3/test4");
// test kademlia functions // test kademlia functions
using namespace libtorrent::dht; using namespace libtorrent::dht;