Skip to content

Commit

Permalink
Fixes #85
Browse files Browse the repository at this point in the history
Removing code for
absolute symlinks
which point outside root.
  • Loading branch information
John-LittleBearLabs committed Dec 19, 2023
1 parent c4632b8 commit 39cc8cb
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 238 deletions.
2 changes: 2 additions & 0 deletions library/include/ipfs_client/multi_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace ipfs {
enum class HashType { INVALID = -1, IDENTITY = 0, SHA2_256 = 0X12 };
constexpr std::uint16_t MaximumHashLength = 127;

HashType Validate(HashType);
std::string_view GetName(HashType);
class MultiHash {
Expand Down
46 changes: 0 additions & 46 deletions library/include/libp2p/multi/content_identifier.hpp

This file was deleted.

99 changes: 0 additions & 99 deletions library/include/libp2p/multi/content_identifier_codec.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion library/src/ipfs_client/cid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Self::Cid(std::string_view s) {
assign(bytes.value());
}
} else {
LOG(ERROR) << "Failed to decode the multibase for a CID: " << s;
LOG(WARNING) << "Failed to decode the multibase for a CID: " << s;
}
}

Expand Down
2 changes: 1 addition & 1 deletion library/src/ipfs_client/dag_block_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ TEST(BlockTest, TypeNames) {
EXPECT_EQ(Stringify(T::FileChunk), "FileChunk");
EXPECT_EQ(Stringify(T::NonFs), "NonFs");
EXPECT_EQ(Stringify(T::Invalid), "Invalid");
}
}
5 changes: 1 addition & 4 deletions library/src/ipfs_client/identity_cid.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include <ipfs_client/identity_cid.h>

#include <libp2p/multi/multihash.hpp>

#include <log_macros.h>

namespace Self = ipfs::id_cid;
namespace m = libp2p::multi;

auto Self::forText(std::string_view txt) -> Cid {
txt = txt.substr(0UL, m::Multihash::kMaxHashLength);
txt = txt.substr(0UL, MaximumHashLength);
auto p = reinterpret_cast<Byte const*>(txt.data());
auto b = ByteView{p, txt.size()};
MultiHash mh(HashType::IDENTITY, b);
Expand Down
61 changes: 13 additions & 48 deletions library/src/ipfs_client/ipld/symlink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,24 @@

using Self = ipfs::ipld::Symlink;

Self::Symlink(std::string target)
: style_{from_target(target)}, target_{target} {}
Self::Symlink(std::string target) : target_{target} {}

Self::~Symlink() {}

auto Self::resolve(SlashDelimited path, BlockLookup, std::string& to_here)
-> ResolveResult {
std::string result;
switch (style_) {
case Style::Absolute:
result.assign(target_);
LOG(INFO) << "Absolute symlink '" << target_ << "' ...";
break;
case Style::Relative: {
auto c = to_here.find_last_not_of('/');
c = to_here.find_last_of('/', c);
DCHECK(c != to_here.size()) << to_here;
if (is_absolute()) {
result.assign(SlashDelimited{to_here}.pop_n(2)).append("/").append(target_);
LOG(INFO) << "Absolute (relative-to-dag-root) symlink '" << target_
<< "' leads to '" << result << "' ... ";
} else {
auto c = to_here.find_last_not_of('/');
c = to_here.find_last_of('/', c);
DCHECK(c != to_here.size()) << to_here;
result.assign(to_here, 0, c + 1).append(target_);
LOG(INFO) << "Relative symlink '" << target_ << "' leads to '" << result
<< "' ... ";
} break;
case Style::FromRoot:
result.assign(SlashDelimited{to_here}.pop_n(2))
.append("/")
.append(target_);
LOG(INFO) << "Relative-to-root symlink '" << target_ << "' leads to '"
<< result << "' ... ";
}
if (path) {
result.append("/").append(path.pop_all());
Expand All @@ -43,36 +34,10 @@ auto Self::resolve(SlashDelimited path, BlockLookup, std::string& to_here)
if (result.back() == '/') {
result.resize(result.size() - 1);
}
LOG(WARNING) << "symlink: '" << to_here << "' -> '" << result << "'.";
LOG(INFO) << "symlink: '" << to_here << "' -> '" << result << "'.";
return PathChange{result};
}

auto Self::from_target(std::string const& target) -> Style {
DCHECK_GT(target.size(), 0U);
if (target.at(0) != '/') {
return Style::Relative;
}
SlashDelimited t{target};
auto ns = t.pop();
if (ns != "ipfs" && ns != "ipns") {
return Style::FromRoot;
}
auto root = t.pop();
using namespace libp2p::multi;
auto cid = Cid(root);
if (!cid.valid()) {
if (ns == "ipns") {
LOG(WARNING) << "Symlink to DNSLink is highly irregular.";
return Style::Absolute;
} else {
LOG(WARNING) << "Someone has a subdirectory of their DAG root that is "
"called /ipfs/ ? Who does that?";
return Style::FromRoot;
}
}
if (cid.codec() == MultiCodec::LIBP2P_KEY) {
return ns == "ipns" ? Style::Absolute : Style::FromRoot;
} else {
return ns == "ipfs" ? Style::Absolute : Style::FromRoot;
}
}
bool Self::is_absolute() const {
return target_.at(0) == '/';
}
9 changes: 2 additions & 7 deletions library/src/ipfs_client/ipld/symlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@

namespace ipfs::ipld {
class Symlink : public DagNode {
enum class Style {
Relative,
Absolute,
FromRoot,
};
Style const style_;
std::string const target_;

Style from_target(std::string const&);
ResolveResult resolve(SlashDelimited path,
BlockLookup,
std::string& up_to_here) override;

bool is_absolute() const;

public:
Symlink(std::string target);
~Symlink() noexcept override;
Expand Down
32 changes: 0 additions & 32 deletions library/src/ipfs_client/ipld/symlink_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,6 @@ TEST(SymlinkTest, fromBlock) {
"a/d/e";
EXPECT_EQ(actual.new_path, expect);
}
TEST(SymlinkTest, absolute) {
ii::Symlink sub{
"/ipfs/bafkreih6wk34z7qlwe2jj4ynowkh4ux6pbqnijcebcz7sd4fz3rnigxa6u"};
ii::DagNode& t = sub;
auto blu = [](std::string const&) -> ii::NodePtr {
throw std::runtime_error{"Block lookup not expected."};
};
std::string observed_path_to_link{"/ipns/"};
observed_path_to_link
.append("k51qzi5uqu5dkq4jxcqvujfm2woh4p9y6inrojofxflzdnfht168zf8ynfzuu1")
.append("symlinks/absolute_link.txt");
auto res = t.resolve(""sv, blu, observed_path_to_link);
EXPECT_TRUE(std::holds_alternative<ii::PathChange>(res));
EXPECT_EQ(
std::get<ii::PathChange>(res).new_path,
"/ipfs/bafkreih6wk34z7qlwe2jj4ynowkh4ux6pbqnijcebcz7sd4fz3rnigxa6u");
}
TEST(SymlinkTest, rooted) {
ii::Symlink sub{"/another/path.txt"};
ii::DagNode& t = sub;
Expand All @@ -61,18 +44,3 @@ TEST(SymlinkTest, rooted) {
"/ipns/k51qzi5uqu5dkq4jxcqvujfm2woh4p9y6inrojofxflzdnfht168zf8ynfzuu1/"
"another/path.txt");
}
TEST(SymlinkTest, ipns_absolute) {
ii::Symlink sub{"/ipns/en.wikipedia-on-ipfs.org"};
ii::DagNode& t = sub;
auto blu = [](std::string const&) -> ii::NodePtr {
throw std::runtime_error{"Block lookup not expected."};
};
std::string observed_path_to_link{"/ipns/"};
observed_path_to_link
.append("k51qzi5uqu5dkq4jxcqvujfm2woh4p9y6inrojofxflzdnfht168zf8ynfzuu1")
.append("symlinks/absolute_link.txt");
auto res = t.resolve(""sv, blu, observed_path_to_link);
EXPECT_TRUE(std::holds_alternative<ii::PathChange>(res));
EXPECT_EQ(std::get<ii::PathChange>(res).new_path,
"/ipns/en.wikipedia-on-ipfs.org");
}

0 comments on commit 39cc8cb

Please sign in to comment.