fix: Additional RPC validation checks on ammRpcInfo account and amm_account fields. (#7324)

This commit is contained in:
Timothy Banks
2026-06-24 08:23:12 -04:00
committed by GitHub
parent 69d289a388
commit bb7c4d1c9f
4 changed files with 66 additions and 8 deletions

View File

@@ -174,12 +174,22 @@ public:
ammRpcInfo(
std::optional<AccountID> const& account = std::nullopt,
std::optional<std::string> const& ledgerIndex = std::nullopt,
std::optional<Asset> asset1 = std::nullopt,
std::optional<Asset> asset2 = std::nullopt,
std::optional<Asset> const& asset1 = std::nullopt,
std::optional<Asset> const& asset2 = std::nullopt,
std::optional<AccountID> const& ammAccount = std::nullopt,
bool ignoreParams = false,
unsigned apiVersion = RPC::kApiInvalidVersion) const;
[[nodiscard]] json::Value
ammRpcInfo(
std::optional<json::Value> const& account,
std::optional<std::string> const& ledgerIndex,
std::optional<Asset> const& asset1,
std::optional<Asset> const& asset2,
std::optional<json::Value> const& ammAccount,
bool ignoreParams,
unsigned apiVersion) const;
/** Verify the AMM balances.
*/
[[nodiscard]] bool

View File

@@ -183,15 +183,37 @@ json::Value
AMM::ammRpcInfo(
std::optional<AccountID> const& account,
std::optional<std::string> const& ledgerIndex,
std::optional<Asset> asset1,
std::optional<Asset> asset2,
std::optional<Asset> const& asset1,
std::optional<Asset> const& asset2,
std::optional<AccountID> const& ammAccount,
bool ignoreParams,
unsigned apiVersion) const
{
auto const toJson = [](AccountID const& a) { return json::Value{to_string(a)}; };
return ammRpcInfo(
account.transform(toJson),
ledgerIndex,
asset1,
asset2,
ammAccount.transform(toJson),
ignoreParams,
apiVersion);
}
json::Value
AMM::ammRpcInfo(
std::optional<json::Value> const& account,
std::optional<std::string> const& ledgerIndex,
std::optional<Asset> const& asset1,
std::optional<Asset> const& asset2,
std::optional<json::Value> const& ammAccount,
bool ignoreParams,
unsigned apiVersion) const
{
json::Value jv;
if (account)
jv[jss::account] = to_string(*account);
jv[jss::account] = *account;
if (ledgerIndex)
jv[jss::ledger_index] = *ledgerIndex;
if (!ignoreParams)
@@ -209,7 +231,7 @@ AMM::ammRpcInfo(
jv[jss::asset2] = STIssue(sfAsset2, asset2_.asset()).getJson(JsonOptions::Values::None);
}
if (ammAccount)
jv[jss::amm_account] = to_string(*ammAccount);
jv[jss::amm_account] = *ammAccount;
}
auto jr =
(apiVersion == RPC::kApiInvalidVersion

View File

@@ -65,6 +65,26 @@ public:
BEAST_EXPECT(jv[jss::error_message] == "Account malformed.");
});
// Account is not a string
testAMM([&](AMM& ammAlice, Env&) {
auto const jv =
ammAlice.ammRpcInfo(json::Value{42}, std::nullopt, XRP, USD, std::nullopt, true, 3);
BEAST_EXPECT(jv[jss::error_message] == "Account malformed.");
});
// AMM Account is not a string
testAMM([&](AMM& ammAlice, Env&) {
auto const jv = ammAlice.ammRpcInfo(
json::Value{to_string(ammAlice.ammAccount())},
std::nullopt,
XRP,
USD,
json::Value{42},
false,
3);
BEAST_EXPECT(jv[jss::error_message] == "Account malformed.");
});
std::vector<std::tuple<std::optional<Issue>, std::optional<Issue>, TestAccount, bool>> const
invalidParams = {
{xrpIssue(), std::nullopt, TestAccount::None, false},

View File

@@ -120,7 +120,10 @@ doAMMInfo(RPC::JsonContext& context)
if (params.isMember(jss::amm_account))
{
auto const id = parseBase58<AccountID>((params[jss::amm_account].asString()));
auto const& ammAccount = params[jss::amm_account];
if (!ammAccount.isString())
return std::unexpected(RpcActMalformed);
auto const id = parseBase58<AccountID>(ammAccount.asString());
if (!id)
return std::unexpected(RpcActMalformed);
auto const sle = ledger->read(keylet::account(*id));
@@ -133,7 +136,10 @@ doAMMInfo(RPC::JsonContext& context)
if (params.isMember(jss::account))
{
accountID = parseBase58<AccountID>(params[jss::account].asString());
auto const& localAccount = params[jss::account];
if (!localAccount.isString())
return std::unexpected(RpcActMalformed);
accountID = parseBase58<AccountID>(localAccount.asString());
if (!accountID || !ledger->read(keylet::account(*accountID)))
return std::unexpected(RpcActMalformed);
}