From e6e051e7c5642ed46131978d9a98ab44fe898914 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 6 Jan 2008 07:35:32 +0000 Subject: [PATCH] improves logic to handle slightly broken .torrent files and added tests --- src/torrent_info.cpp | 19 +++++++++++++++---- test/test_primitives.cpp | 14 +++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index be06b77a0..d3c72ca70 100755 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -386,12 +386,23 @@ namespace libtorrent fs::path tmp = m_name; if (tmp.is_complete()) { - error = "torrent contains a file with an absolute path: '" + m_name + "'"; - return false; + m_name = tmp.leaf(); } - 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; } diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index 15835fe95..b860781fd 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -286,9 +286,21 @@ int test_main() torrent["info"] = info; torrent_info ti(torrent); - + std::cerr << ti.name() << std::endl; 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 using namespace libtorrent::dht;