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

SmartContract: use executing contract state to check permissions #3290

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/Neo/Hardfork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum Hardfork : byte
{
HF_Aspidochelone,
HF_Basilisk,
HF_Cockatrice
HF_Cockatrice,
HF_Domovoi
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions on name are welcomed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not familier with any of them, LOL, LGTM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as Jimmy

}
}
10 changes: 6 additions & 4 deletions src/Neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,18 @@ private ExecutionContext CallContractInternal(ContractState contract, ContractMe
if (NativeContract.Policy.IsBlocked(Snapshot, contract.Hash))
throw new InvalidOperationException($"The contract {contract.Hash} has been blocked.");

ExecutionContext currentContext = CurrentContext;
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
ExecutionContextState state = currentContext.GetState<ExecutionContextState>();
if (method.Safe)
{
flags &= ~(CallFlags.WriteStates | CallFlags.AllowNotify);
}
else
{
ContractState currentContract = NativeContract.ContractManagement.GetContract(Snapshot, CurrentScriptHash);
if (currentContract?.CanCall(contract, method.Name) == false)
var executingContract = IsHardforkEnabled(Hardfork.HF_Domovoi)
? state.Contract // use executing contract state to avoid possible contract update/destroy side-effects, ref. https://github.com/neo-project/neo/pull/3290.
: NativeContract.ContractManagement.GetContract(Snapshot, CurrentScriptHash);
if (executingContract?.CanCall(contract, method.Name) == false)
throw new InvalidOperationException($"Cannot Call Method {method.Name} Of Contract {contract.Hash} From Contract {CurrentScriptHash}");
}

Expand All @@ -306,8 +310,6 @@ private ExecutionContext CallContractInternal(ContractState contract, ContractMe
invocationCounter[contract.Hash] = 1;
}

ExecutionContext currentContext = CurrentContext;
ExecutionContextState state = currentContext.GetState<ExecutionContextState>();
CallFlags callingFlags = state.CallFlags;

if (args.Count != method.Parameters.Length) throw new InvalidOperationException($"Method {method} Expects {method.Parameters.Length} Arguments But Receives {args.Count} Arguments");
Expand Down
101 changes: 101 additions & 0 deletions tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.UnitTests.Extensions;
using Neo.VM;
using System;
using System.Collections.Immutable;
using System.Linq;
Expand Down Expand Up @@ -103,5 +106,103 @@ public void TestCheckingHardfork()
(setting[sortedHardforks[i]] > setting[sortedHardforks[i + 1]]).Should().Be(false);
}
}

[TestMethod]
public void TestSystem_Contract_Call_Permissions()
{
UInt160 scriptHash;
var snapshot = TestBlockchain.GetTestSnapshot();

// Setup: put a simple contract to the storage.
using (var script = new ScriptBuilder())
{
// Push True on stack and return.
script.EmitPush(true);
script.Emit(OpCode.RET);

// Mock contract and put it to the Managemant's storage.
scriptHash = script.ToArray().ToScriptHash();

snapshot.DeleteContract(scriptHash);
vncoelho marked this conversation as resolved.
Show resolved Hide resolved
var contract = TestUtils.GetContract(script.ToArray(), TestUtils.CreateManifest("test", ContractParameterType.Any));
contract.Manifest.Abi.Methods = new[]
{
new ContractMethodDescriptor
{
Name = "disallowed",
Parameters = new ContractParameterDefinition[]{}
},
new ContractMethodDescriptor
{
Name = "test",
Parameters = new ContractParameterDefinition[]{}
}
};
snapshot.AddContract(scriptHash, contract);
}

// Disallowed method call.
using (var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, null, ProtocolSettings.Default))
using (var script = new ScriptBuilder())
{
// Build call script calling disallowed method.
script.EmitDynamicCall(scriptHash, "disallowed");

// Mock executing state to be a contract-based.
engine.LoadScript(script.ToArray());
engine.CurrentContext.GetState<ExecutionContextState>().Contract = new()
{
Manifest = new()
{
Abi = new() { },
Permissions = new ContractPermission[]
{
new ContractPermission
{
Contract = ContractPermissionDescriptor.Create(scriptHash),
Methods = WildcardContainer<string>.Create(new string[]{"test"}) // allowed to call only "test" method of the target contract.
}
}
}
};
var currentScriptHash = engine.EntryScriptHash;

Assert.AreEqual(VMState.FAULT, engine.Execute());
Assert.IsTrue(engine.FaultException.ToString().Contains($"Cannot Call Method disallowed Of Contract {scriptHash.ToString()}"));
}

// Allowed method call.
using (var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, null, ProtocolSettings.Default))
using (var script = new ScriptBuilder())
{
// Build call script.
script.EmitDynamicCall(scriptHash, "test");

// Mock executing state to be a contract-based.
engine.LoadScript(script.ToArray());
engine.CurrentContext.GetState<ExecutionContextState>().Contract = new()
{
Manifest = new()
{
Abi = new() { },
Permissions = new ContractPermission[]
{
new ContractPermission
{
Contract = ContractPermissionDescriptor.Create(scriptHash),
Methods = WildcardContainer<string>.Create(new string[]{"test"}) // allowed to call only "test" method of the target contract.
}
}
}
};
var currentScriptHash = engine.EntryScriptHash;

Assert.AreEqual(VMState.HALT, engine.Execute());
Assert.AreEqual(1, engine.ResultStack.Count);
Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(VM.Types.Boolean));
var res = (VM.Types.Boolean)engine.ResultStack.Pop();
Assert.IsTrue(res.GetBoolean());
}
}
}
}
26 changes: 25 additions & 1 deletion tests/Neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public void Runtime_GetNotifications_Test()
}
}
};
contract.Manifest.Permissions = new ContractPermission[]
{
new ContractPermission
{
Contract = ContractPermissionDescriptor.Create(scriptHash2),
Methods = WildcardContainer<string>.Create(new string[]{"test"})
}
};
snapshot.AddContract(scriptHash2, contract);
}

Expand Down Expand Up @@ -133,6 +141,14 @@ public void Runtime_GetNotifications_Test()
Parameters = System.Array.Empty<ContractParameterDefinition>()
}
}
},
Permissions = new ContractPermission[]
{
new ContractPermission
{
Contract = ContractPermissionDescriptor.Create(scriptHash2),
Methods = WildcardContainer<string>.Create(new string[]{"test"})
}
}
}
};
Expand Down Expand Up @@ -202,6 +218,14 @@ public void Runtime_GetNotifications_Test()
Parameters = System.Array.Empty<ContractParameterDefinition>()
}
}
},
Permissions = new ContractPermission[]
{
new ContractPermission
{
Contract = ContractPermissionDescriptor.Create(scriptHash2),
Methods = WildcardContainer<string>.Create(new string[]{"test"})
}
}
}
};
Expand Down Expand Up @@ -642,7 +666,7 @@ public void TestContract_Call()
var args = new VM.Types.Array { 0, 1 };
var state = TestUtils.GetContract(method, args.Count);

var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, null, ProtocolSettings.Default);
engine.LoadScript(new byte[] { 0x01 });
engine.Snapshot.AddContract(state.Hash, state);

Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.UnitTests/SmartContract/UT_Syscalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void System_Runtime_GetInvocationCounter()

// Execute

var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, null, ProtocolSettings.Default);
engine.LoadScript(script.ToArray());
Assert.AreEqual(VMState.HALT, engine.Execute());

Expand Down