use alternative uri validation

This commit is contained in:
Andrew Camilleri 2018-05-14 09:32:04 +02:00
parent 0ba1072d54
commit bcd79c5882
5 changed files with 48 additions and 5 deletions

View file

@ -36,6 +36,7 @@ using BTCPayServer.Services.Stores;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using BTCPayServer.Rating; using BTCPayServer.Rating;
using BTCPayServer.Validation;
using ExchangeSharp; using ExchangeSharp;
namespace BTCPayServer.Tests namespace BTCPayServer.Tests
@ -48,6 +49,25 @@ namespace BTCPayServer.Tests
Logs.LogProvider = new XUnitLogProvider(helper); 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] [Fact]
public void CanCalculateCryptoDue2() public void CanCalculateCryptoDue2()
{ {

View file

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Validation;
namespace BTCPayServer.Models.InvoicingModels namespace BTCPayServer.Models.InvoicingModels
{ {
@ -52,8 +53,7 @@ namespace BTCPayServer.Models.InvoicingModels
get; set; get; set;
} }
[Uri]
[Url]
public string NotificationUrl public string NotificationUrl
{ {
get; set; get; set;

View file

@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Services; using BTCPayServer.Services;
using BTCPayServer.Validation;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;
namespace BTCPayServer.Models.StoreViewModels namespace BTCPayServer.Models.StoreViewModels
@ -42,10 +43,10 @@ namespace BTCPayServer.Models.StoreViewModels
public string OnChainMinValue { get; set; } public string OnChainMinValue { get; set; }
[Display(Name = "Link to a custom CSS stylesheet")] [Display(Name = "Link to a custom CSS stylesheet")]
[Url] [Uri]
public string CustomCSS { get; set; } public string CustomCSS { get; set; }
[Display(Name = "Link to a custom logo")] [Display(Name = "Link to a custom logo")]
[Url] [Uri]
public string CustomLogo { get; set; } public string CustomLogo { get; set; }
[Display(Name = "Custom HTML title to display on Checkout page")] [Display(Name = "Custom HTML title to display on Checkout page")]

View file

@ -1,6 +1,7 @@
using BTCPayServer.Services; using BTCPayServer.Services;
using BTCPayServer.Services.Invoices; using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Rates; using BTCPayServer.Services.Rates;
using BTCPayServer.Validation;
using BTCPayServer.Validations; using BTCPayServer.Validations;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;
using System; using System;
@ -34,7 +35,7 @@ namespace BTCPayServer.Models.StoreViewModels
get; set; get; set;
} }
[Url] [Uri]
[Display(Name = "Store Website")] [Display(Name = "Store Website")]
[MaxLength(500)] [MaxLength(500)]
public string StoreWebsite public string StoreWebsite

View file

@ -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;
}
}
}