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

Fix json rpc missing parameter check #6351

Merged
merged 3 commits into from
Dec 11, 2023
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
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())
Copy link
Contributor

Choose a reason for hiding this comment

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

same foreach, no null issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

it has to be array

{
Expand Down
Loading