Add serverside validation of assigned hostnames
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -131,6 +131,7 @@
|
||||
}
|
||||
|
||||
}, function (e) {
|
||||
vm.error = e;
|
||||
vm.submitButtonState = "error";
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user