Skip to content

Commit

Permalink
Fix/uppercase url (#848)
Browse files Browse the repository at this point in the history
* Update Site.cs (#693)
Fix #311
* Add a test and refactor the url validator method.

Co-authored-by: Thiago Yuri <thiago.souza@ccc.ufcg.edu.br>
  • Loading branch information
tusmester and thiagoyeds committed Jan 6, 2020
1 parent 784b70e commit a101907
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Services/Site.cs
Expand Up @@ -226,15 +226,15 @@ private void ValidateUrlList()
foreach (var url in UrlList.Keys.Where(url => PortalContext.Sites.Keys.Count(k => k == url && PortalContext.Sites[k].Id != this.Id) > 0))
throw new ApplicationException(SNSR.GetString(SNSR.Exceptions.Site.UrlAlreadyUsed_2, url, PortalContext.Sites[url].DisplayName));
}
private bool IsValidSiteUrl(string url)
internal static bool IsValidSiteUrl(string url)
{
var absUrl = "http://" + url;
if (!Uri.IsWellFormedUriString(absUrl, UriKind.Absolute))
return false;
try
{
var uri = new Uri(absUrl);
if (uri.Authority != url)
if (string.Compare(uri.Authority, url, StringComparison.InvariantCultureIgnoreCase) != 0)
return false;
}
catch
Expand Down
Expand Up @@ -158,6 +158,7 @@
<Compile Include="HttpHelper.cs" />
<Compile Include="OAuthAuthenticationTests.cs" />
<Compile Include="ServicesTestUser.cs" />
<Compile Include="SiteTests.cs" />
<Compile Include="TokenAuthenticationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WopiTests.cs" />
Expand Down
32 changes: 32 additions & 0 deletions src/Tests/SenseNet.Services.Tests/SiteTests.cs
@@ -0,0 +1,32 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SenseNet.Portal;
using SenseNet.Tests;

namespace SenseNet.Services.Tests
{
[TestClass]
public class SiteTests : TestBase
{
[TestMethod]
public void Site_UppercaseUrl()
{
Assert.IsTrue(Site.IsValidSiteUrl("example.com"));
Assert.IsTrue(Site.IsValidSiteUrl("example.com:1234")); // a port is allowed
Assert.IsTrue(Site.IsValidSiteUrl("exAmPlE.cOm")); // uppercase url
Assert.IsTrue(Site.IsValidSiteUrl("www.example.com"));
Assert.IsTrue(Site.IsValidSiteUrl("sub1.sub2.example.com"));

Assert.IsFalse(Site.IsValidSiteUrl(""));
Assert.IsFalse(Site.IsValidSiteUrl("."));
Assert.IsFalse(Site.IsValidSiteUrl(".com"));
Assert.IsFalse(Site.IsValidSiteUrl("http://example.com")); // a schema prefix is not allowed
Assert.IsFalse(Site.IsValidSiteUrl("http:/example.com"));
Assert.IsFalse(Site.IsValidSiteUrl("/example.com"));
Assert.IsFalse(Site.IsValidSiteUrl("//example.com"));
Assert.IsFalse(Site.IsValidSiteUrl(":/example.com"));
Assert.IsFalse(Site.IsValidSiteUrl("example.com//"));
Assert.IsFalse(Site.IsValidSiteUrl("example.com/file.txt")); // only a domain is allowed
Assert.IsFalse(Site.IsValidSiteUrl("example.com/page1/page2")); // only a domain is allowed
}
}
}

0 comments on commit a101907

Please sign in to comment.