Detect ASP.NET SignalR server connection attempt (#2820)

This commit is contained in:
BrennanConroy 2018-08-22 11:17:04 -07:00 committed by GitHub
parent 5f4d0dac3c
commit f8a46faf9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 2 deletions

View File

@ -146,6 +146,10 @@ export class HttpConnection implements IConnection {
return;
}
if ((negotiateResponse as any).ProtocolVersion) {
throw Error("Detected a connection attempt to an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server. See https://aka.ms/signalr-core-differences for details.");
}
if (negotiateResponse.url) {
url = negotiateResponse.url;
}

View File

@ -794,5 +794,38 @@ describe("HttpConnection", () => {
expect(() => connection.start(42)).toThrowError("Unknown transferFormat value: 42.");
});
});
it("throws if trying to connect to an ASP.NET SignalR Server", async () => {
await VerifyLogger.run(async (logger) => {
const options: IHttpConnectionOptions = {
...commonOptions,
httpClient: new TestHttpClient()
.on("POST", () => "{\"Url\":\"/signalr\"," +
"\"ConnectionToken\":\"X97dw3uxW4NPPggQsYVcNcyQcuz4w2\"," +
"\"ConnectionId\":\"05265228-1e2c-46c5-82a1-6a5bcc3f0143\"," +
"\"KeepAliveTimeout\":10.0," +
"\"DisconnectTimeout\":5.0," +
"\"TryWebSockets\":true," +
"\"ProtocolVersion\":\"1.5\"," +
"\"TransportConnectTimeout\":30.0," +
"\"LongPollDelay\":0.0}")
.on("GET", () => ""),
logger,
} as IHttpConnectionOptions;
const connection = new HttpConnection("http://tempuri.org", options);
let receivedError = false;
try {
await connection.start(TransferFormat.Text);
} catch (error) {
expect(error).toEqual(new Error("Detected a connection attempt to an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server. See https://aka.ms/signalr-core-differences for details."));
receivedError = true;
} finally {
await connection.stop();
}
expect(receivedError).toBe(true);
},
"Failed to start the connection: Error: Detected a connection attempt to an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server. See https://aka.ms/signalr-core-differences for details.");
});
});
});

View File

@ -18,6 +18,8 @@ namespace Microsoft.AspNetCore.Http.Connections
private const string AvailableTransportsPropertyName = "availableTransports";
private const string TransportPropertyName = "transport";
private const string TransferFormatsPropertyName = "transferFormats";
// Used to detect ASP.NET SignalR Server connection attempt
private const string ProtocolVersionPropertyName = "ProtocolVersion";
public static void WriteResponse(NegotiationResponse response, IBufferWriter<byte> output)
{
@ -134,6 +136,8 @@ namespace Microsoft.AspNetCore.Http.Connections
}
}
break;
case ProtocolVersionPropertyName:
throw new InvalidOperationException("Detected a connection attempt to an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server. See https://aka.ms/signalr-core-differences for details.");
default:
reader.Skip();
break;

View File

@ -55,6 +55,27 @@ namespace Microsoft.AspNetCore.Http.Connections.Tests
Assert.Equal(expectedMessage, exception.InnerException.Message);
}
[Fact]
public void ParsingAspNetSignalRResponseThrowsError()
{
var payload = "{\"Url\":\"/signalr\"," +
"\"ConnectionToken\":\"X97dw3uxW4NPPggQsYVcNcyQcuz4w2\"," +
"\"ConnectionId\":\"05265228-1e2c-46c5-82a1-6a5bcc3f0143\"," +
"\"KeepAliveTimeout\":10.0," +
"\"DisconnectTimeout\":5.0," +
"\"TryWebSockets\":true," +
"\"ProtocolVersion\":\"1.5\"," +
"\"TransportConnectTimeout\":30.0," +
"\"LongPollDelay\":0.0}";
var responseData = Encoding.UTF8.GetBytes(payload);
var ms = new MemoryStream(responseData);
var exception = Assert.Throws<InvalidDataException>(() => NegotiateProtocol.ParseResponse(ms));
Assert.Equal("Detected a connection attempt to an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server. See https://aka.ms/signalr-core-differences for details.", exception.InnerException.Message);
}
[Fact]
public void WriteNegotiateResponseWithNullAvailableTransports()
{

View File

@ -83,7 +83,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
}
[Fact]
public async Task NegotiateReturnedConenctionIdIsSetOnConnection()
public async Task NegotiateReturnedConnectionIdIsSetOnConnection()
{
string connectionId = null;
@ -153,7 +153,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
testHttpHandler.OnLongPoll((token) =>
{
var tcs = new TaskCompletionSource<HttpResponseMessage>(TaskCreationOptions.RunContinuationsAsynchronously);
token.Register(() => tcs.TrySetResult(ResponseUtils.CreateResponse(HttpStatusCode.NoContent)));
return tcs.Task;