Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/unkai unexpected symbol check #2731

Merged
merged 5 commits into from
Sep 13, 2022
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: 7 additions & 0 deletions irohad/ametsuchi/impl/database_cache/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ namespace iroha::ametsuchi {
drop();
}

static bool allowed(std::string_view const &key) {
for (auto c : key)
if (!Alphabet::allowed(c))
return false;
return true;
}

void addCacheblePath(std::string const &path) {
auto it = cachebleSearch(path);
auto insert = [&]() {
Expand Down
24 changes: 17 additions & 7 deletions irohad/ametsuchi/impl/rocksdb_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,12 +1024,15 @@ namespace iroha::ametsuchi {
valueBuffer().clear();
rocksdb::Slice const slice(keyBuffer().data(), keyBuffer().size());

if (auto c = cache(); c && c->isCacheable(slice.ToStringView())
&& c->get(slice.ToStringView(), [&](auto const &str) {
valueBuffer() = str;
return true;
})) {
return rocksdb::Status();
if (auto c = cache(); c && c->isCacheable(slice.ToStringView())) {
if (!DatabaseCache<std::string>::allowed(slice.ToStringView()))
return rocksdb::Status::InvalidArgument("Contains invalid symbols.");

if (c->get(slice.ToStringView(), [&](auto const &str) {
valueBuffer() = str;
return true;
}))
return rocksdb::Status();
}

rocksdb::ReadOptions ro;
Expand All @@ -1052,6 +1055,10 @@ namespace iroha::ametsuchi {
fmt::format_to(keyBuffer(), fmtstring, std::forward<Args>(args)...);

rocksdb::Slice const slice(keyBuffer().data(), keyBuffer().size());
if (auto c = cache(); c && c->isCacheable(slice.ToStringView())
&& !DatabaseCache<std::string>::allowed(slice.ToStringView()))
return rocksdb::Status::InvalidArgument("Contains invalid symbols.");

auto status =
transaction()->Put(getHandle(cf_type), slice, valueBuffer());

Expand All @@ -1070,8 +1077,11 @@ namespace iroha::ametsuchi {
fmt::format_to(keyBuffer(), fmtstring, std::forward<Args>(args)...);

rocksdb::Slice const slice(keyBuffer().data(), keyBuffer().size());
if (auto c = cache(); c && c->isCacheable(slice.ToStringView()))
if (auto c = cache(); c && c->isCacheable(slice.ToStringView())) {
if (!DatabaseCache<std::string>::allowed(slice.ToStringView()))
return rocksdb::Status::InvalidArgument("Contains invalid symbols.");
c->erase(slice.ToStringView());
}

return transaction()->Delete(getHandle(cf_type), slice);
}
Expand Down
3 changes: 2 additions & 1 deletion irohad/main/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ Irohad::Irohad(
}

Irohad::~Irohad() {
iroha_status_subscription_->unsubscribe();
if (iroha_status_subscription_)
iroha_status_subscription_->unsubscribe();

if (db_context_ && log_) {
RocksDbCommon common(db_context_);
Expand Down
14 changes: 10 additions & 4 deletions libs/common/radix_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@ namespace iroha {
*/
struct Alphabet {
static constexpr uint32_t f0 = 'z' - '_' + 1ul;
static constexpr uint32_t f1 = '9' - '.' + 1ul;
static constexpr uint32_t f1 = '9' - '-' + 1ul;
static constexpr uint32_t f2 = 'Z' - '@' + 1ul;

static uint32_t position(char d) {
assert((d >= '_' && d <= 'z') || (d >= '.' && d <= '9')
assert((d >= '_' && d <= 'z') || (d >= '-' && d <= '9')
|| (d >= '@' && d <= 'Z') || d == '#');

return ((uint32_t)d - uint32_t('_')) < f0
? uint32_t(d) - uint32_t('_')
: ((uint32_t)d - uint32_t('.')) < f1
? uint32_t(d) - uint32_t('.') + f0
: ((uint32_t)d - uint32_t('-')) < f1
? uint32_t(d) - uint32_t('-') + f0
: ((uint32_t)d - uint32_t('@')) < f2
? uint32_t(d) - uint32_t('@') + f0 + f1
: d == '#' ? f2 + f1 + f0 : (uint32_t)-1;
}

static bool allowed(char d) {
return ((uint32_t)d - uint32_t('_')) < f0
|| ((uint32_t)d - uint32_t('-')) < f1
|| ((uint32_t)d - uint32_t('@')) < f2 || d == '#';
}

static constexpr uint32_t size() {
return f2 + f1 + f0 + 1ul;
}
Expand Down