Skip to content

Commit a677fd2

Browse files
mderrieyTratcher
authored andcommitted
Make OIDC handler skip unrecognized requests (#10060)
1 parent 6d5b6b0 commit a677fd2

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

src/Security/Authentication/OpenIdConnect/src/OpenIdConnectHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ protected override async Task<HandleRequestResult> HandleRemoteAuthenticateAsync
515515
authorizationResponse = messageReceivedContext.ProtocolMessage;
516516
properties = messageReceivedContext.Properties;
517517

518-
if (properties == null)
518+
if (properties == null || properties.Items.Count == 0)
519519
{
520520
// Fail if state is missing, it's required for the correlation id.
521521
if (string.IsNullOrEmpty(authorizationResponse.State))
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.Net.Http;
6+
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Http;
8+
using Xunit;
9+
10+
namespace Microsoft.AspNetCore.Authentication.Test.OpenIdConnect
11+
{
12+
public class OpenIdConnectAuthenticateTests
13+
{
14+
[Fact]
15+
public async Task RegularGetRequestToCallbackPathSkips()
16+
{
17+
// Arrange
18+
var settings = new TestSettings(
19+
opt =>
20+
{
21+
opt.Authority = TestServerBuilder.DefaultAuthority;
22+
opt.CallbackPath = new PathString("/");
23+
opt.SkipUnrecognizedRequests = true;
24+
opt.ClientId = "Test Id";
25+
});
26+
27+
var server = settings.CreateTestServer(handler: async context =>
28+
{
29+
await context.Response.WriteAsync("Hi from the callback path");
30+
});
31+
32+
// Act
33+
var transaction = await server.SendAsync("/");
34+
35+
// Assert
36+
Assert.Equal("Hi from the callback path", transaction.ResponseText);
37+
}
38+
39+
[Fact]
40+
public async Task RegularPostRequestToCallbackPathSkips()
41+
{
42+
// Arrange
43+
var settings = new TestSettings(
44+
opt =>
45+
{
46+
opt.Authority = TestServerBuilder.DefaultAuthority;
47+
opt.CallbackPath = new PathString("/");
48+
opt.SkipUnrecognizedRequests = true;
49+
opt.ClientId = "Test Id";
50+
});
51+
52+
var server = settings.CreateTestServer(handler: async context =>
53+
{
54+
await context.Response.WriteAsync("Hi from the callback path");
55+
});
56+
57+
// Act
58+
var request = new HttpRequestMessage(HttpMethod.Post, "/");
59+
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>());
60+
61+
var transaction = await server.SendAsync(request, cookieHeader: null);
62+
63+
// Assert
64+
Assert.Equal("Hi from the callback path", transaction.ResponseText);
65+
}
66+
}
67+
}

src/Security/Authentication/test/OpenIdConnect/TestServerExtensions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ public static Task<TestTransaction> SendAsync(this TestServer server, string url
1616
return SendAsync(server, url, cookieHeader: null);
1717
}
1818

19-
public static async Task<TestTransaction> SendAsync(this TestServer server, string uri, string cookieHeader)
19+
public static Task<TestTransaction> SendAsync(this TestServer server, string url, string cookieHeader)
20+
{
21+
return SendAsync(server, new HttpRequestMessage(HttpMethod.Get, url), cookieHeader);
22+
}
23+
24+
public static async Task<TestTransaction> SendAsync(this TestServer server, HttpRequestMessage request, string cookieHeader)
2025
{
21-
var request = new HttpRequestMessage(HttpMethod.Get, uri);
2226
if (!string.IsNullOrEmpty(cookieHeader))
2327
{
2428
request.Headers.Add("Cookie", cookieHeader);

0 commit comments

Comments
 (0)