The OpenIddict services registration APIs have been revamped
In this release, we focused on reworking the OpenIddict registration APIs to offer a better user experience.
As part of this change, we split the OpenIddict services into three areas - Core, Server and Validation - and the IServiceCollection APIs have been updated to reflect that:
Each specialized builder only exposes the options that are relevant to its specific area:
Of course, the calls to AddCore(), AddServer() and AddValidation() can be chained:
// Register the OpenIddict core services. .AddCore(options => { // Register the Entity Framework stores and models. options.UseEntityFrameworkCore() .UseDbContext<ApplicationDbContext>(); })
// Register the OpenIddict server handler. .AddServer(options => { // Register the ASP.NET Core MVC binder used by OpenIddict. // Note: if you don't call this method, you won't be able to // bind OpenIdConnectRequest or OpenIdConnectResponse parameters. options.UseMvc();
// Enable the authorization, logout, token and userinfo endpoints. options.EnableAuthorizationEndpoint("/connect/authorize") .EnableLogoutEndpoint("/connect/logout") .EnableTokenEndpoint("/connect/token") .EnableUserinfoEndpoint("/api/userinfo");
// Note: the Mvc.Client sample only uses the code flow and the password flow, but you // can enable the other flows if you need to support implicit or client credentials. options.AllowAuthorizationCodeFlow() .AllowPasswordFlow() .AllowRefreshTokenFlow();
// During development, you can disable the HTTPS requirement. options.DisableHttpsRequirement(); })
// Register the OpenIddict validation handler. // Note: the OpenIddict validation handler is only compatible with the // default token format or with reference tokens and cannot be used with // JWT tokens. For JWT tokens, use the Microsoft JWT bearer handler. .AddValidation();
Introducing these specialized builders was also a great opportunity to revisit how the OpenIddict entities are registered. In the RC2 bits, this is controlled by the services.AddOpenIddict<...>() method, that determines which entities are used depending on the overload.
In RC3, the generic services.AddOpenIddict<...>() methods have been removed and replaced by a more explicit pattern:
Today, I'm really pleased to announce that the aspnet-contrib social providers are now RTM.
I'd like to thank all the contributors – 47 at the time of writing – who helped make that possible, with a special mention to Chino Chang, who did a fantastic job by manually porting most of the social providers to the ASP.NET Core 2.0 authentication stack: sir, you rock!
Each provider comes with 2 versions: one for ASP.NET Core 1.x and one for ASP.NET Core 2.x. You can find the complete list at the end of this post.
Both versions will receive security updates/bug fixes but new providers will be backported to ASP.NET Core 1.x only if there's enough demand. If you still run an ASP.NET Core 1.x application, let us know!
Starting with RC2, using OpenIddict with third-party client applications (i.e applications you don't own and are managed by someone else) is officially supported. For that, new features – that were still work in progress in the previous iterations – have been added:
A new application permissions feature was added, which allows controlling and limiting the features a client application can use. For instance, to allow a client application to use only the authorization code flow and the logout endpoint, the following permissions can be granted:
A new opt-in scope validation option was added. When it is enabled, OpenIddict automatically rejects authorization and token requests that specify unregistered scopes. Scopes can be registered statically using options.RegisterScopes([list of authorized scopes]) or dynamically, using OpenIddictScopeManager.CreateAsync():
1 2 3 4 5 6 7 8 9 10 11 12
services.AddOpenIddict(options => { // ...
// Mark the "email" and "profile" scopes as valid scopes. options.RegisterScopes(OpenIdConnectConstants.Scopes.Email, OpenIdConnectConstants.Scopes.Profile);
// Enable scope validation, so that authorization and token requests // that specify unregistered scopes are automatically rejected. options.EnableScopeValidation(); });
1 2 3 4 5 6
await _scopeManager.CreateAsync(new OpenIddictScopeDescriptor { Description = "Grants access to the reporting API", DisplayName = "Reporting API", Name = "reporting" });
The introspection endpoint was updated to reject access tokens that don't have any audience (since OpenIddict can no longer assume all the registered applications are fully trusted). This change requires updating your code to explicitly attach a resource to your tokens.
1 2 3 4 5 6
var ticket = new AuthenticationTicket( new ClaimsPrincipal(identity), new AuthenticationProperties(), OpenIdConnectServerDefaults.AuthenticationScheme);
Earlier today, Microsoft released two security advisories for vulnerabilities I discovered in the ASP.NET Core 2.0 VS2017 templates and reported late October:
CVE-2018-0785 - ASP.NET Core Templates enable Cross Site Request Forgery: a CSRF vulnerability in ManageController.GenerateRecoveryCodes() allows re-generating the recovery codes associated with a victim's account, which may result in a definitive account lockout (aka per-user denial of service).
While the first one is a very classic case of cross-site request forgery (CSRF), the second one is a bit more interesting as it relies on a specificity of ASP.NET Core MVC to be exploited.
Where was the vulnerability located?
When you create a new project based on the ASP.NET Core 2.0 templates offered by Visual Studio 2017 and opt for individual authentication (that uses ASP.NET Core Identity under the hood), a new ManageController and a bunch of views are automatically added to the resulting solution.
One of the actions exposed by this controller, EnableAuthenticator, allows you to generate a shared secret you can use in your favorite TOTP-based application (like Microsoft or Google Authenticator) to enable 2-factor authentication:
[HttpGet] publicasync Task<IActionResult> EnableAuthenticator() { var user = await _userManager.GetUserAsync(User); if (user == null) { thrownew ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); }
var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); }
var model = new EnableAuthenticatorViewModel { SharedKey = FormatKey(unformattedKey), AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey) };
return View(model); }
For that, a random value is generated server-side by Identity, stored in the database and used to build a special otpauth://totp URI which is rendered in the Razor view as an HTML attribute (that can be optionally read by a JS library to generate a QR code image client-side):
1 2 3 4 5 6
<li> <p>Scan the QR Code or enter this key <kbd>@Model.SharedKey</kbd> into your two factor authenticator app. Spaces and casing do not matter.</p> <divclass="alert alert-info">To enable QR code generation please read our <ahref="https://go.microsoft.com/fwlink/?Linkid=852423">documentation</a>.</div> <divid="qrCode"></div> <divid="qrCodeData"data-url="@Html.Raw(Model.AuthenticatorUri)"></div> </li>
1 2 3 4 5 6
<li> <p>Scan the QR Code or enter this key <kbd>xuht bbzd 3juv 4kt6 glpb l5tc jbc6 yjsn</kbd> into your two factor authenticator app. Spaces and casing do not matter.</p> <divclass="alert alert-info">To enable QR code generation please read our <ahref="https://go.microsoft.com/fwlink/?Linkid=852423">documentation</a>.</div> <divid="qrCode"></div> <divid="qrCodeData"data-url="otpauth://totp/myappname:alice@bob.com?secret=XUHTBBZD3JUV4KT6GLPBL5TCJBC6YJSN&issuer=myappname&digits=6"></div> </li>
What does the vulnerability consist in?
As you can see, the view uses Html.Raw to render the generated authenticator URI, which is confirmed by the fact the & character is not properly HTML-encoded (it should be rendered in the HTML document as & since it's a special character).
Innocently, you might think it's not a big deal since AuthenticatorUri is generated server-side (and thus can't be directly set by an attacker).
Unfortunately, that's not exact: while it's true that adding an AuthenticatorUri paramater will have initially no effect on the GET EnableAuthenticator action (since the value will be always overridden when setting EnableAuthenticatorViewModel.AuthenticatorUri before returning the view), the query string value will be used if the form is re-displayed.
Yet, that's exactly what the POST action does if the model state is not valid (e.g because the 2FA confirmation code was not correctly typed by the user or was invalid, which may happen if the date/time differ between the server and the device that generated the 2FA code):
var user = await _userManager.GetUserAsync(User); if (user == null) { thrownew ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); }
// Strip spaces and hypens var verificationCode = model.Code.Replace(" ", string.Empty).Replace("-", string.Empty);
var is2faTokenValid = await _userManager.VerifyTwoFactorTokenAsync( user, _userManager.Options.Tokens.AuthenticatorTokenProvider, verificationCode);
if (!is2faTokenValid) { ModelState.AddModelError("model.Code", "Verification code is invalid."); return View(model); }
await _userManager.SetTwoFactorEnabledAsync(user, true); _logger.LogInformation("User with ID {UserId} has enabled 2FA with an authenticator app.", user.Id); return RedirectToAction(nameof(GenerateRecoveryCodes)); }
This feature – which is part of ASP.NET Core MVC's model binding/validation stack – is extremely useful since it's what allows the users of your websites to avoid re-typing all the values of an invalid form when it's re-displayed.
The bad news is that using this specificity alongside Html.Raw can result in a XSS vulnerability being exploitable since an attacker can craft a special URL containing a malicious JavaScript payload that will be executed by the victim's browser if he or she sends an invalid 2FA confirmation code.
For instance, if a victim visits https://localhost:44370/Manage/EnableAuthenticator?AuthenticatorUri=%22%3E%3C/div%3E%00%00%00%00%00%00%00%3Cscript%3Ealert(%22XSS%22)%3C/script%3E (which uses a special pattern to bypass Chrome 61's XSS auditor feature) and enters an invalid code, the following HTML source will be rendered:
1 2 3 4 5 6
<li> <p>Scan the QR Code or enter this key <kbd></kbd> into your two factor authenticator app. Spaces and casing do not matter.</p> <divclass="alert alert-info">To enable QR code generation please read our <ahref="https://go.microsoft.com/fwlink/?Linkid=852423">documentation</a>.</div> <divid="qrCode"></div> <divid="qrCodeData"data-url=""></div><script>alert("XSS")</script>"></div> </li>
In some cases, disabling HTML encoding is just unavoidable, for instance if the content is already encoded: not doing that would result in double-encoding.
So, why shouldn't you use Html.Raw in your views? It's actually all about intent: Html.Raw encourages developers to "disable" HTML encoding at the wrong layer (i.e in the view rather than in the controller itself) by declaring already-encoded HTML properties as simple string properties in their view model rather than as HtmlString properties.
Using this type instead of Html.Raw has two big advantages, that would have helped avoid this vulnerability in the first place:
By marking your view model properties as HtmlString, you force the developer in charge of maintaining the controller code to manually create a HtmlString instance from an existing string: new HtmlString(existingEncodedString). Technically, the result is exactly the same, but it's now clear by looking at either your view model or your controller code that a property is not a simple string, but something hightly susceptible of containing HTML payloads you should treat with extreme care.
HtmlString is not bindable: a form model containing a HtmlString will never be populated with user input. In this specific case, exploiting the bug wouldn't have been possible with HtmlString.
In AspNet.Security.OpenIdConnect.Server and Owin.Security.OpenIdConnect.Server 1.0.2
Calling context.HandleResponse() from the SerializeAuthorizationCode, SerializeAccessToken, SerializeIdentityToken and SerializeRefreshToken events no longer throws an exception if the authentication ticket is not explicitly set (c734c6f).
An invalid exception message mentioning OpenIddict was reworded (cd83912).
The authorization code/access token/identity token/refresh token deserialization methods are no longer called twice for introspection and revocation requests that specify a token_type_hint that doesn't match the actual token type (c561a34).
A standard-compliant Expires HTTP header is now returned by the non-interactive ASOS endpoints (5af1d44).
New constants have been added to OpenIdConnectConstants (0980fb8) (461ecd4).
New events allowing to control the sign-in, sign-out and challenge operations have been introduced (d95810b) (3801427).
In AspNet.Security.OpenIdConnect.Server 2.0.0-rc1-final
OpenIdConnectServerProvider can now be resolved from the DI container
To use constructor-injected dependencies in your provider, you can ask ASOS to resolve the provider instance at request-time by setting the new OpenIdConnectServerOptions.ProviderType option (which is a wrapper around AuthenticationSchemeOptions.EventsType):
publicclassStartup { publicvoidConfigureServices(IServiceCollection services) { // Register your custom provider in the DI container. services.AddScoped<AuthorizationProvider>();
// Ask ASOS to resolve the provider instance corresponding // to the specified type when an OIDC request is received. options.ProviderType = typeof(AuthorizationProvider); }); } }
Such feature requires built-in support in the authentication stack and thus, is unfortunately not available in the OWIN/Katana version of ASOS 2.x.
In Owin.Security.OpenIdConnect.Server 2.0.0-rc1-final
The OWIN/Katana version of ASOS 2.x now requires targeting the Microsoft.Owin4.0.0-alpha1 packages, which are natively compatible with IdentityModel 5.2.0-preview1 (unlike the previous iteration).
In OpenIddict 1.0.0-rc1-final and 2.0.0-rc1-final
OpenIddict 1.0.0-rc1-final/2.0.0-rc1-final is the first public version of OpenIddict. To learn more about the changes added since the first betas, don't hesitate to take a look at the GitHub repository.