-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add NetworkID field to transactions to help prevent replay attacks on and from side-chains #4370
Changes from 3 commits
3af76ce
437ad33
368589c
3f0336b
a02b3bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,7 @@ CONSTRUCT_TYPED_SFIELD(sfHookExecutionIndex, "HookExecutionIndex", UINT16, | |
CONSTRUCT_TYPED_SFIELD(sfHookApiVersion, "HookApiVersion", UINT16, 20); | ||
|
||
// 32-bit integers (common) | ||
CONSTRUCT_TYPED_SFIELD(sfNetworkID, "NetworkID", UINT32, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would not resue the unused value unless you are confidente that its not used elsewhere. Also why use the ''common'' value? To save a one byte in the encoding version? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’ve never seen UINT32 field code 1 used anywhere. Not even in old old ledgers. Maybe someone can shed light on why it was free? |
||
CONSTRUCT_TYPED_SFIELD(sfFlags, "Flags", UINT32, 2); | ||
CONSTRUCT_TYPED_SFIELD(sfSourceTag, "SourceTag", UINT32, 3); | ||
CONSTRUCT_TYPED_SFIELD(sfSequence, "Sequence", UINT32, 4); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
//------------------------------------------------------------------------------ | ||
/* | ||
This file is part of rippled: https://github.com/ripple/rippled | ||
Copyright (c) 2020 Dev Null Productions | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
//============================================================================== | ||
|
||
#include <ripple/basics/BasicConfig.h> | ||
#include <ripple/core/ConfigSections.h> | ||
#include <test/jtx.h> | ||
RichardAH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <test/jtx/Env.h> | ||
#include <ripple/protocol/jss.h> | ||
|
||
namespace ripple { | ||
namespace test { | ||
|
||
class NetworkID_test : public beast::unit_test::suite | ||
{ | ||
public: | ||
void | ||
run() override | ||
{ | ||
testNetworkID(); | ||
} | ||
|
||
std::unique_ptr<Config> | ||
makeNetworkConfig(uint32_t networkID) | ||
{ | ||
using namespace jtx; | ||
return envconfig([&](std::unique_ptr<Config> cfg) { | ||
cfg->NETWORK_ID = networkID; | ||
return cfg; | ||
}); | ||
} | ||
|
||
void | ||
testNetworkID() | ||
{ | ||
testcase( | ||
"Require txn NetworkID to be specified (or not) depending on the " | ||
"network ID of the node"); | ||
using namespace jtx; | ||
|
||
auto const alice = Account{"alice"}; | ||
|
||
auto const runTx = [&](test::jtx::Env& env, | ||
Json::Value const& jv, | ||
TER expectedOutcome) { | ||
env.memoize(env.master); | ||
env.memoize(alice); | ||
|
||
// fund alice | ||
{ | ||
Json::Value jv; | ||
jv[jss::Account] = env.master.human(); | ||
jv[jss::Destination] = alice.human(); | ||
jv[jss::TransactionType] = "Payment"; | ||
jv[jss::Amount] = "10000000000"; | ||
if (env.app().config().NETWORK_ID > 1024) | ||
jv[jss::NetworkID] = | ||
std::to_string(env.app().config().NETWORK_ID); | ||
|
||
env(jv, fee(1000), sig(env.master)); | ||
} | ||
|
||
// run tx | ||
env(jv, fee(1000), ter(expectedOutcome)); | ||
env.close(); | ||
}; | ||
|
||
// test mainnet | ||
{ | ||
test::jtx::Env env{*this, makeNetworkConfig(0)}; | ||
BEAST_EXPECT(env.app().config().NETWORK_ID == 0); | ||
|
||
// try to submit a txn without network id, this should work | ||
Json::Value jv; | ||
jv[jss::Account] = alice.human(); | ||
jv[jss::TransactionType] = jss::AccountSet; | ||
runTx(env, jv, tesSUCCESS); | ||
|
||
// try to submit a txn with NetworkID present against a mainnet | ||
// node, this will fail | ||
jv[jss::NetworkID] = 0; | ||
runTx(env, jv, telNETWORK_ID_MAKES_TX_NON_CANONICAL); | ||
|
||
// change network id to something else, should still return same | ||
// error | ||
jv[jss::NetworkID] = 10000; | ||
runTx(env, jv, telNETWORK_ID_MAKES_TX_NON_CANONICAL); | ||
} | ||
|
||
// any network up to and including networkid 1024 cannot support | ||
// NetworkID | ||
{ | ||
test::jtx::Env env{*this, makeNetworkConfig(1024)}; | ||
BEAST_EXPECT(env.app().config().NETWORK_ID == 1024); | ||
|
||
// try to submit a txn without network id, this should work | ||
Json::Value jv; | ||
jv[jss::Account] = alice.human(); | ||
jv[jss::TransactionType] = jss::AccountSet; | ||
runTx(env, jv, tesSUCCESS); | ||
|
||
// now submit with a network id, this will fail | ||
jv[jss::NetworkID] = 1024; | ||
runTx(env, jv, telNETWORK_ID_MAKES_TX_NON_CANONICAL); | ||
|
||
jv[jss::NetworkID] = 1000; | ||
runTx(env, jv, telNETWORK_ID_MAKES_TX_NON_CANONICAL); | ||
} | ||
|
||
// any network above networkid 1024 will produce an error if fed a txn | ||
// absent networkid | ||
{ | ||
test::jtx::Env env{*this, makeNetworkConfig(1025)}; | ||
BEAST_EXPECT(env.app().config().NETWORK_ID == 1025); | ||
|
||
// try to submit a txn without network id, this should not work | ||
Json::Value jv; | ||
jv[jss::Account] = alice.human(); | ||
jv[jss::TransactionType] = jss::AccountSet; | ||
runTx(env, jv, telREQUIRES_NETWORK_ID); | ||
|
||
// try to submit with wrong network id | ||
jv[jss::NetworkID] = 0; | ||
runTx(env, jv, telWRONG_NETWORK); | ||
|
||
jv[jss::NetworkID] = 1024; | ||
runTx(env, jv, telWRONG_NETWORK); | ||
|
||
// submit the correct network id | ||
jv[jss::NetworkID] = 1025; | ||
runTx(env, jv, tesSUCCESS); | ||
} | ||
} | ||
}; | ||
|
||
BEAST_DEFINE_TESTSUITE(NetworkID, app, ripple); | ||
|
||
} // namespace test | ||
} // namespace ripple |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -411,6 +411,71 @@ port_wss_admin | |
} | ||
} | ||
|
||
void | ||
testNetworkID() | ||
{ | ||
testcase("network id"); | ||
std::string error; | ||
Config c; | ||
try | ||
{ | ||
c.loadFromString(R"rippleConfig( | ||
[network_id] | ||
main | ||
)rippleConfig"); | ||
} | ||
catch (std::runtime_error& e) | ||
{ | ||
error = e.what(); | ||
} | ||
|
||
BEAST_EXPECT(error == ""); | ||
BEAST_EXPECT(c.NETWORK_ID == 0); | ||
|
||
try | ||
{ | ||
c.loadFromString(R"rippleConfig( | ||
)rippleConfig"); | ||
} | ||
catch (std::runtime_error& e) | ||
{ | ||
error = e.what(); | ||
} | ||
|
||
BEAST_EXPECT(error == ""); | ||
BEAST_EXPECT(c.NETWORK_ID == 0); | ||
|
||
try | ||
{ | ||
c.loadFromString(R"rippleConfig( | ||
[network_id] | ||
255 | ||
)rippleConfig"); | ||
} | ||
catch (std::runtime_error& e) | ||
{ | ||
error = e.what(); | ||
} | ||
|
||
BEAST_EXPECT(error == ""); | ||
BEAST_EXPECT(c.NETWORK_ID == 255); | ||
|
||
try | ||
{ | ||
c.loadFromString(R"rippleConfig( | ||
[network_id] | ||
10000 | ||
)rippleConfig"); | ||
} | ||
catch (std::runtime_error& e) | ||
{ | ||
error = e.what(); | ||
} | ||
|
||
BEAST_EXPECT(error == ""); | ||
BEAST_EXPECT(c.NETWORK_ID == 10000); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please consider adding a negative case, e.g. |
||
|
||
void | ||
testValidatorsFile() | ||
{ | ||
|
@@ -1151,6 +1216,7 @@ r.ripple.com 51235 | |
testGetters(); | ||
testAmendment(); | ||
testOverlay(); | ||
testNetworkID(); | ||
} | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,7 +163,10 @@ Env::lookup(AccountID const& id) const | |
{ | ||
auto const iter = map_.find(id); | ||
if (iter == map_.end()) | ||
{ | ||
std::cout << "Unknown account: " << id << "\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please consider to remove. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How? |
||
Throw<std::runtime_error>("Env::lookup:: unknown account ID"); | ||
} | ||
return iter->second; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed this section is similar to the
OverlayImpl.cpp
version; is that something that can be updated to be consistent or shared in some way so this switch doesn't have to be in multiple places?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also saw this but didn't want to touch OverlayImpl. Feel free to suggest an additional commit with appropriate tests to remove this redundant code.