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

MaxParamsDepth and MaxRequestBodySize in RpcServer #827

Merged
merged 6 commits into from
Nov 8, 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
9 changes: 7 additions & 2 deletions src/RpcServer/RpcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace Neo.Plugins
{
public partial class RpcServer : IDisposable
{
private const int MaxParamsDepth = 32;

private readonly Dictionary<string, Func<JArray, object>> methods = new();

private IWebHost host;
Expand Down Expand Up @@ -105,6 +107,9 @@ public void StartRpcServer()
{
host = new WebHostBuilder().UseKestrel(options => options.Listen(settings.BindAddress, settings.Port, listenOptions =>
{
// Default value is 5Mb
options.Limits.MaxRequestBodySize = settings.MaxRequestBodySize;
shargon marked this conversation as resolved.
Show resolved Hide resolved
options.Limits.MaxRequestLineSize = Math.Min(settings.MaxRequestBodySize, options.Limits.MaxRequestLineSize);
// Default value is 40
options.Limits.MaxConcurrentConnections = settings.MaxConcurrentConnections;
// Default value is 1 minutes
Expand Down Expand Up @@ -182,15 +187,15 @@ public async Task ProcessAsync(HttpContext context)
request["jsonrpc"] = jsonrpc;
request["id"] = id;
request["method"] = method;
request["params"] = JToken.Parse(_params);
request["params"] = JToken.Parse(_params, MaxParamsDepth);
}
}
else if (context.Request.Method == "POST")
{
using StreamReader reader = new(context.Request.Body);
try
{
request = JToken.Parse(await reader.ReadToEndAsync());
request = JToken.Parse(await reader.ReadToEndAsync(), MaxParamsDepth);
}
catch (FormatException) { }
}
Expand Down
3 changes: 3 additions & 0 deletions src/RpcServer/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public record RpcServerSettings
public string SslCertPassword { get; init; }
public string[] TrustedAuthorities { get; init; }
public int MaxConcurrentConnections { get; init; }
public int MaxRequestBodySize { get; init; }
public string RpcUser { get; init; }
public string RpcPass { get; init; }
public long MaxGasInvoke { get; init; }
Expand All @@ -60,6 +61,7 @@ public record RpcServerSettings
MaxStackSize = ushort.MaxValue,
DisabledMethods = Array.Empty<string>(),
MaxConcurrentConnections = 40,
MaxRequestBodySize = 5 * 1024 * 1024,
SessionEnabled = false,
SessionExpirationTime = TimeSpan.FromSeconds(60),
FindStoragePageSize = 50
Expand All @@ -81,6 +83,7 @@ public record RpcServerSettings
MaxStackSize = section.GetValue("MaxStackSize", Default.MaxStackSize),
DisabledMethods = section.GetSection("DisabledMethods").GetChildren().Select(p => p.Get<string>()).ToArray(),
MaxConcurrentConnections = section.GetValue("MaxConcurrentConnections", Default.MaxConcurrentConnections),
MaxRequestBodySize = section.GetValue("MaxRequestBodySize", Default.MaxRequestBodySize),
SessionEnabled = section.GetValue("SessionEnabled", Default.SessionEnabled),
SessionExpirationTime = TimeSpan.FromSeconds(section.GetValue("SessionExpirationTime", (int)Default.SessionExpirationTime.TotalSeconds)),
FindStoragePageSize = section.GetValue("FindStoragePageSize", Default.FindStoragePageSize)
Expand Down