Skip to content

Commit

Permalink
Fix json rpc missing parameter check (#6351)
Browse files Browse the repository at this point in the history
* Fix json rpc missing parameter check

* Add more check when enumerating

* Modify tests to serialize null on net_version
  • Loading branch information
LukaszRozmej authored Dec 11, 2023
1 parent 1242ecd commit c284fa1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.JsonRpc.Test/JsonRpcServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void Initialize()
private ILogManager _logManager = null!;
private JsonRpcContext _context = null!;

private JsonRpcResponse TestRequest<T>(T module, string method, params string[] parameters) where T : IRpcModule
private JsonRpcResponse TestRequest<T>(T module, string method, params string[]? parameters) where T : IRpcModule
{
RpcModuleProvider moduleProvider = new(new FileSystem(), _configurationProvider.GetConfig<IJsonRpcConfig>(), LimboLogs.Instance);
moduleProvider.Register(new SingletonModulePool<T>(new SingletonFactory<T>(module), true));
Expand Down Expand Up @@ -182,7 +182,7 @@ public void NetVersionTest()
{
INetRpcModule netRpcModule = Substitute.For<INetRpcModule>();
netRpcModule.net_version().ReturnsForAnyArgs(x => ResultWrapper<string>.Success("1"));
JsonRpcSuccessResponse? response = TestRequest(netRpcModule, "net_version") as JsonRpcSuccessResponse;
JsonRpcSuccessResponse? response = TestRequest(netRpcModule, "net_version", null) as JsonRpcSuccessResponse;
Assert.That(response?.Result, Is.EqualTo("1"));
Assert.IsNotInstanceOf<JsonRpcErrorResponse>(response);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.JsonRpc.Test/RpcTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public static IJsonRpcService BuildRpcService<T>(T module) where T : class, IRpc
return service;
}

public static JsonRpcRequest GetJsonRequest(string method, params string[] parameters)
public static JsonRpcRequest GetJsonRequest(string method, params string[]? parameters)
{
var doc = JsonDocument.Parse(JsonSerializer.Serialize(parameters?.ToArray() ?? Array.Empty<string>()));
var doc = JsonDocument.Parse(JsonSerializer.Serialize(parameters?.ToArray()));
var request = new JsonRpcRequest()
{
JsonRpc = "2.0",
Expand Down
14 changes: 9 additions & 5 deletions src/Nethermind/Nethermind.JsonRpc/JsonRpcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,16 @@ private async Task<JsonRpcResponse> ExecuteAsync(JsonRpcRequest request, string

LogRequest(methodName, providedParameters, method.expectedParameters);

int missingParamsCount = method.expectedParameters.Length - providedParameters.GetArrayLength();
foreach (JsonElement item in providedParameters.EnumerateArray())
var providedParametersLength = providedParameters.ValueKind == JsonValueKind.Array ? providedParameters.GetArrayLength() : 0;
int missingParamsCount = method.expectedParameters.Length - providedParametersLength;
if (providedParametersLength > 0)
{
if (item.ValueKind == JsonValueKind.Null || (item.ValueKind == JsonValueKind.String && item.ValueEquals(ReadOnlySpan<byte>.Empty)))
foreach (JsonElement item in providedParameters.EnumerateArray())
{
missingParamsCount++;
if (item.ValueKind == JsonValueKind.Null || (item.ValueKind == JsonValueKind.String && item.ValueEquals(ReadOnlySpan<byte>.Empty)))
{
missingParamsCount++;
}
}
}

Expand Down Expand Up @@ -232,7 +236,7 @@ private void LogRequest(string methodName, JsonElement providedParameters, Param
int paramsCount = 0;
const string separator = ", ";

if (providedParameters.ValueKind != JsonValueKind.Undefined && providedParameters.ValueKind != JsonValueKind.Null)
if (providedParameters.ValueKind == JsonValueKind.Array)
{
foreach (JsonElement param in providedParameters.EnumerateArray())
{
Expand Down

0 comments on commit c284fa1

Please sign in to comment.