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

[automated] Merge branch 'release/3.0-preview8' => 'release/3.0' #12710

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
ad533b7
Limit max handshake size (#12678)
BrennanConroy Jul 29, 2019
2f61b3d
Update dependencies from https://github.com/aspnet/EntityFrameworkCor…
Jul 30, 2019
91ff3ce
Update dependencies from https://github.com/aspnet/EntityFrameworkCor…
Jul 30, 2019
a453cb2
Add .NET Standard 2.1 workaround
dougbu Jul 30, 2019
fad2108
Update dependencies from https://github.com/aspnet/EntityFrameworkCor…
Jul 30, 2019
96950da
Update dependencies from https://github.com/aspnet/EntityFrameworkCor…
Jul 30, 2019
a8a4791
Update dependencies from https://github.com/aspnet/EntityFrameworkCor…
Jul 30, 2019
f6d9c6b
Update dependencies from https://github.com/aspnet/Blazor build 20190…
Jul 30, 2019
2a44ee2
Update dependencies from https://github.com/aspnet/EntityFrameworkCor…
dotnet-maestro[bot] Jul 30, 2019
8b6a50f
Update dependencies from https://github.com/aspnet/AspNetCore-Tooling…
dotnet-maestro[bot] Jul 30, 2019
4841d5f
Update gRPC template dependency for preview 8 and link to macOS docs
JamesNK Jul 31, 2019
10b9c33
Clean up
JamesNK Jul 31, 2019
ccfa7f1
Clean up
JamesNK Jul 31, 2019
2544fef
Update dependencies from https://github.com/aspnet/EntityFrameworkCor…
dotnet-maestro[bot] Jul 31, 2019
9a08094
[release/3.0-preview8] Update dependencies from aspnet/AspNetCore-Too…
dotnet-maestro[bot] Jul 31, 2019
5c11687
SPA template precedence (#12781)
ryanbrandenburg Jul 31, 2019
9459b73
Merge pull request #12752 from aspnet/jamesnk/grpc-dep-preview8
Aug 1, 2019
0dc822a
Merge branch 'release/3.0' into merge/release/3.0-preview8-to-release…
dougbu Aug 3, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public partial class HubConnectionContextOptions
public HubConnectionContextOptions() { }
public System.TimeSpan ClientTimeoutInterval { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public System.TimeSpan KeepAliveInterval { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public long? MaximumReceiveMessageSize { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public int StreamBufferCapacity { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
}
public partial class HubConnectionHandler<THub> : Microsoft.AspNetCore.Connections.ConnectionHandler where THub : Microsoft.AspNetCore.SignalR.Hub
Expand Down
30 changes: 28 additions & 2 deletions src/SignalR/server/Core/src/HubConnectionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class HubConnectionContext
private bool _clientTimeoutActive;
private bool _connectionAborted;
private int _streamBufferCapacity;
private long? _maxMessageSize;

/// <summary>
/// Initializes a new instance of the <see cref="HubConnectionContext"/> class.
Expand All @@ -55,6 +56,7 @@ public HubConnectionContext(ConnectionContext connectionContext, HubConnectionCo
_keepAliveInterval = contextOptions.KeepAliveInterval.Ticks;
_clientTimeoutInterval = contextOptions.ClientTimeoutInterval.Ticks;
_streamBufferCapacity = contextOptions.StreamBufferCapacity;
_maxMessageSize = contextOptions.MaximumReceiveMessageSize;

_connectionContext = connectionContext;
_logger = loggerFactory.CreateLogger<HubConnectionContext>();
Expand Down Expand Up @@ -406,10 +408,20 @@ internal async Task<bool> HandshakeAsync(TimeSpan timeout, IReadOnlyList<string>

if (!buffer.IsEmpty)
{
if (HandshakeProtocol.TryParseRequestMessage(ref buffer, out var handshakeRequestMessage))
var segment = buffer;
var overLength = false;

if (_maxMessageSize != null && buffer.Length > _maxMessageSize.Value)
{
// We give the parser a sliding window of the default message size
segment = segment.Slice(segment.Start, _maxMessageSize.Value);
overLength = true;
}

if (HandshakeProtocol.TryParseRequestMessage(ref segment, out var handshakeRequestMessage))
{
// We parsed the handshake
consumed = buffer.Start;
consumed = segment.Start;
examined = consumed;

Protocol = protocolResolver.GetProtocol(handshakeRequestMessage.Protocol, supportedProtocols);
Expand Down Expand Up @@ -461,6 +473,12 @@ await WriteHandshakeResponseAsync(new HandshakeResponseMessage(
await WriteHandshakeResponseAsync(HandshakeResponseMessage.Empty);
return true;
}
else if (overLength)
{
Log.HandshakeSizeLimitExceeded(_logger, _maxMessageSize.Value);
await WriteHandshakeResponseAsync(new HandshakeResponseMessage("Handshake was canceled."));
return false;
}
}

if (result.IsCompleted)
Expand Down Expand Up @@ -619,6 +637,9 @@ private static class Log
private static readonly Action<ILogger, int, Exception> _clientTimeout =
LoggerMessage.Define<int>(LogLevel.Debug, new EventId(9, "ClientTimeout"), "Client timeout ({ClientTimeout}ms) elapsed without receiving a message from the client. Closing connection.");

private static readonly Action<ILogger, long, Exception> _handshakeSizeLimitExceeded =
LoggerMessage.Define<long>(LogLevel.Debug, new EventId(10, "HandshakeSizeLimitExceeded"), "The maximum message size of {MaxMessageSize}B was exceeded while parsing the Handshake. The message size can be configured in AddHubOptions.");

public static void HandshakeComplete(ILogger logger, string hubProtocol)
{
_handshakeComplete(logger, hubProtocol, null);
Expand Down Expand Up @@ -663,6 +684,11 @@ public static void ClientTimeout(ILogger logger, TimeSpan timeout)
{
_clientTimeout(logger, (int)timeout.TotalMilliseconds, null);
}

public static void HandshakeSizeLimitExceeded(ILogger logger, long maxMessageSize)
{
_handshakeSizeLimitExceeded(logger, maxMessageSize, null);
}
}
}
}
5 changes: 5 additions & 0 deletions src/SignalR/server/Core/src/HubConnectionContextOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ public class HubConnectionContextOptions
/// Gets or sets the max buffer size for client upload streams.
/// </summary>
public int StreamBufferCapacity { get; set; }

/// <summary>
/// Gets or sets the maximum message size the client can send.
/// </summary>
public long? MaximumReceiveMessageSize { get; set; }
}
}
1 change: 1 addition & 0 deletions src/SignalR/server/Core/src/HubConnectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public override async Task OnConnectedAsync(ConnectionContext connection)
KeepAliveInterval = _hubOptions.KeepAliveInterval ?? _globalHubOptions.KeepAliveInterval ?? HubOptionsSetup.DefaultKeepAliveInterval,
ClientTimeoutInterval = _hubOptions.ClientTimeoutInterval ?? _globalHubOptions.ClientTimeoutInterval ?? HubOptionsSetup.DefaultClientTimeoutInterval,
StreamBufferCapacity = _hubOptions.StreamBufferCapacity ?? _globalHubOptions.StreamBufferCapacity ?? HubOptionsSetup.DefaultStreamBufferCapacity,
MaximumReceiveMessageSize = _maximumMessageSize,
};

Log.ConnectedStarting(_logger);
Expand Down
34 changes: 34 additions & 0 deletions src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,40 @@ bool ExpectedErrors(WriteContext writeContext)
}
}

[Fact]
public async Task ConnectionClosedWhenHandshakeLargerThanMaxMessageSize()
{
using (StartVerifiableLog())
{
var connectionHandler = HubConnectionHandlerTestUtils.GetHubConnectionHandler(typeof(HubT), loggerFactory: LoggerFactory,
builder =>
{
builder.AddSignalR(o =>
{
o.MaximumReceiveMessageSize = 1;
});
});

using (var client = new TestClient())
{
client.SupportedFormats = TransferFormat.Text;

var connectionHandlerTask = await client.ConnectAsync(connectionHandler,
sendHandshakeRequestMessage: true,
expectedHandshakeResponseMessage: false);

var message = await client.ReadAsync(isHandshake: true).OrTimeout();

Assert.Equal("Handshake was canceled.", ((HandshakeResponseMessage)message).Error);

// Connection closes
await connectionHandlerTask.OrTimeout();

client.Dispose();
}
}
}

[Fact]
public async Task SendingHandshakeRequestInChunksWorks()
{
Expand Down