diff --git a/BTCPayServer.Tests/UnitTest1.cs b/BTCPayServer.Tests/UnitTest1.cs index 7ae09e3fd..02b420508 100644 --- a/BTCPayServer.Tests/UnitTest1.cs +++ b/BTCPayServer.Tests/UnitTest1.cs @@ -36,6 +36,7 @@ using BTCPayServer.Services.Stores; using System.Net.Http; using System.Text; using BTCPayServer.Rating; +using BTCPayServer.Validation; using ExchangeSharp; namespace BTCPayServer.Tests @@ -48,6 +49,25 @@ namespace BTCPayServer.Tests Logs.LogProvider = new XUnitLogProvider(helper); } + [Fact] + public void CanHandleUriValidation() + { + var attribute = new UriAttribute(); + Assert.True(attribute.IsValid("http://localhost")); + Assert.True(attribute.IsValid("http://localhost:1234")); + Assert.True(attribute.IsValid("https://localhost")); + Assert.True(attribute.IsValid("https://127.0.0.1")); + Assert.True(attribute.IsValid("http://127.0.0.1")); + Assert.True(attribute.IsValid("http://127.0.0.1:1234")); + Assert.True(attribute.IsValid("http://gozo.com")); + Assert.True(attribute.IsValid("https://gozo.com")); + Assert.True(attribute.IsValid("https://gozo.com:1234")); + Assert.False(attribute.IsValid("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud e")); + Assert.False(attribute.IsValid(2)); + Assert.False(attribute.IsValid("http://")); + Assert.False(attribute.IsValid("httpdsadsa.com")); + } + [Fact] public void CanCalculateCryptoDue2() { diff --git a/BTCPayServer/Models/InvoicingModels/CreateInvoiceModel.cs b/BTCPayServer/Models/InvoicingModels/CreateInvoiceModel.cs index 9ac99d667..631303ae0 100644 --- a/BTCPayServer/Models/InvoicingModels/CreateInvoiceModel.cs +++ b/BTCPayServer/Models/InvoicingModels/CreateInvoiceModel.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; +using BTCPayServer.Validation; namespace BTCPayServer.Models.InvoicingModels { @@ -52,8 +53,7 @@ namespace BTCPayServer.Models.InvoicingModels get; set; } - - [Url] + [Uri] public string NotificationUrl { get; set; diff --git a/BTCPayServer/Models/StoreViewModels/CheckoutExperienceViewModel.cs b/BTCPayServer/Models/StoreViewModels/CheckoutExperienceViewModel.cs index 025315ad8..ae5176ffd 100644 --- a/BTCPayServer/Models/StoreViewModels/CheckoutExperienceViewModel.cs +++ b/BTCPayServer/Models/StoreViewModels/CheckoutExperienceViewModel.cs @@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; using BTCPayServer.Services; +using BTCPayServer.Validation; using Microsoft.AspNetCore.Mvc.Rendering; namespace BTCPayServer.Models.StoreViewModels @@ -42,10 +43,10 @@ namespace BTCPayServer.Models.StoreViewModels public string OnChainMinValue { get; set; } [Display(Name = "Link to a custom CSS stylesheet")] - [Url] + [Uri] public string CustomCSS { get; set; } [Display(Name = "Link to a custom logo")] - [Url] + [Uri] public string CustomLogo { get; set; } [Display(Name = "Custom HTML title to display on Checkout page")] diff --git a/BTCPayServer/Models/StoreViewModels/StoreViewModel.cs b/BTCPayServer/Models/StoreViewModels/StoreViewModel.cs index c2fadcdd3..1b86a07c2 100644 --- a/BTCPayServer/Models/StoreViewModels/StoreViewModel.cs +++ b/BTCPayServer/Models/StoreViewModels/StoreViewModel.cs @@ -1,6 +1,7 @@ using BTCPayServer.Services; using BTCPayServer.Services.Invoices; using BTCPayServer.Services.Rates; +using BTCPayServer.Validation; using BTCPayServer.Validations; using Microsoft.AspNetCore.Mvc.Rendering; using System; @@ -34,7 +35,7 @@ namespace BTCPayServer.Models.StoreViewModels get; set; } - [Url] + [Uri] [Display(Name = "Store Website")] [MaxLength(500)] public string StoreWebsite diff --git a/BTCPayServer/Validation/UriAttribute.cs b/BTCPayServer/Validation/UriAttribute.cs new file mode 100644 index 000000000..717d10746 --- /dev/null +++ b/BTCPayServer/Validation/UriAttribute.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace BTCPayServer.Validation +{ + //from https://stackoverflow.com/a/47196738/275504 + public class UriAttribute : ValidationAttribute + { + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + Uri uri; + bool valid = Uri.TryCreate(Convert.ToString(value), UriKind.Absolute, out uri); + + if (!valid) + { + return new ValidationResult(ErrorMessage); + } + return ValidationResult.Success; + } + } +}