[Security] Move to GenericHost (#24282)

This commit is contained in:
Kahbazi 2020-07-28 00:39:15 +04:30 committed by GitHub
parent 780d527f0b
commit 6097145096
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 335 additions and 211 deletions

View File

@ -1,18 +1,18 @@
using Microsoft.AspNetCore;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Https;
using Microsoft.Extensions.Hosting;
namespace Certificate.Sample
{
public class Program
{
public static void Main(string[] args)
public static Task Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
=> WebHost.CreateDefaultBuilder(args)
var host = Host.CreateDefaultBuilder(args)
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseStartup<Startup>()
.ConfigureKestrel(options =>
{
@ -20,7 +20,11 @@ namespace Certificate.Sample
{
opt.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
});
});
})
.Build();
return host.RunAsync();
}
}
}

View File

@ -1,26 +1,32 @@
using System.IO;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace CookieSample
{
public static class Program
{
public static void Main(string[] args)
public static Task Main(string[] args)
{
var host = new WebHostBuilder()
var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
})
.ConfigureLogging(factory =>
{
factory.AddConsole();
factory.AddFilter("Console", level => level >= LogLevel.Information);
})
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
return host.RunAsync();
}
}
}

View File

@ -1,26 +1,32 @@
using System.IO;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace CookieSessionSample
{
public static class Program
{
public static void Main(string[] args)
public static Task Main(string[] args)
{
var host = new WebHostBuilder()
var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
})
.ConfigureLogging(factory =>
{
factory.AddConsole();
factory.AddFilter("Console", level => level >= LogLevel.Information);
})
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
return host.RunAsync();
}
}
}

View File

@ -1,4 +1,4 @@
using Microsoft.AspNetCore;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
@ -6,13 +6,17 @@ namespace JwtBearerSample
{
public static class Program
{
public static void Main(string[] args)
public static Task Main(string[] args)
{
var host = WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
var host = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder
.UseStartup<Startup>();
})
.Build();
host.Run();
return host.RunAsync();
}
}
}

View File

@ -1,17 +1,22 @@
using Microsoft.AspNetCore;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace OpenIdConnect.AzureAdSample
{
public static class Program
{
public static void Main(string[] args)
public static Task Main(string[] args)
{
var host = WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
var host = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder
.UseStartup<Startup>();
})
.Build();
host.Run();
return host.RunAsync();
}
}
}

View File

@ -1,18 +1,23 @@
using Microsoft.AspNetCore;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace OpenIdConnectSample
{
public static class Program
{
public static void Main(string[] args)
public static Task Main(string[] args)
{
var host = WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
var host = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder
.UseStartup<Startup>();
})
.Build();
host.Run();
return host.RunAsync();
}
}
}

View File

@ -1,31 +1,23 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace WsFedSample
{
public class Program
{
public static void Main(string[] args)
public static Task Main(string[] args)
{
var host = new WebHostBuilder()
.ConfigureLogging(factory =>
var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
factory.AddConsole();
factory.AddDebug();
factory.AddFilter("Console", level => level >= LogLevel.Information);
factory.AddFilter("Debug", level => level >= LogLevel.Information);
})
webHostBuilder
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, 44307, listenOptions =>
@ -37,10 +29,18 @@ namespace WsFedSample
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseStartup<Startup>();
})
.ConfigureLogging(factory =>
{
factory.AddConsole();
factory.AddDebug();
factory.AddFilter("Console", level => level >= LogLevel.Information);
factory.AddFilter("Debug", level => level >= LogLevel.Information);
})
.Build();
host.Run();
return host.RunAsync();
}
private static X509Certificate2 LoadCertificate()

View File

@ -437,7 +437,22 @@ namespace Microsoft.AspNetCore.Authentication.Test.OpenIdConnect
[Fact]
public async Task MetadataAddressIsGeneratedFromAuthorityWhenMissing()
{
var builder = new WebHostBuilder()
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.Configure(app =>
{
app.UseAuthentication();
app.Run(async context =>
{
var resolver = context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
var handler = await resolver.GetHandlerAsync(context, OpenIdConnectDefaults.AuthenticationScheme) as OpenIdConnectHandler;
Assert.Equal($"{TestServerBuilder.DefaultAuthority}/.well-known/openid-configuration", handler.Options.MetadataAddress);
});
})
.UseTestServer();
})
.ConfigureServices(services =>
{
services.AddAuthentication()
@ -449,17 +464,11 @@ namespace Microsoft.AspNetCore.Authentication.Test.OpenIdConnect
o.SignInScheme = Guid.NewGuid().ToString();
});
})
.Configure(app =>
{
app.UseAuthentication();
app.Run(async context =>
{
var resolver = context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
var handler = await resolver.GetHandlerAsync(context, OpenIdConnectDefaults.AuthenticationScheme) as OpenIdConnectHandler;
Assert.Equal($"{TestServerBuilder.DefaultAuthority}/.well-known/openid-configuration", handler.Options.MetadataAddress);
});
});
var server = new TestServer(builder);
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var transaction = await server.SendAsync(@"https://example.com");
Assert.Equal(HttpStatusCode.OK, transaction.Response.StatusCode);
}

View File

@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
namespace Microsoft.AspNetCore.Authentication
@ -17,7 +18,7 @@ namespace Microsoft.AspNetCore.Authentication
[Fact]
public async Task CanDispatch()
{
var server = CreateServer(services =>
using var server = await CreateServer(services =>
{
services.AddLogging().AddAuthentication(o =>
{
@ -333,7 +334,7 @@ namespace Microsoft.AspNetCore.Authentication
[Fact]
public async Task CanDynamicTargetBasedOnQueryString()
{
var server = CreateServer(services =>
using var server = await CreateServer(services =>
{
services.AddAuthentication(o =>
{
@ -455,9 +456,12 @@ namespace Microsoft.AspNetCore.Authentication
}
}
private static TestServer CreateServer(Action<IServiceCollection> configure = null, string defaultScheme = null)
private static async Task<TestServer> CreateServer(Action<IServiceCollection> configure = null, string defaultScheme = null)
{
var builder = new WebHostBuilder()
var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.Configure(app =>
{
app.UseAuthentication();
@ -477,11 +481,19 @@ namespace Microsoft.AspNetCore.Authentication
}
});
})
.UseTestServer();
})
.ConfigureServices(services =>
{
configure?.Invoke(services);
});
return new TestServer(builder);
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
return server;
}
}
}

View File

@ -1,26 +1,32 @@
using System.IO;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace CookiePolicySample
{
public static class Program
{
public static void Main(string[] args)
public static Task Main(string[] args)
{
var host = new WebHostBuilder()
var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
})
.ConfigureLogging(factory =>
{
factory.AddConsole();
factory.AddFilter("Microsoft", LogLevel.Trace);
})
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
return host.RunAsync();
}
}
}

View File

@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Net.Http.Headers;
using Xunit;
@ -641,20 +642,30 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
Assert.NotNull(manualCookie.Expires); // Expires may not exactly match to the second.
}
private Task<HttpContext> RunTestAsync(Action<CookiePolicyOptions> configureOptions, Action<HttpContext> configureRequest, RequestDelegate handleRequest)
private async Task<HttpContext> RunTestAsync(Action<CookiePolicyOptions> configureOptions, Action<HttpContext> configureRequest, RequestDelegate handleRequest)
{
var builder = new WebHostBuilder()
.ConfigureServices(services =>
var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
services.Configure(configureOptions);
})
webHostBuilder
.Configure(app =>
{
app.UseCookiePolicy();
app.Run(handleRequest);
});
var server = new TestServer(builder);
return server.SendAsync(configureRequest);
})
.UseTestServer();
})
.ConfigureServices(services =>
{
services.Configure(configureOptions);
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
return await server.SendAsync(configureRequest);
}
}
}

View File

@ -13,6 +13,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Net.Http.Headers;
using Xunit;
@ -244,7 +245,10 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
[Fact]
public async Task CookiePolicyCanHijackAppend()
{
var builder = new WebHostBuilder()
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.Configure(app =>
{
app.UseCookiePolicy(new CookiePolicyOptions
@ -259,8 +263,14 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
context.Response.Cookies.Append("D", "D", new CookieOptions { Secure = true });
return Task.FromResult(0);
});
});
var server = new TestServer(builder);
})
.UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var transaction = await server.SendAsync("http://example.com/login");
@ -274,7 +284,10 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
[Fact]
public async Task CookiePolicyCanHijackDelete()
{
var builder = new WebHostBuilder()
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.Configure(app =>
{
app.UseCookiePolicy(new CookiePolicyOptions
@ -289,8 +302,14 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
context.Response.Cookies.Delete("D", new CookieOptions { Secure = true });
return Task.FromResult(0);
});
});
var server = new TestServer(builder);
})
.UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var transaction = await server.SendAsync("http://example.com/login");
@ -302,7 +321,10 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
[Fact]
public async Task CookiePolicyCallsCookieFeature()
{
var builder = new WebHostBuilder()
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.Configure(app =>
{
app.Use(next => context =>
@ -322,8 +344,14 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
Assert.Throws<NotImplementedException>(() => context.Response.Cookies.Append("A", "A", new CookieOptions()));
return context.Response.WriteAsync("Done");
});
});
var server = new TestServer(builder);
})
.UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var transaction = await server.SendAsync("http://example.com/login");
Assert.Equal("Done", transaction.ResponseText);
@ -332,16 +360,10 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
[Fact]
public async Task CookiePolicyAppliesToCookieAuth()
{
var builder = new WebHostBuilder()
.ConfigureServices(services =>
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
services.AddAuthentication().AddCookie(o =>
{
o.Cookie.Name = "TestCookie";
o.Cookie.HttpOnly = false;
o.Cookie.SecurePolicy = CookieSecurePolicy.None;
});
})
webHostBuilder
.Configure(app =>
{
app.UseCookiePolicy(new CookiePolicyOptions
@ -355,8 +377,23 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
return context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity("TestUser", "Cookies"))));
});
})
.UseTestServer();
})
.ConfigureServices(services =>
{
services.AddAuthentication().AddCookie(o =>
{
o.Cookie.Name = "TestCookie";
o.Cookie.HttpOnly = false;
o.Cookie.SecurePolicy = CookieSecurePolicy.None;
});
var server = new TestServer(builder);
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var transaction = await server.SendAsync("http://example.com/login");
@ -372,16 +409,10 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
[Fact]
public async Task CookiePolicyAppliesToCookieAuthChunks()
{
var builder = new WebHostBuilder()
.ConfigureServices(services =>
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
services.AddAuthentication().AddCookie(o =>
{
o.Cookie.Name = "TestCookie";
o.Cookie.HttpOnly = false;
o.Cookie.SecurePolicy = CookieSecurePolicy.None;
});
})
webHostBuilder
.Configure(app =>
{
app.UseCookiePolicy(new CookiePolicyOptions
@ -395,8 +426,23 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
return context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity(new string('c', 1024 * 5), "Cookies"))));
});
})
.UseTestServer();
})
.ConfigureServices(services =>
{
services.AddAuthentication().AddCookie(o =>
{
o.Cookie.Name = "TestCookie";
o.Cookie.HttpOnly = false;
o.Cookie.SecurePolicy = CookieSecurePolicy.None;
});
var server = new TestServer(builder);
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var transaction = await server.SendAsync("http://example.com/login");
@ -475,7 +521,10 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
RequestDelegate configureSetup,
params RequestTest[] tests)
{
var builder = new WebHostBuilder()
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.Configure(app =>
{
app.Map(path, map =>
@ -483,8 +532,15 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
map.UseCookiePolicy(cookiePolicy);
map.Run(configureSetup);
});
});
var server = new TestServer(builder);
})
.UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
foreach (var test in tests)
{
await test.Execute(server);