From 75b7e26efec7ddd25561bd3e4b3d05cac805b333 Mon Sep 17 00:00:00 2001 From: Hinton Date: Fri, 17 Dec 2021 17:18:51 +0100 Subject: [PATCH 1/8] Remove icon service dependency on core --- bitwarden-server.sln | 7 ++++ src/Icons/Controllers/InfoController.cs | 4 +- src/Icons/Icons.csproj | 2 +- src/Icons/Program.cs | 38 ++++++++++++------- src/Icons/Startup.cs | 11 ++---- src/SharedKernel/SharedKernel.csproj | 15 ++++++++ .../Utilities/SecurityHeadersMiddleware.cs | 30 +++++++++++++++ src/SharedKernel/Utilities/VersionHelper.cs | 21 ++++++++++ 8 files changed, 104 insertions(+), 24 deletions(-) create mode 100644 src/SharedKernel/SharedKernel.csproj create mode 100644 src/SharedKernel/Utilities/SecurityHeadersMiddleware.cs create mode 100644 src/SharedKernel/Utilities/VersionHelper.cs diff --git a/bitwarden-server.sln b/bitwarden-server.sln index 50095d72591f..b894650134c9 100644 --- a/bitwarden-server.sln +++ b/bitwarden-server.sln @@ -76,6 +76,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostgresMigrations", "util\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "test\Common\Common.csproj", "{17DA09D7-0212-4009-879E-6B9CFDE5FA60}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharedKernel", "src\SharedKernel\SharedKernel.csproj", "{55D65CC6-6F00-40DD-9EFC-9ED1F301A4F3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -172,6 +174,10 @@ Global {17DA09D7-0212-4009-879E-6B9CFDE5FA60}.Debug|Any CPU.Build.0 = Debug|Any CPU {17DA09D7-0212-4009-879E-6B9CFDE5FA60}.Release|Any CPU.ActiveCfg = Release|Any CPU {17DA09D7-0212-4009-879E-6B9CFDE5FA60}.Release|Any CPU.Build.0 = Release|Any CPU + {55D65CC6-6F00-40DD-9EFC-9ED1F301A4F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {55D65CC6-6F00-40DD-9EFC-9ED1F301A4F3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {55D65CC6-6F00-40DD-9EFC-9ED1F301A4F3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {55D65CC6-6F00-40DD-9EFC-9ED1F301A4F3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -199,6 +205,7 @@ Global {EDC0D688-D58C-4CE1-AA07-3606AC6874B8} = {4FDB6543-F68B-4202-9EA6-7FEA984D2D0A} {0E99A21B-684B-4C59-9831-90F775CAB6F7} = {287CFF34-BBDB-4BC4-AF88-1E19A5A4679B} {17DA09D7-0212-4009-879E-6B9CFDE5FA60} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84F} + {55D65CC6-6F00-40DD-9EFC-9ED1F301A4F3} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {E01CBF68-2E20-425F-9EDB-E0A6510CA92F} diff --git a/src/Icons/Controllers/InfoController.cs b/src/Icons/Controllers/InfoController.cs index 7897569e8e27..fc36119e4b53 100644 --- a/src/Icons/Controllers/InfoController.cs +++ b/src/Icons/Controllers/InfoController.cs @@ -1,5 +1,5 @@ using System; -using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Microsoft.AspNetCore.Mvc; namespace Bit.Icons.Controllers @@ -16,7 +16,7 @@ public DateTime GetAlive() [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(VersionHelper.GetVersion()); } } } diff --git a/src/Icons/Icons.csproj b/src/Icons/Icons.csproj index 31fa723a04a1..6b6116edf1e5 100644 --- a/src/Icons/Icons.csproj +++ b/src/Icons/Icons.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/Icons/Program.cs b/src/Icons/Program.cs index 0fba981117fc..92958ea545f4 100644 --- a/src/Icons/Program.cs +++ b/src/Icons/Program.cs @@ -1,7 +1,6 @@ -using Bit.Core.Utilities; -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; -using Serilog.Events; +using Serilog; namespace Bit.Icons { @@ -9,16 +8,29 @@ public class Program { public static void Main(string[] args) { - Host - .CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - webBuilder.ConfigureLogging((hostingContext, logging) => - logging.AddSerilog(hostingContext, e => e.Level >= LogEventLevel.Error)); - }) - .Build() - .Run(); + Log.Logger = new LoggerConfiguration() + .WriteTo.Console() + .CreateBootstrapLogger(); + + try + { + Host + .CreateDefaultBuilder(args) + .UseSerilog((context, configuration) => + { + configuration.ReadFrom.Configuration(context.Configuration); + }) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }) + .Build() + .Run(); + } + finally + { + Log.CloseAndFlush(); + } } } } diff --git a/src/Icons/Startup.cs b/src/Icons/Startup.cs index 1d53d34c26d8..71ff966815a9 100644 --- a/src/Icons/Startup.cs +++ b/src/Icons/Startup.cs @@ -1,8 +1,7 @@ using System; using System.Globalization; -using Bit.Core.Settings; -using Bit.Core.Utilities; using Bit.Icons.Services; +using Bit.SharedKernel.Utilities; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; @@ -29,10 +28,9 @@ public void ConfigureServices(IServiceCollection services) services.AddOptions(); // Settings - var globalSettings = services.AddGlobalSettingsServices(Configuration); var iconsSettings = new IconsSettings(); ConfigurationBinder.Bind(Configuration.GetSection("IconsSettings"), iconsSettings); - services.AddSingleton(s => iconsSettings); + services.AddSingleton(_ => iconsSettings); // Cache services.AddMemoryCache(options => @@ -51,11 +49,8 @@ public void ConfigureServices(IServiceCollection services) public void Configure( IApplicationBuilder app, IWebHostEnvironment env, - IHostApplicationLifetime appLifetime, - GlobalSettings globalSettings) + IHostApplicationLifetime appLifetime) { - app.UseSerilog(env, appLifetime, globalSettings); - // Add general security headers app.UseMiddleware(); diff --git a/src/SharedKernel/SharedKernel.csproj b/src/SharedKernel/SharedKernel.csproj new file mode 100644 index 000000000000..18cf9dc0dbb5 --- /dev/null +++ b/src/SharedKernel/SharedKernel.csproj @@ -0,0 +1,15 @@ + + + + enable + + + + + + + + + + + diff --git a/src/SharedKernel/Utilities/SecurityHeadersMiddleware.cs b/src/SharedKernel/Utilities/SecurityHeadersMiddleware.cs new file mode 100644 index 000000000000..cc4b25b20535 --- /dev/null +++ b/src/SharedKernel/Utilities/SecurityHeadersMiddleware.cs @@ -0,0 +1,30 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Primitives; + +namespace Bit.SharedKernel.Utilities +{ + public sealed class SecurityHeadersMiddleware + { + private readonly RequestDelegate _next; + + public SecurityHeadersMiddleware(RequestDelegate next) + { + _next = next; + } + + public Task Invoke(HttpContext context) + { + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options + context.Response.Headers.Add("x-frame-options", new StringValues("SAMEORIGIN")); + + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection + context.Response.Headers.Add("x-xss-protection", new StringValues("1; mode=block")); + + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options + context.Response.Headers.Add("x-content-type-options", new StringValues("nosniff")); + + return _next(context); + } + } +} diff --git a/src/SharedKernel/Utilities/VersionHelper.cs b/src/SharedKernel/Utilities/VersionHelper.cs new file mode 100644 index 000000000000..e284361525bb --- /dev/null +++ b/src/SharedKernel/Utilities/VersionHelper.cs @@ -0,0 +1,21 @@ +using System.Reflection; + +namespace Bit.SharedKernel.Utilities +{ + public static class VersionHelper + { + private static string? _version; + + public static string? GetVersion() + { + if (string.IsNullOrWhiteSpace(_version)) + { + _version = Assembly.GetEntryAssembly() + .GetCustomAttribute() + .InformationalVersion; + } + + return _version; + } + } +} From 24deb9c8a7b32534a3f9777612c135d94e5a3ffa Mon Sep 17 00:00:00 2001 From: Hinton Date: Fri, 17 Dec 2021 17:31:54 +0100 Subject: [PATCH 2/8] Add configs --- src/Icons/appsettings.Development.json | 5 +++++ src/Icons/appsettings.json | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/Icons/appsettings.Development.json b/src/Icons/appsettings.Development.json index fa8ce71a97a3..8ed9de65b181 100644 --- a/src/Icons/appsettings.Development.json +++ b/src/Icons/appsettings.Development.json @@ -6,5 +6,10 @@ "System": "Information", "Microsoft": "Information" } + }, + "Serilog": { + "WriteTo": [ + { "Name": "Console" } + ] } } diff --git a/src/Icons/appsettings.json b/src/Icons/appsettings.json index 65267ef4e917..7d7d45b32b1d 100644 --- a/src/Icons/appsettings.json +++ b/src/Icons/appsettings.json @@ -10,5 +10,8 @@ "cacheEnabled": true, "cacheHours": 24, "cacheSizeLimit": null + }, + "Serilog": { + "Enrich": [ "FromLogContext" ] } } From c8da2bab07539a3732732056f1d1eb76e27bd523 Mon Sep 17 00:00:00 2001 From: Hinton Date: Wed, 22 Dec 2021 09:41:01 +0100 Subject: [PATCH 3/8] Use SharedKernels VersionHelper everywhere --- .../src/Sso/Controllers/InfoController.cs | 4 ++-- .../src/Sso/Views/Shared/_Layout.cshtml | 2 +- src/Admin/Controllers/HomeController.cs | 3 ++- src/Admin/Controllers/InfoController.cs | 3 ++- src/Api/Controllers/InfoController.cs | 4 ++-- src/Billing/Controllers/InfoController.cs | 4 ++-- src/Core/Core.csproj | 10 ++++------ src/Core/Utilities/CoreHelpers.cs | 12 ------------ src/Core/Utilities/ServiceCollectionExtensions.cs | 5 ++--- src/Events/Controllers/InfoController.cs | 4 ++-- src/EventsProcessor/Startup.cs | 4 +++- src/Icons/Program.cs | 7 ++++++- src/Icons/appsettings.json | 5 ++++- src/Identity/Controllers/InfoController.cs | 4 ++-- src/Notifications/Controllers/InfoController.cs | 4 ++-- src/Notifications/Notifications.csproj | 1 + src/SharedKernel/Utilities/VersionHelper.cs | 6 +++--- 17 files changed, 40 insertions(+), 42 deletions(-) diff --git a/bitwarden_license/src/Sso/Controllers/InfoController.cs b/bitwarden_license/src/Sso/Controllers/InfoController.cs index 7f3058ae95b0..063238a22d9c 100644 --- a/bitwarden_license/src/Sso/Controllers/InfoController.cs +++ b/bitwarden_license/src/Sso/Controllers/InfoController.cs @@ -1,5 +1,5 @@ using System; -using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Microsoft.AspNetCore.Mvc; namespace Bit.Sso.Controllers @@ -16,7 +16,7 @@ public DateTime GetAlive() [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(VersionHelper.GetVersion()); } } } diff --git a/bitwarden_license/src/Sso/Views/Shared/_Layout.cshtml b/bitwarden_license/src/Sso/Views/Shared/_Layout.cshtml index 4467f08f255c..46c6de335d91 100644 --- a/bitwarden_license/src/Sso/Views/Shared/_Layout.cshtml +++ b/bitwarden_license/src/Sso/Views/Shared/_Layout.cshtml @@ -1,4 +1,4 @@ -@using static Bit.Core.Utilities.CoreHelpers; +@using static Bit.SharedKernel.Utilities.VersionHelper; diff --git a/src/Admin/Controllers/HomeController.cs b/src/Admin/Controllers/HomeController.cs index f2c899064c96..b9b7bdac3296 100644 --- a/src/Admin/Controllers/HomeController.cs +++ b/src/Admin/Controllers/HomeController.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Bit.Admin.Models; using Bit.Core.Settings; +using Bit.SharedKernel.Utilities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; @@ -25,7 +26,7 @@ public IActionResult Index() return View(new HomeModel { GlobalSettings = _globalSettings, - CurrentVersion = Core.Utilities.CoreHelpers.GetVersion() + CurrentVersion = VersionHelper.GetVersion() }); } diff --git a/src/Admin/Controllers/InfoController.cs b/src/Admin/Controllers/InfoController.cs index e09f0a9f3022..29870d18e4dc 100644 --- a/src/Admin/Controllers/InfoController.cs +++ b/src/Admin/Controllers/InfoController.cs @@ -1,5 +1,6 @@ using System; using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Microsoft.AspNetCore.Mvc; namespace Bit.Admin.Controllers @@ -16,7 +17,7 @@ public DateTime GetAlive() [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(VersionHelper.GetVersion()); } } } diff --git a/src/Api/Controllers/InfoController.cs b/src/Api/Controllers/InfoController.cs index 82aa7b938be3..bcedd2d469a5 100644 --- a/src/Api/Controllers/InfoController.cs +++ b/src/Api/Controllers/InfoController.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; -using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Microsoft.AspNetCore.Mvc; namespace Bit.Api.Controllers @@ -18,7 +18,7 @@ public DateTime GetAlive() [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(VersionHelper.GetVersion()); } [HttpGet("~/ip")] diff --git a/src/Billing/Controllers/InfoController.cs b/src/Billing/Controllers/InfoController.cs index 016bee2c4d2a..92c26f1f0410 100644 --- a/src/Billing/Controllers/InfoController.cs +++ b/src/Billing/Controllers/InfoController.cs @@ -1,5 +1,5 @@ using System; -using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Microsoft.AspNetCore.Mvc; namespace Bit.Billing.Controllers @@ -16,7 +16,7 @@ public DateTime GetAlive() [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(VersionHelper.GetVersion()); } } } diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index c2bdbb01dff4..2415b06a88d5 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -42,15 +42,9 @@ - - - - - - @@ -66,4 +60,8 @@ + + + + diff --git a/src/Core/Utilities/CoreHelpers.cs b/src/Core/Utilities/CoreHelpers.cs index 42202227e547..3d7bfd561517 100644 --- a/src/Core/Utilities/CoreHelpers.cs +++ b/src/Core/Utilities/CoreHelpers.cs @@ -560,18 +560,6 @@ public static string FormatLicenseSignatureValue(object val) return val.ToString(); } - public static string GetVersion() - { - if (string.IsNullOrWhiteSpace(_version)) - { - _version = Assembly.GetEntryAssembly() - .GetCustomAttribute() - .InformationalVersion; - } - - return _version; - } - public static string Dvorak2Qwerty(string value) { return Other2Qwerty(value, _dvorakMap, _qwertyDvorakMap); diff --git a/src/Core/Utilities/ServiceCollectionExtensions.cs b/src/Core/Utilities/ServiceCollectionExtensions.cs index c9bf1f8a7142..20ae241ffc37 100644 --- a/src/Core/Utilities/ServiceCollectionExtensions.cs +++ b/src/Core/Utilities/ServiceCollectionExtensions.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Linq; using System.Reflection; using System.Security.Claims; using System.Security.Cryptography.X509Certificates; @@ -14,7 +13,7 @@ using Bit.Core.Resources; using Bit.Core.Services; using Bit.Core.Settings; -using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using IdentityModel; using IdentityServer4.AccessTokenValidation; using IdentityServer4.Configuration; @@ -554,7 +553,7 @@ string GetHeaderValue(HttpContext httpContext, string header) { httpContext.Response.OnStarting((state) => { - httpContext.Response.Headers.Append("Server-Version", CoreHelpers.GetVersion()); + httpContext.Response.Headers.Append("Server-Version", VersionHelper.GetVersion()); return Task.FromResult(0); }, null); await next.Invoke(); diff --git a/src/Events/Controllers/InfoController.cs b/src/Events/Controllers/InfoController.cs index 3a14ea6f7fb5..99133b10699c 100644 --- a/src/Events/Controllers/InfoController.cs +++ b/src/Events/Controllers/InfoController.cs @@ -1,5 +1,5 @@ using System; -using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Microsoft.AspNetCore.Mvc; namespace Bit.Events.Controllers @@ -16,7 +16,7 @@ public DateTime GetAlive() [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(VersionHelper.GetVersion()); } } } diff --git a/src/EventsProcessor/Startup.cs b/src/EventsProcessor/Startup.cs index fd437b96fb3f..8b78ff35c739 100644 --- a/src/EventsProcessor/Startup.cs +++ b/src/EventsProcessor/Startup.cs @@ -1,6 +1,7 @@ using System.Globalization; using Bit.Core.Settings; using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; @@ -8,6 +9,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Logging; +using SecurityHeadersMiddleware = Bit.Core.Utilities.SecurityHeadersMiddleware; namespace Bit.EventsProcessor { @@ -53,7 +55,7 @@ public void Configure( endpoints.MapGet("/now", async context => await context.Response.WriteAsJsonAsync(System.DateTime.UtcNow)); endpoints.MapGet("/version", - async context => await context.Response.WriteAsJsonAsync(CoreHelpers.GetVersion())); + async context => await context.Response.WriteAsJsonAsync(VersionHelper.GetVersion())); }); } diff --git a/src/Icons/Program.cs b/src/Icons/Program.cs index 92958ea545f4..3aec0c7d60dc 100644 --- a/src/Icons/Program.cs +++ b/src/Icons/Program.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Hosting; +using System; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; using Serilog; @@ -27,6 +28,10 @@ public static void Main(string[] args) .Build() .Run(); } + catch (Exception ex) + { + Log.Fatal(ex, "Host terminated unexpectedly"); + } finally { Log.CloseAndFlush(); diff --git a/src/Icons/appsettings.json b/src/Icons/appsettings.json index 7d7d45b32b1d..7c3271bfcee2 100644 --- a/src/Icons/appsettings.json +++ b/src/Icons/appsettings.json @@ -12,6 +12,9 @@ "cacheSizeLimit": null }, "Serilog": { - "Enrich": [ "FromLogContext" ] + "Enrich": [ "FromLogContext" ], // TODO: Figure out filter for inclusionPredicate + "Properties": { + "Project": "Icons" + } } } diff --git a/src/Identity/Controllers/InfoController.cs b/src/Identity/Controllers/InfoController.cs index 9824ac93a90d..e12daeb982a2 100644 --- a/src/Identity/Controllers/InfoController.cs +++ b/src/Identity/Controllers/InfoController.cs @@ -1,5 +1,5 @@ using System; -using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Microsoft.AspNetCore.Mvc; namespace Bit.Identity.Controllers @@ -16,7 +16,7 @@ public DateTime GetAlive() [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(VersionHelper.GetVersion()); } } } diff --git a/src/Notifications/Controllers/InfoController.cs b/src/Notifications/Controllers/InfoController.cs index d9169724316d..3c9b8d13a503 100644 --- a/src/Notifications/Controllers/InfoController.cs +++ b/src/Notifications/Controllers/InfoController.cs @@ -1,5 +1,5 @@ using System; -using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Microsoft.AspNetCore.Mvc; namespace Bit.Notifications.Controllers @@ -16,7 +16,7 @@ public DateTime GetAlive() [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(VersionHelper.GetVersion()); } } } diff --git a/src/Notifications/Notifications.csproj b/src/Notifications/Notifications.csproj index fc5016613b8d..31226ebed23f 100644 --- a/src/Notifications/Notifications.csproj +++ b/src/Notifications/Notifications.csproj @@ -12,6 +12,7 @@ + diff --git a/src/SharedKernel/Utilities/VersionHelper.cs b/src/SharedKernel/Utilities/VersionHelper.cs index e284361525bb..a5a2b1a4a013 100644 --- a/src/SharedKernel/Utilities/VersionHelper.cs +++ b/src/SharedKernel/Utilities/VersionHelper.cs @@ -6,12 +6,12 @@ public static class VersionHelper { private static string? _version; - public static string? GetVersion() + public static string GetVersion() { if (string.IsNullOrWhiteSpace(_version)) { - _version = Assembly.GetEntryAssembly() - .GetCustomAttribute() + _version = Assembly.GetEntryAssembly()! + .GetCustomAttribute()! .InformationalVersion; } From 2ce65744032589d5c5fce2d177b894c975564d1b Mon Sep 17 00:00:00 2001 From: Hinton Date: Wed, 12 Jan 2022 13:41:37 +0100 Subject: [PATCH 4/8] Remove duplicate SecurityHeadersMiddleware --- bitwarden_license/src/Sso/Startup.cs | 1 + src/Admin/Startup.cs | 1 + src/Api/Startup.cs | 1 + src/Billing/Startup.cs | 1 + .../Utilities/SecurityHeadersMiddleware.cs | 30 ------------------- src/Events/Startup.cs | 1 + src/EventsProcessor/Startup.cs | 1 - src/Icons/Icons.csproj | 2 -- src/Identity/Startup.cs | 1 + src/Notifications/Startup.cs | 1 + 10 files changed, 7 insertions(+), 33 deletions(-) delete mode 100644 src/Core/Utilities/SecurityHeadersMiddleware.cs diff --git a/bitwarden_license/src/Sso/Startup.cs b/bitwarden_license/src/Sso/Startup.cs index 2b45504543e0..ff36363ddc27 100644 --- a/bitwarden_license/src/Sso/Startup.cs +++ b/bitwarden_license/src/Sso/Startup.cs @@ -3,6 +3,7 @@ using Bit.Core.Context; using Bit.Core.Settings; using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Bit.SharedWeb.Utilities; using Bit.Sso.Utilities; using IdentityServer4.Extensions; diff --git a/src/Admin/Startup.cs b/src/Admin/Startup.cs index 8ba71017b850..f2a3a984bf4b 100644 --- a/src/Admin/Startup.cs +++ b/src/Admin/Startup.cs @@ -4,6 +4,7 @@ using Bit.Core.Identity; using Bit.Core.Settings; using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Bit.SharedWeb.Utilities; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; diff --git a/src/Api/Startup.cs b/src/Api/Startup.cs index 1953064071a0..2540b745f750 100644 --- a/src/Api/Startup.cs +++ b/src/Api/Startup.cs @@ -17,6 +17,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using System.Collections.Generic; +using Bit.SharedKernel.Utilities; using Bit.SharedWeb.Utilities; #if !OSS diff --git a/src/Billing/Startup.cs b/src/Billing/Startup.cs index 28f17ef8cba1..cee7ea74f96c 100644 --- a/src/Billing/Startup.cs +++ b/src/Billing/Startup.cs @@ -3,6 +3,7 @@ using Bit.Core.Context; using Bit.Core.Settings; using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Bit.SharedWeb.Utilities; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; diff --git a/src/Core/Utilities/SecurityHeadersMiddleware.cs b/src/Core/Utilities/SecurityHeadersMiddleware.cs deleted file mode 100644 index 2f11ee3315db..000000000000 --- a/src/Core/Utilities/SecurityHeadersMiddleware.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Primitives; - -namespace Bit.Core.Utilities -{ - public sealed class SecurityHeadersMiddleware - { - private readonly RequestDelegate _next; - - public SecurityHeadersMiddleware(RequestDelegate next) - { - _next = next; - } - - public Task Invoke(HttpContext context) - { - // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options - context.Response.Headers.Add("x-frame-options", new StringValues("SAMEORIGIN")); - - // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection - context.Response.Headers.Add("x-xss-protection", new StringValues("1; mode=block")); - - // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options - context.Response.Headers.Add("x-content-type-options", new StringValues("nosniff")); - - return _next(context); - } - } -} diff --git a/src/Events/Startup.cs b/src/Events/Startup.cs index 6cf103df690b..14bc16c12b6c 100644 --- a/src/Events/Startup.cs +++ b/src/Events/Startup.cs @@ -3,6 +3,7 @@ using Bit.Core.Services; using Bit.Core.Settings; using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Bit.SharedWeb.Utilities; using IdentityModel; using Microsoft.AspNetCore.Builder; diff --git a/src/EventsProcessor/Startup.cs b/src/EventsProcessor/Startup.cs index e92156acfe48..04eab2500847 100644 --- a/src/EventsProcessor/Startup.cs +++ b/src/EventsProcessor/Startup.cs @@ -10,7 +10,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Logging; -using SecurityHeadersMiddleware = Bit.Core.Utilities.SecurityHeadersMiddleware; namespace Bit.EventsProcessor { diff --git a/src/Icons/Icons.csproj b/src/Icons/Icons.csproj index 5f62052fcb71..6b6116edf1e5 100644 --- a/src/Icons/Icons.csproj +++ b/src/Icons/Icons.csproj @@ -15,8 +15,6 @@ - - diff --git a/src/Identity/Startup.cs b/src/Identity/Startup.cs index 7584bf99f3eb..5d1f702efdaf 100644 --- a/src/Identity/Startup.cs +++ b/src/Identity/Startup.cs @@ -8,6 +8,7 @@ using Bit.Core.Settings; using Bit.Core.Utilities; using Bit.Identity.Utilities; +using Bit.SharedKernel.Utilities; using Bit.SharedWeb.Utilities; using IdentityServer4.Extensions; using Microsoft.AspNetCore.Builder; diff --git a/src/Notifications/Startup.cs b/src/Notifications/Startup.cs index 4ca7b728ecb9..6a1f4477821e 100644 --- a/src/Notifications/Startup.cs +++ b/src/Notifications/Startup.cs @@ -2,6 +2,7 @@ using System.Globalization; using Bit.Core.Settings; using Bit.Core.Utilities; +using Bit.SharedKernel.Utilities; using Bit.SharedWeb.Utilities; using IdentityModel; using Microsoft.AspNetCore.Builder; From abd4a19c4bd3e34ce23576f2250213a0852b0e52 Mon Sep 17 00:00:00 2001 From: Hinton Date: Wed, 12 Jan 2022 14:05:16 +0100 Subject: [PATCH 5/8] Add Serilog.Expressions --- src/Icons/appsettings.json | 12 ++++++++++-- src/Notifications/Notifications.csproj | 1 - src/SharedKernel/SharedKernel.csproj | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Icons/appsettings.json b/src/Icons/appsettings.json index 7c3271bfcee2..7ea1a32ac4ca 100644 --- a/src/Icons/appsettings.json +++ b/src/Icons/appsettings.json @@ -12,9 +12,17 @@ "cacheSizeLimit": null }, "Serilog": { - "Enrich": [ "FromLogContext" ], // TODO: Figure out filter for inclusionPredicate + "Enrich": [ "FromLogContext" ], "Properties": { "Project": "Icons" - } + }, + "Filter": [ + { + "Name": "ByExcluding", + "Args": { + "expression": "EventId.Id = 12482444" + } + } + ] } } diff --git a/src/Notifications/Notifications.csproj b/src/Notifications/Notifications.csproj index 964e5195473c..57a3b46cab29 100644 --- a/src/Notifications/Notifications.csproj +++ b/src/Notifications/Notifications.csproj @@ -11,7 +11,6 @@ - diff --git a/src/SharedKernel/SharedKernel.csproj b/src/SharedKernel/SharedKernel.csproj index 18cf9dc0dbb5..cd14aae77f11 100644 --- a/src/SharedKernel/SharedKernel.csproj +++ b/src/SharedKernel/SharedKernel.csproj @@ -7,6 +7,7 @@ + From 400a2f64ee7fe4e8eca4da4a76458ea1565b73ae Mon Sep 17 00:00:00 2001 From: Hinton Date: Thu, 13 Jan 2022 18:30:09 +0100 Subject: [PATCH 6/8] Cleanup Icons logging config --- src/Icons/appsettings.Development.json | 8 -------- src/Icons/appsettings.Production.json | 18 +++--------------- src/Icons/appsettings.json | 10 +--------- 3 files changed, 4 insertions(+), 32 deletions(-) diff --git a/src/Icons/appsettings.Development.json b/src/Icons/appsettings.Development.json index 8ed9de65b181..53f9e17ceee4 100644 --- a/src/Icons/appsettings.Development.json +++ b/src/Icons/appsettings.Development.json @@ -1,12 +1,4 @@ { - "Logging": { - "IncludeScopes": false, - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } - }, "Serilog": { "WriteTo": [ { "Name": "Console" } diff --git a/src/Icons/appsettings.Production.json b/src/Icons/appsettings.Production.json index 437045a7fbb3..e1facf6fad1a 100644 --- a/src/Icons/appsettings.Production.json +++ b/src/Icons/appsettings.Production.json @@ -1,19 +1,7 @@ { - "Logging": { - "IncludeScopes": false, - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - }, - "Console": { - "IncludeScopes": true, - "LogLevel": { - "Default": "Warning", - "System": "Warning", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } + "Serilog": { + "MinimumLevel": { + "Default": "Error" } } } diff --git a/src/Icons/appsettings.json b/src/Icons/appsettings.json index 7ea1a32ac4ca..cf495b1947fa 100644 --- a/src/Icons/appsettings.json +++ b/src/Icons/appsettings.json @@ -15,14 +15,6 @@ "Enrich": [ "FromLogContext" ], "Properties": { "Project": "Icons" - }, - "Filter": [ - { - "Name": "ByExcluding", - "Args": { - "expression": "EventId.Id = 12482444" - } - } - ] + } } } From d9667ea6e555137f85c6b3aeb1aa2fb2ed23d4b9 Mon Sep 17 00:00:00 2001 From: Hinton Date: Thu, 13 Jan 2022 18:30:39 +0100 Subject: [PATCH 7/8] Add new Serilog configuration to Identity --- src/Identity/Program.cs | 57 ++++++++++++----------- src/Identity/appsettings.Development.json | 5 ++ src/Identity/appsettings.Production.json | 21 +++------ src/Identity/appsettings.json | 6 +++ 4 files changed, 46 insertions(+), 43 deletions(-) diff --git a/src/Identity/Program.cs b/src/Identity/Program.cs index a1397cd4ce5b..923adcad0143 100644 --- a/src/Identity/Program.cs +++ b/src/Identity/Program.cs @@ -1,8 +1,8 @@ -using AspNetCoreRateLimit; +using System; using Bit.Core.Utilities; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; -using Serilog.Events; +using Serilog; namespace Bit.Identity { @@ -10,33 +10,34 @@ public class Program { public static void Main(string[] args) { - Host - .CreateDefaultBuilder(args) - .ConfigureCustomAppConfiguration(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - webBuilder.ConfigureLogging((hostingContext, logging) => - logging.AddSerilog(hostingContext, e => - { - var context = e.Properties["SourceContext"].ToString(); - if (context.Contains(typeof(IpRateLimitMiddleware).FullName) && - e.Level == LogEventLevel.Information) - { - return true; - } + Log.Logger = new LoggerConfiguration() + .WriteTo.Console() + .CreateBootstrapLogger(); - if (context.Contains("IdentityServer4.Validation.TokenValidator") || - context.Contains("IdentityServer4.Validation.TokenRequestValidator")) - { - return e.Level > LogEventLevel.Error; - } - - return e.Level >= LogEventLevel.Error; - })); - }) - .Build() - .Run(); + try + { + Host + .CreateDefaultBuilder(args) + .ConfigureCustomAppConfiguration(args) + .UseSerilog((context, configuration) => + { + configuration.ReadFrom.Configuration(context.Configuration); + }) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }) + .Build() + .Run(); + } + catch (Exception ex) + { + Log.Fatal(ex, "Host terminated unexpectedly"); + } + finally + { + Log.CloseAndFlush(); + } } } } diff --git a/src/Identity/appsettings.Development.json b/src/Identity/appsettings.Development.json index 950c72818782..2e162080fac8 100644 --- a/src/Identity/appsettings.Development.json +++ b/src/Identity/appsettings.Development.json @@ -26,5 +26,10 @@ "storage": { "connectionString": "UseDevelopmentStorage=true" } + }, + "Serilog": { + "WriteTo": [ + { "Name": "Console" } + ] } } diff --git a/src/Identity/appsettings.Production.json b/src/Identity/appsettings.Production.json index 4f05fcec9f25..d5c951d5e9ed 100644 --- a/src/Identity/appsettings.Production.json +++ b/src/Identity/appsettings.Production.json @@ -18,21 +18,12 @@ "production": true } }, - "Logging": { - "IncludeScopes": false, - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - }, - "Console": { - "IncludeScopes": true, - "LogLevel": { - "Default": "Warning", - "System": "Warning", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } + "Serilog": { + "MinimumLevel": { + "Default": "Error", + "AspNetCoreRateLimit.IpRateLimitMiddleware": "Information", + "IdentityServer4.Validation.TokenValidator": "Fatal", + "IdentityServer4.Validation.TokenRequestValidator": "Fatal" } } } diff --git a/src/Identity/appsettings.json b/src/Identity/appsettings.json index fb3469b5fe11..6b1dcbb1b96f 100644 --- a/src/Identity/appsettings.json +++ b/src/Identity/appsettings.json @@ -90,5 +90,11 @@ }, "IpRateLimitPolicies": { "IpRules": [] + }, + "Serilog": { + "Enrich": [ "FromLogContext" ], + "Properties": { + "Project": "Identity" + } } } From 67328577bffe6337d699794be0b2074b31135109 Mon Sep 17 00:00:00 2001 From: Hinton Date: Thu, 13 Jan 2022 19:40:57 +0100 Subject: [PATCH 8/8] Remove AspNetCoreRateLimit.IpRateLimitMiddleware, we extend it so it's never logged currently --- src/Identity/appsettings.Production.json | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Identity/appsettings.Production.json b/src/Identity/appsettings.Production.json index d5c951d5e9ed..6d7588f1e7c7 100644 --- a/src/Identity/appsettings.Production.json +++ b/src/Identity/appsettings.Production.json @@ -21,7 +21,6 @@ "Serilog": { "MinimumLevel": { "Default": "Error", - "AspNetCoreRateLimit.IpRateLimitMiddleware": "Information", "IdentityServer4.Validation.TokenValidator": "Fatal", "IdentityServer4.Validation.TokenRequestValidator": "Fatal" }