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

feat(world): prevent the World from calling itself #1563

Merged
merged 19 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
8 changes: 8 additions & 0 deletions .changeset/brown-garlics-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@latticexyz/world": major
---
All `World` methods now revert if the `World` calls itself.
The `World` should never need to call itself, since all internal table operations are done as library calls, and all root system operations are done as delegate call to the system contract.
It should not be possible to make the `World` call itself as an external actor.
If it were possible to make the `World` call itself, it would be possible to write to internal tables that only the `World` should have access to.
As this is a very important invariance, we decided to codify it in an explicit requirement check in every `World` method, instead of just relying on making it impossible to trigger the `World` to call itself.
106 changes: 50 additions & 56 deletions packages/world/gas-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,103 +3,97 @@
"file": "test/AccessControl.t.sol",
"test": "testAccessControl",
"name": "AccessControl: hasAccess (cold)",
"gasUsed": 7178
"gasUsed": 7134
},
{
"file": "test/AccessControl.t.sol",
"test": "testAccessControl",
"name": "AccessControl: hasAccess (warm, namespace only)",
"gasUsed": 1726
"gasUsed": 1682
},
{
"file": "test/AccessControl.t.sol",
"test": "testAccessControl",
"name": "AccessControl: hasAccess (warm)",
"gasUsed": 3186
"gasUsed": 3142
},
{
"file": "test/AccessControl.t.sol",
"test": "testRequireAccess",
"name": "AccessControl: requireAccess (cold)",
"gasUsed": 7221
"gasUsed": 7177
},
{
"file": "test/AccessControl.t.sol",
"test": "testRequireAccess",
"name": "AccessControl: requireAccess (warm)",
"gasUsed": 3224
},
{
"file": "test/AccessControl.t.sol",
"test": "testRequireAccess",
"name": "AccessControl: requireAccess (this address)",
"gasUsed": 153
"gasUsed": 3180
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testInstallComposite",
"name": "install keys in table module",
"gasUsed": 1414653
"gasUsed": 1414785
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testInstallGas",
"name": "install keys in table module",
"gasUsed": 1414653
"gasUsed": 1414785
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testInstallGas",
"name": "set a record on a table with keysInTableModule installed",
"gasUsed": 158293
"gasUsed": 158224
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testInstallSingleton",
"name": "install keys in table module",
"gasUsed": 1414653
"gasUsed": 1414785
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testSetAndDeleteRecordHookCompositeGas",
"name": "install keys in table module",
"gasUsed": 1414653
"gasUsed": 1414785
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testSetAndDeleteRecordHookCompositeGas",
"name": "change a composite record on a table with keysInTableModule installed",
"gasUsed": 22265
"gasUsed": 22242
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testSetAndDeleteRecordHookCompositeGas",
"name": "delete a composite record on a table with keysInTableModule installed",
"gasUsed": 160800
"gasUsed": 160593
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testSetAndDeleteRecordHookGas",
"name": "install keys in table module",
"gasUsed": 1414653
"gasUsed": 1414785
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testSetAndDeleteRecordHookGas",
"name": "change a record on a table with keysInTableModule installed",
"gasUsed": 20987
"gasUsed": 20964
},
{
"file": "test/KeysInTableModule.t.sol",
"test": "testSetAndDeleteRecordHookGas",
"name": "delete a record on a table with keysInTableModule installed",
"gasUsed": 85824
"gasUsed": 85709
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testGetKeysWithValueGas",
"name": "install keys with value module",
"gasUsed": 665378
"gasUsed": 654666
},
{
"file": "test/KeysWithValueModule.t.sol",
Expand All @@ -117,49 +111,49 @@
"file": "test/KeysWithValueModule.t.sol",
"test": "testInstall",
"name": "install keys with value module",
"gasUsed": 665378
"gasUsed": 654666
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testInstall",
"name": "set a record on a table with KeysWithValueModule installed",
"gasUsed": 135704
"gasUsed": 135635
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testSetAndDeleteRecordHook",
"name": "install keys with value module",
"gasUsed": 665378
"gasUsed": 654666
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testSetAndDeleteRecordHook",
"name": "change a record on a table with KeysWithValueModule installed",
"gasUsed": 106045
"gasUsed": 105976
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testSetAndDeleteRecordHook",
"name": "delete a record on a table with KeysWithValueModule installed",
"gasUsed": 35455
"gasUsed": 35409
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testSetField",
"name": "install keys with value module",
"gasUsed": 665378
"gasUsed": 654666
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testSetField",
"name": "set a field on a table with KeysWithValueModule installed",
"gasUsed": 148453
"gasUsed": 148384
},
{
"file": "test/KeysWithValueModule.t.sol",
"test": "testSetField",
"name": "change a field on a table with KeysWithValueModule installed",
"gasUsed": 113212
"gasUsed": 113143
},
{
"file": "test/query.t.sol",
Expand Down Expand Up @@ -231,157 +225,157 @@
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromCallboundDelegation",
"name": "register a callbound delegation",
"gasUsed": 114589
"gasUsed": 114610
},
{
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromCallboundDelegation",
"name": "call a system via a callbound delegation",
"gasUsed": 33993
"gasUsed": 34014
},
{
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromTimeboundDelegation",
"name": "register a timebound delegation",
"gasUsed": 109084
"gasUsed": 109105
},
{
"file": "test/StandardDelegationsModule.t.sol",
"test": "testCallFromTimeboundDelegation",
"name": "call a system via a timebound delegation",
"gasUsed": 26998
"gasUsed": 27019
},
{
"file": "test/UniqueEntityModule.t.sol",
"test": "testInstall",
"name": "install unique entity module",
"gasUsed": 690431
"gasUsed": 679682
},
{
"file": "test/UniqueEntityModule.t.sol",
"test": "testInstall",
"name": "get a unique entity nonce (non-root module)",
"gasUsed": 52176
"gasUsed": 52168
},
{
"file": "test/UniqueEntityModule.t.sol",
"test": "testInstallRoot",
"name": "installRoot unique entity module",
"gasUsed": 680681
"gasUsed": 669974
},
{
"file": "test/UniqueEntityModule.t.sol",
"test": "testInstallRoot",
"name": "get a unique entity nonce (root module)",
"gasUsed": 52176
"gasUsed": 52168
},
{
"file": "test/World.t.sol",
"test": "testCall",
"name": "call a system via the World",
"gasUsed": 12702
"gasUsed": 12679
},
{
"file": "test/World.t.sol",
"test": "testCallFromUnlimitedDelegation",
"name": "register an unlimited delegation",
"gasUsed": 50629
"gasUsed": 50650
},
{
"file": "test/World.t.sol",
"test": "testCallFromUnlimitedDelegation",
"name": "call a system via an unlimited delegation",
"gasUsed": 12895
"gasUsed": 12916
},
{
"file": "test/World.t.sol",
"test": "testDeleteRecord",
"name": "Delete record",
"gasUsed": 9197
"gasUsed": 9174
},
{
"file": "test/World.t.sol",
"test": "testPushToField",
"name": "Push data to the table",
"gasUsed": 86871
"gasUsed": 86848
},
{
"file": "test/World.t.sol",
"test": "testRegisterFallbackSystem",
"name": "Register a fallback system",
"gasUsed": 59306
"gasUsed": 59321
},
{
"file": "test/World.t.sol",
"test": "testRegisterFallbackSystem",
"name": "Register a root fallback system",
"gasUsed": 52950
"gasUsed": 52965
},
{
"file": "test/World.t.sol",
"test": "testRegisterFunctionSelector",
"name": "Register a function selector",
"gasUsed": 79872
"gasUsed": 79887
},
{
"file": "test/World.t.sol",
"test": "testRegisterNamespace",
"name": "Register a new namespace",
"gasUsed": 123269
"gasUsed": 123284
},
{
"file": "test/World.t.sol",
"test": "testRegisterRootFunctionSelector",
"name": "Register a root function selector",
"gasUsed": 74863
"gasUsed": 74878
},
{
"file": "test/World.t.sol",
"test": "testRegisterSystem",
"name": "register a system",
"gasUsed": 165750
"gasUsed": 165771
},
{
"file": "test/World.t.sol",
"test": "testRegisterTable",
"name": "Register a new table in the namespace",
"gasUsed": 651802
"gasUsed": 641004
},
{
"file": "test/World.t.sol",
"test": "testSetField",
"name": "Write data to a table field",
"gasUsed": 37387
"gasUsed": 37364
},
{
"file": "test/World.t.sol",
"test": "testSetRecord",
"name": "Write data to the table",
"gasUsed": 35381
"gasUsed": 36490
},
{
"file": "test/WorldDynamicUpdate.t.sol",
"test": "testPopFromField",
"name": "pop 1 address (cold)",
"gasUsed": 24672
"gasUsed": 24649
},
{
"file": "test/WorldDynamicUpdate.t.sol",
"test": "testPopFromField",
"name": "pop 1 address (warm)",
"gasUsed": 13818
"gasUsed": 13795
},
{
"file": "test/WorldDynamicUpdate.t.sol",
"test": "testUpdateInField",
"name": "updateInField 1 item (cold)",
"gasUsed": 25236
"gasUsed": 25213
},
{
"file": "test/WorldDynamicUpdate.t.sol",
"test": "testUpdateInField",
"name": "updateInField 1 item (warm)",
"gasUsed": 14441
"gasUsed": 14418
},
{
"file": "test/WorldResourceId.t.sol",
Expand Down
9 changes: 9 additions & 0 deletions packages/world/mud.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ export default mudConfig({
},
tableIdArgument: true,
},
TwoFields: {
directory: "../test/tables",
keySchema: {},
valueSchema: {
value1: "bool",
value2: "bool",
},
tableIdArgument: true,
},
AddressArray: {
directory: "../test/tables",
valueSchema: "address[]",
Expand Down
3 changes: 1 addition & 2 deletions packages/world/src/AccessControl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ library AccessControl {
*/
function hasAccess(ResourceId resourceId, address caller) internal view returns (bool) {
return
address(this) == caller || // First check if the World is calling itself
ResourceAccess._get(ResourceId.unwrap(resourceId.getNamespaceId()), caller) || // Then check access based on the namespace
ResourceAccess._get(ResourceId.unwrap(resourceId.getNamespaceId()), caller) || // First check access based on the namespace
ResourceAccess._get(ResourceId.unwrap(resourceId), caller); // If caller has no namespace access, check access on the name
}

Expand Down
Loading