From f1cef81d76b9d0b73a16c70ec7ae9e39e1f1a419 Mon Sep 17 00:00:00 2001 From: Kukks Date: Thu, 14 Nov 2019 19:01:26 +0100 Subject: [PATCH] Do not allow login or register over an insecure connection --- BTCPayServer/Controllers/AccountController.cs | 56 +++++++++++- BTCPayServer/Views/Account/Login.cshtml | 53 ++++++----- BTCPayServer/Views/Account/Register.cshtml | 90 +++++++++++-------- 3 files changed, 139 insertions(+), 60 deletions(-) diff --git a/BTCPayServer/Controllers/AccountController.cs b/BTCPayServer/Controllers/AccountController.cs index 247491352..89ade5e4b 100644 --- a/BTCPayServer/Controllers/AccountController.cs +++ b/BTCPayServer/Controllers/AccountController.cs @@ -74,21 +74,29 @@ namespace BTCPayServer.Controllers [AllowAnonymous] public async Task Login(string returnUrl = null) { + if (User.Identity.IsAuthenticated && string.IsNullOrEmpty(returnUrl)) return RedirectToLocal(); // Clear the existing external cookie to ensure a clean login process await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); + CanLoginOrRegister(); + ViewData["ReturnUrl"] = returnUrl; return View(); } + [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] [RateLimitsFilter(ZoneLimits.Login, Scope = RateLimitsScope.RemoteAddress)] public async Task Login(LoginViewModel model, string returnUrl = null) { + if (!CanLoginOrRegister()) + { + return View(model); + } ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { @@ -199,6 +207,11 @@ namespace BTCPayServer.Controllers [ValidateAntiForgeryToken] public async Task LoginWithU2F(LoginWithU2FViewModel viewModel, string returnUrl = null) { + if (!CanLoginOrRegister()) + { + return RedirectToAction("Login"); + } + ViewData["ReturnUrl"] = returnUrl; var user = await _userManager.FindByIdAsync(viewModel.UserId); @@ -242,6 +255,11 @@ namespace BTCPayServer.Controllers [AllowAnonymous] public async Task LoginWith2fa(bool rememberMe, string returnUrl = null) { + if (!CanLoginOrRegister()) + { + return RedirectToAction("Login"); + } + // Ensure the user has gone through the username & password screen first var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); @@ -264,6 +282,11 @@ namespace BTCPayServer.Controllers [ValidateAntiForgeryToken] public async Task LoginWith2fa(LoginWith2faViewModel model, bool rememberMe, string returnUrl = null) { + if (!CanLoginOrRegister()) + { + return RedirectToAction("Login"); + } + if (!ModelState.IsValid) { return View(model); @@ -305,6 +328,11 @@ namespace BTCPayServer.Controllers [AllowAnonymous] public async Task LoginWithRecoveryCode(string returnUrl = null) { + if (!CanLoginOrRegister()) + { + return RedirectToAction("Login"); + } + // Ensure the user has gone through the username & password screen first var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) @@ -322,6 +350,11 @@ namespace BTCPayServer.Controllers [ValidateAntiForgeryToken] public async Task LoginWithRecoveryCode(LoginWithRecoveryCodeViewModel model, string returnUrl = null) { + if (!CanLoginOrRegister()) + { + return RedirectToAction("Login"); + } + if (!ModelState.IsValid) { return View(model); @@ -366,7 +399,8 @@ namespace BTCPayServer.Controllers [AllowAnonymous] public async Task Register(string returnUrl = null, bool logon = true, bool useBasicLayout = false) { - var policies = await _SettingsRepository.GetSettingAsync() ?? new PoliciesSettings(); + CanLoginOrRegister(); + var policies = await _SettingsRepository.GetSettingAsync() ?? new PoliciesSettings(); if (policies.LockSubscription && !User.IsInRole(Roles.ServerAdmin)) return RedirectToAction(nameof(HomeController.Index), "Home"); ViewData["ReturnUrl"] = returnUrl; @@ -381,6 +415,11 @@ namespace BTCPayServer.Controllers [ValidateAntiForgeryToken] public async Task Register(RegisterViewModel model, string returnUrl = null, bool logon = true) { + if (!CanLoginOrRegister()) + { + return RedirectToAction("Register"); + } + ViewData["ReturnUrl"] = returnUrl; ViewData["Logon"] = logon.ToString(CultureInfo.InvariantCulture).ToLowerInvariant(); ViewData["AllowIsAdmin"] = _Options.AllowAdminRegistration; @@ -580,6 +619,21 @@ namespace BTCPayServer.Controllers return RedirectToAction(nameof(HomeController.Index), "Home"); } } + + + private bool CanLoginOrRegister() + { + if (_btcPayServerEnvironment.IsDevelopping || _btcPayServerEnvironment.IsSecure) return true; + TempData.SetStatusMessageModel(new StatusMessageModel() + { + Severity = StatusMessageModel.StatusSeverity.Error, + Message = "You cannot login over an insecure connection. Please use HTTPS or Tor." + }); + + ViewData["disabled"] = true; + return false; + + } #endregion } diff --git a/BTCPayServer/Views/Account/Login.cshtml b/BTCPayServer/Views/Account/Login.cshtml index 2272a73e6..2fc0c4d41 100644 --- a/BTCPayServer/Views/Account/Login.cshtml +++ b/BTCPayServer/Views/Account/Login.cshtml @@ -4,7 +4,14 @@ ViewData["Title"] = "Log in"; Layout = "_WelcomeLayout.cshtml"; } - +@if (TempData.HasStatusMessage()) +{ +
+
+ +
+
+}