Add serverside validation of assigned hostnames

This commit is contained in:
Kenn Jacobsen
2019-02-24 09:37:22 +01:00
parent 49ca4bbc91
commit b94b94ea06
5 changed files with 34 additions and 7 deletions

View File

@@ -16,6 +16,13 @@
<umb-pane>
<div ng-show="vm.error">
<div class="alert alert-error">
<div><strong>{{vm.error.errorMsg}}</strong></div>
<div>{{vm.error.data.Message}}</div>
</div>
</div>
<h5 class="umb-pane-title"><localize key="assignDomain_setDomains">Domains</localize></h5>
<small class="db" style="margin-bottom: 10px;">
<localize key="assignDomain_domainHelp"></localize>

View File

@@ -131,6 +131,7 @@
}
}, function (e) {
vm.error = e;
vm.submitButtonState = "error";
});
}

View File

@@ -37,6 +37,7 @@ using Umbraco.Web.Editors.Filters;
using Umbraco.Core.Models.Entities;
using Umbraco.Core.Persistence;
using Umbraco.Core.Security;
using Umbraco.Web.Routing;
namespace Umbraco.Web.Editors
{
@@ -1588,6 +1589,19 @@ namespace Umbraco.Web.Editors
[HttpPost]
public DomainSave PostSaveLanguageAndDomains(DomainSave model)
{
foreach(var domain in model.Domains)
{
try
{
var uri = DomainHelper.ParseUriFromDomainName(domain.Name, Request.RequestUri);
}
catch (UriFormatException)
{
var response = Request.CreateValidationErrorResponse("One or more domains are not valid");
throw new HttpResponseException(response);
}
}
var node = Services.ContentService.GetById(model.NodeId);
if (node == null)

View File

@@ -22,16 +22,11 @@ namespace Umbraco.Web.Routing
{
try
{
// turn "/en" into "http://whatever.com/en" so it becomes a parseable uri
var name = Name.StartsWith("/") && currentUri != null
? currentUri.GetLeftPart(UriPartial.Authority) + Name
: Name;
var scheme = currentUri?.Scheme ?? Uri.UriSchemeHttp;
Uri = new Uri(UriUtility.TrimPathEndSlash(UriUtility.StartWithScheme(name, scheme)));
Uri = DomainHelper.ParseUriFromDomainName(Name, currentUri);
}
catch (UriFormatException)
{
throw new ArgumentException($"Failed to parse invalid domain: node id={domain.ContentId}, hostname=\"{Name.ToCSharpString()}\"."
throw new ArgumentException($"Failed to parse invalid domain: node id={domain.ContentId}, hostname=\"{domain.Name.ToCSharpString()}\"."
+ " Hostname should be a valid uri.", nameof(domain));
}
}

View File

@@ -251,6 +251,16 @@ namespace Umbraco.Web.Routing
.OrderByDescending(d => d.Uri.ToString());
}
internal static Uri ParseUriFromDomainName(string Name, Uri currentUri)
{
// turn "/en" into "http://whatever.com/en" so it becomes a parseable uri
var name = Name.StartsWith("/") && currentUri != null
? currentUri.GetLeftPart(UriPartial.Authority) + Name
: Name;
var scheme = currentUri?.Scheme ?? Uri.UriSchemeHttp;
return new Uri(UriUtility.TrimPathEndSlash(UriUtility.StartWithScheme(name, scheme)));
}
#endregion
#region Utilities