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 incorrect parsing of optional JSON RPC params (#1703) #1771

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 12 additions & 11 deletions core/api/service/child_state/requests/get_keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace kagome::api::child_state::request {
// childKey
auto &param0 = params[0];

if (not param0.IsString() or param0.IsNil()) {
if (not param0.IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[child_storage_key]' must be a hex string");
}
Expand All @@ -29,7 +29,7 @@ namespace kagome::api::child_state::request {

auto &param1 = params[1];

if (not param1.IsString() and not param1.IsNil()) {
if (not param1.IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[prefix]' must be a hex string");
}
Expand All @@ -48,16 +48,17 @@ namespace kagome::api::child_state::request {
return outcome::success();
}

// process at param
if (not params[2].IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[at]' must be a hex string representation of an encoded "
"optional byte sequence");
if (not params[2].IsNil()) {
// process at param
if (not params[2].IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[at]' must be a hex string representation of an "
"encoded optional byte sequence");
}
OUTCOME_TRY(at_span, common::unhexWith0x(params[2].AsString()));
OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
at_ = at;
}
OUTCOME_TRY(at_span, common::unhexWith0x(params[2].AsString()));
OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
at_ = at;

return outcome::success();
}

Expand Down
22 changes: 12 additions & 10 deletions core/api/service/child_state/requests/get_keys_paged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace kagome::api::child_state::request {
// childKey
auto &param0 = params[0];

if (not param0.IsString() or param0.IsNil()) {
if (not param0.IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[child_storage_key]' must be a hex string");
}
Expand All @@ -29,7 +29,7 @@ namespace kagome::api::child_state::request {

auto &param1 = params[1];

if (not param1.IsString() and not param1.IsNil()) {
if (not param1.IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[prefix]' must be a hex string");
}
Expand Down Expand Up @@ -69,15 +69,17 @@ namespace kagome::api::child_state::request {
return outcome::success();
}

// process at param
if (not params[4].IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[at]' must be a hex string representation of an encoded "
"optional byte sequence");
if (not params[4].IsNil()) {
// process at param
if (not params[4].IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[at]' must be a hex string representation of an encoded "
"optional byte sequence");
}
OUTCOME_TRY(at_span, common::unhexWith0x(params[4].AsString()));
OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
at_ = at;
}
OUTCOME_TRY(at_span, common::unhexWith0x(params[4].AsString()));
OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
at_ = at;

return outcome::success();
}
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/child_state/requests/get_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace kagome::api::child_state::request {
}
auto &param0 = params[0];

if (not param0.IsString() or param0.IsNil()) {
if (not param0.IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[child_storage_key]' must be a hex string");
}
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/child_state/requests/get_storage_size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace kagome::api::child_state::request {
}
auto &param0 = params[0];

if (not param0.IsString() or param0.IsNil()) {
if (not param0.IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[child_storage_key]' must be a hex string");
}
Expand Down
25 changes: 14 additions & 11 deletions core/api/service/state/requests/call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ namespace kagome::api::state::request {
};

outcome::result<void> Call::init(const jsonrpc::Request::Parameters &params) {
if (params.size() > 3 or params.size() < 2) {
if (params.size() < 2 or params.size() > 3) {
throw jsonrpc::InvalidParametersFault("Incorrect number of params");
}
auto &param0 = params[0];

if (param0.IsString() and not param0.IsNil()) {
if (param0.IsString()) {
method_ = param0.AsString();
} else {
throw jsonrpc::InvalidParametersFault(
"Parameter '[method]' must be a string");
}

auto &param1 = params[1];
if (param1.IsString() and not param1.IsNil()) {
if (param1.IsString()) {
auto encoded_args = common::unhexWith0x(param1.AsString());
if (encoded_args.has_value()) {
data_ = common::Buffer(encoded_args.value());
Expand All @@ -45,15 +45,18 @@ namespace kagome::api::state::request {
}

auto &param2 = params[2];
// process at param
if (not param2.IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[at]' must be a hex string representation of an encoded "
"optional byte sequence");
if (!param2.IsNil()) {
// process at param
if (not param2.IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[at]' must be a hex string representation of an "
"encoded "
"optional byte sequence");
Harrm marked this conversation as resolved.
Show resolved Hide resolved
}
OUTCOME_TRY(at_span, common::unhexWith0x(param2.AsString()));
OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
at_ = at;
}
OUTCOME_TRY(at_span, common::unhexWith0x(param2.AsString()));
OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
at_ = at;

return outcome::success();
}
Expand Down
23 changes: 12 additions & 11 deletions core/api/service/state/requests/get_keys_paged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace kagome::api::state::request {

outcome::result<void> GetKeysPaged::init(
const jsonrpc::Request::Parameters &params) {
if (params.size() > 4 or params.size() <= 1) {
if (params.size() > 4 or params.size() < 2) {
throw jsonrpc::InvalidParametersFault("Incorrect number of params");
}
auto &param0 = params[0];

if (not param0.IsString() and not param0.IsNil()) {
if (not param0.IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[prefix]' must be a hex string");
}
Expand All @@ -42,7 +42,6 @@ namespace kagome::api::state::request {
return outcome::success();
}

// process prev_key param
if (not params[2].IsNil()) {
if (not params[2].IsString()) {
throw jsonrpc::InvalidParametersFault(
Expand All @@ -57,15 +56,17 @@ namespace kagome::api::state::request {
return outcome::success();
}

// process at param
if (not params[3].IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[at]' must be a hex string representation of an encoded "
"optional byte sequence");
if (not params[3].IsNil()) {
// process at param
if (not params[3].IsString()) {
throw jsonrpc::InvalidParametersFault(
"Parameter '[at]' must be a hex string representation of an encoded "
"optional byte sequence");
}
OUTCOME_TRY(at_span, common::unhexWith0x(params[3].AsString()));
OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
at_ = at;
}
OUTCOME_TRY(at_span, common::unhexWith0x(params[3].AsString()));
OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
at_ = at;

return outcome::success();
}
Expand Down
4 changes: 2 additions & 2 deletions core/api/service/state/requests/subscribe_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace kagome::api::state::request {

outcome::result<void> SubscribeStorage::init(
const jsonrpc::Request::Parameters &params) {
if (params.size() > 1 or params.empty()) {
throw jsonrpc::InvalidParametersFault("Incorrect number of params");
if (params.size() != 1) {
throw jsonrpc::InvalidParametersFault("subscribeStorage takes one parameter");
}
auto &keys = params[0];
if (!keys.IsArray()) {
Expand Down
4 changes: 2 additions & 2 deletions core/api/service/state/requests/unsubscribe_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace kagome::api::state::request {

outcome::result<void> UnsubscribeStorage::init(
const jsonrpc::Request::Parameters &params) {
if (params.size() > 1 or params.empty()) {
throw jsonrpc::InvalidParametersFault("Incorrect number of params");
if (params.size() != 1) {
throw jsonrpc::InvalidParametersFault("unsubscribeStorage takes one parameter");
}

auto &id = params[0];
Expand Down
Loading