Skip to content

Commit

Permalink
Fixes #304, UrlPath.IsAbsoluteUrl method was misidentifying the URL a…
Browse files Browse the repository at this point in the history
…s absolute when an absolute URL was added as a query string parameter, as in the case with ASP.NET error pages.
  • Loading branch information
NightOwl888 committed Mar 28, 2014
1 parent 52342e5 commit 4e5c013
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ public void IsAbsoluteUrl_WithAbsoluteUrl_ShouldReturnTrue()
Assert.AreEqual(expected, actual);
}

[Test]
public void IsAbsoluteUrl_WithAbsoluteUrlAsQueryParameter_ShouldReturnFalse()
{
// arrange
var target = this.NewUrlPath();

// act
var result = target.IsAbsoluteUrl(@"/error/pagenotfound?404;http://somewhere.com/directory/subdirectory/page.aspx?a=b");

// assert
var actual = result;
var expected = false;
Assert.AreEqual(expected, actual);
}

/// <summary>
/// Return false when the virtual application name (and host name) match.
/// </summary>
Expand Down
11 changes: 9 additions & 2 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/Web/UrlPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using MvcSiteMapProvider.Web.Mvc;

Expand Down Expand Up @@ -363,8 +364,14 @@ public string UrlDecode(string url)
/// <returns><b>true</b> if the URL is absolute; otherwise <b>false</b>.</returns>
public bool IsAbsoluteUrl(string url)
{
// There must be at least 1 character before the scheme delimiter.
return (url.IndexOf(Uri.SchemeDelimiter) > 0);
// Optimization: Return false early if there is no scheme delimiter in the string
// prefixed by at least 1 character.
if (!(url.IndexOf(Uri.SchemeDelimiter) > 0))
return false;

// There must be at least 1 word character before the scheme delimiter.
// This ensures we don't return true for query strings that contain absolute URLs.
return Regex.IsMatch(url, @"^\w+://", RegexOptions.Compiled);
}

/// <summary>
Expand Down

0 comments on commit 4e5c013

Please sign in to comment.