Skip to content

Commit 40be25a

Browse files
committed
fix: Add upper bound to limit
1 parent 9fc8846 commit 40be25a

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

src/rpc/handlers/AccountTx.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#include <xrpl/protocol/jss.h>
4040

4141
#include <cstdint>
42-
#include <limits>
4342
#include <memory>
4443
#include <optional>
4544
#include <string>
@@ -57,8 +56,8 @@ class AccountTxHandler {
5756
std::shared_ptr<BackendInterface> sharedPtrBackend_;
5857

5958
public:
60-
// no max limit
6159
static auto constexpr LIMIT_MIN = 1;
60+
static auto constexpr LIMIT_MAX = 1000;
6261
static auto constexpr LIMIT_DEFAULT = 200;
6362

6463
/**
@@ -133,7 +132,7 @@ class AccountTxHandler {
133132
{JS(limit),
134133
validation::Type<uint32_t>{},
135134
validation::Min(1u),
136-
modifiers::Clamp<int32_t>{LIMIT_MIN, std::numeric_limits<int32_t>::max()}},
135+
modifiers::Clamp<int32_t>{LIMIT_MIN, LIMIT_MAX}},
137136
{JS(marker),
138137
meta::WithCustomError{
139138
validation::Type<boost::json::object>{},

tests/unit/rpc/handlers/AccountTxTests.cpp

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,14 +769,13 @@ TEST_F(RPCAccountTxHandlerTest, LimitAndMarker)
769769

770770
auto const transactions = genTransactions(MINSEQ + 1, MAXSEQ - 1);
771771
auto const transCursor = TransactionsAndCursor{transactions, TransactionsCursor{12, 34}};
772-
ON_CALL(*backend, fetchAccountTransactions).WillByDefault(Return(transCursor));
773772
EXPECT_CALL(
774773
*backend,
775774
fetchAccountTransactions(
776775
testing::_, testing::_, false, testing::Optional(testing::Eq(TransactionsCursor{10, 11})), testing::_
777776
)
778777
)
779-
.Times(1);
778+
.WillOnce(Return(transCursor));
780779

781780
runSpawn([&, this](auto yield) {
782781
auto const handler = AnyHandler{AccountTxHandler{backend}};
@@ -804,6 +803,73 @@ TEST_F(RPCAccountTxHandlerTest, LimitAndMarker)
804803
});
805804
}
806805

806+
TEST_F(RPCAccountTxHandlerTest, LimitIsCapped)
807+
{
808+
backend->setRange(MINSEQ, MAXSEQ);
809+
810+
auto const transactions = genTransactions(MINSEQ + 1, MAXSEQ - 1);
811+
auto const transCursor = TransactionsAndCursor{transactions, TransactionsCursor{12, 34}};
812+
EXPECT_CALL(*backend, fetchAccountTransactions(testing::_, testing::_, false, testing::_, testing::_))
813+
.WillOnce(Return(transCursor));
814+
815+
runSpawn([&, this](auto yield) {
816+
auto const handler = AnyHandler{AccountTxHandler{backend}};
817+
auto static const input = json::parse(fmt::format(
818+
R"({{
819+
"account": "{}",
820+
"ledger_index_min": {},
821+
"ledger_index_max": {},
822+
"limit": 100000,
823+
"forward": false
824+
}})",
825+
ACCOUNT,
826+
-1,
827+
-1
828+
));
829+
auto const output = handler.process(input, Context{yield});
830+
ASSERT_TRUE(output);
831+
EXPECT_EQ(output.result->at("account").as_string(), ACCOUNT);
832+
EXPECT_EQ(output.result->at("ledger_index_min").as_uint64(), MINSEQ);
833+
EXPECT_EQ(output.result->at("ledger_index_max").as_uint64(), MAXSEQ);
834+
EXPECT_EQ(output.result->at("limit").as_uint64(), AccountTxHandler::LIMIT_MAX);
835+
EXPECT_EQ(output.result->at("transactions").as_array().size(), 2);
836+
});
837+
}
838+
839+
TEST_F(RPCAccountTxHandlerTest, LimitAllowedUpToCap)
840+
{
841+
backend->setRange(MINSEQ, MAXSEQ);
842+
843+
auto const transactions = genTransactions(MINSEQ + 1, MAXSEQ - 1);
844+
auto const transCursor = TransactionsAndCursor{transactions, TransactionsCursor{12, 34}};
845+
EXPECT_CALL(*backend, fetchAccountTransactions(testing::_, testing::_, false, testing::_, testing::_))
846+
.WillOnce(Return(transCursor));
847+
848+
runSpawn([&, this](auto yield) {
849+
auto const handler = AnyHandler{AccountTxHandler{backend}};
850+
auto static const input = json::parse(fmt::format(
851+
R"({{
852+
"account": "{}",
853+
"ledger_index_min": {},
854+
"ledger_index_max": {},
855+
"limit": {},
856+
"forward": false
857+
}})",
858+
ACCOUNT,
859+
-1,
860+
-1,
861+
AccountTxHandler::LIMIT_MAX - 1
862+
));
863+
auto const output = handler.process(input, Context{yield});
864+
ASSERT_TRUE(output);
865+
EXPECT_EQ(output.result->at("account").as_string(), ACCOUNT);
866+
EXPECT_EQ(output.result->at("ledger_index_min").as_uint64(), MINSEQ);
867+
EXPECT_EQ(output.result->at("ledger_index_max").as_uint64(), MAXSEQ);
868+
EXPECT_EQ(output.result->at("limit").as_uint64(), AccountTxHandler::LIMIT_MAX - 1);
869+
EXPECT_EQ(output.result->at("transactions").as_array().size(), 2);
870+
});
871+
}
872+
807873
TEST_F(RPCAccountTxHandlerTest, SpecificLedgerIndex)
808874
{
809875
backend->setRange(MINSEQ, MAXSEQ);

0 commit comments

Comments
 (0)