Skip to content

Complain if auth hasn't been set up correctly #9181

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

Merged
merged 8 commits into from
Apr 11, 2019

Conversation

pranavkm
Copy link
Contributor

@pranavkm pranavkm commented Apr 8, 2019

Fixes #9041

@pranavkm
Copy link
Contributor Author

pranavkm commented Apr 8, 2019

Plain text benchmarks:

Description RPS CPU (%) Memory (MB) Avg. Latency (ms) Startup (ms) First Request (ms) Latency (ms)
Baseline 1,042,838 95 161 2.73 318 114.95 0.47
Check HttpContext.Items first 939,324 94 158 2.69 329 109.38 0.34
Check endpoint first 1,022,092 95 158 3.57 331 108.98 0.5
Also check Cors 1,008,837 95 158 2.75 321 115.12 0.72
Do auth 114,312 100 173 20.01 418 183.73 0.93

Auth one is a likely worse than normal since I was manually signing in every request.

@pranavkm
Copy link
Contributor Author

pranavkm commented Apr 8, 2019

cc @davidfowl

@pranavkm pranavkm requested review from JamesNK and rynowak April 8, 2019 22:34
@Eilon Eilon added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer labels Apr 8, 2019
@pranavkm pranavkm changed the base branch from master to release/3.0-preview4 April 8, 2019 22:53
@@ -4,7 +4,7 @@
namespace Microsoft.AspNetCore.Authorization
{
/// <summary>
/// Marker interface to enable the <see cref="AllowAnonymousAttribute"/>.
/// Marker interface to allow anonymous.
Copy link
Member

Choose a reason for hiding this comment

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

This doc comment makes me sad.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you have suggestions on what we could do to make this better?

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// Marker interface to allow anonymous.
/// Marker interface to allow access to anonymous users.

maybe?

@@ -11,6 +14,9 @@ namespace Microsoft.AspNetCore.Routing
{
internal sealed class EndpointMiddleware
{
internal const string AuthorizationMiddlewareInvokedKey = "__AuthorizationMiddlewareInvoked";
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if these should be features... Then we could back them by fields in Kestrel and avoid the dictionary look up altogether

Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to use mvc without ever initilizing Items? Most middleware use features instead. Note adding a feature would reset the version cache.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

private static void ThrowMissingAuthMiddlewareException(Endpoint endpoint)
{
throw new InvalidOperationException($"Endpoint {endpoint.DisplayName} contains authorization metadata, " +
"but a middleware was not found that supports authorization.");
Copy link
Member

Choose a reason for hiding this comment

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

We say "a middleware", but we really require middleware that sets AuthorizationMiddlewareInvokedKey.

Is this a problem for people who think that they can implement their own middleware to fix this problem? I think this exception could explicitly say .UseAuthorization() is required. It is the only middleware that knows to set AuthorizationMiddlewareInvokedKey

The alternative is providing a more first class way of saying that auth has run for the request.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error message is a bit more explicit about what to do resolve it and includes a line saying you could add auth.

The alternative is providing a more first class way of saying that auth has run for the request.

We could add a gesture for this or turn it in to a feature with settable properties. Either of these seem viable to me

@pranavkm pranavkm force-pushed the prkrishn/auth-check branch from 8ce2ba7 to 6e1bf58 Compare April 9, 2019 21:31
@@ -73,17 +77,33 @@ public void ConfigureServices(IServiceCollection services)
{
OnMessageReceived = context =>
{
var signalRTokenHeader = context.Request.Query["access_token"];
var endpoint = context.HttpContext.Features.Get<IEndpointFeature>()?.Endpoint;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Auth currently runs twice in SignalR (#9216). Perhaps it would be safer to leave that be in preview4. This particular change was the result of the WebSocketMiddleware not having run prior to endpoint routing - consequently the check context.HttpContext.WebSockets.IsWebSocketRequest failed.

@pranavkm
Copy link
Contributor Author

pranavkm commented Apr 9, 2019

🆙 📅

@@ -73,17 +77,33 @@ public void ConfigureServices(IServiceCollection services)
{
OnMessageReceived = context =>
{
var signalRTokenHeader = context.Request.Query["access_token"];
var endpoint = context.HttpContext.Features.Get<IEndpointFeature>()?.Endpoint;
if (endpoint != null && endpoint.Metadata.GetMetadata<HubMetadata>() != null)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@davidfowl this particular change was suggested by @BrennanConroy. In lieu of checking if a particular request is a WebSocket request, this always checks if the query string is specified. However it needs to read the Auth header if available.

Copy link
Member

Choose a reason for hiding this comment

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

I think this is reasonable.

The slight concern I have is that this could encourage people to put an access_token in the query string when they could have set an Authorization header. We'd like to encourage using the header over the query string since the query string is more likely to be logged.

I don't think that concern is big enough to warrant changing this logic though.

@Eilon
Copy link
Contributor

Eilon commented Apr 9, 2019

QB approved for Preview 4. Please check in and watch the build stay green.

Please also resolve https://devdiv.visualstudio.com/DevDiv/_workitems/edit/843164 when done.

@pranavkm
Copy link
Contributor Author

@halter73 \ @BrennanConroy how do I proceed with the SignalR functional test failure? The Windows build doesn't say what test failed and I'm not sure how to get to the logs to diagnose the Ubuntu failure.

@natemcmaster
Copy link
Contributor

👀 @aspnet/build - this will probably be the last Preview 4 change.

@BrennanConroy
Copy link
Member

The windows test is the same failure as the ubuntu one.

You can look in the <os>_Test_Logs artifacts folder for "browserlogs.console.log" and "ts.functionaltests.dotnet.log" for the server and client side logs. I looked at them last night and couldn't see any obvious issue. I would just skip the test for now to get this PR in.

@pranavkm
Copy link
Contributor Author

I'm giving it one more go putting back the custom CORS code that was in there. In the absence of it, I'm going to consider skipping the test.

@pranavkm
Copy link
Contributor Author

CORS seems to have done the trick.

@BrennanConroy
Copy link
Member

That is odd... any idea why?

@pranavkm
Copy link
Contributor Author

Not entirely sure. Perhaps the way the CI runs tests requires CORS to have run prior to redirect?

@pranavkm pranavkm force-pushed the prkrishn/auth-check branch from 4ccde15 to 6cd4599 Compare April 10, 2019 22:09
@pranavkm pranavkm closed this Apr 10, 2019
@pranavkm pranavkm reopened this Apr 10, 2019
@natemcmaster
Copy link
Contributor

/azp run AspNetCore-ci

@azure-pipelines
Copy link

Azure Pipelines failed to run 1 pipeline(s).

@natemcmaster
Copy link
Contributor

@natemcmaster
Copy link
Contributor

Previous test run failed on one flaky test. This has QB and review approvals, so I'm bringing it in with admin permissions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants