From dd94d54e1df564c4ace93d3085444eace5df3b55 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Tue, 1 Sep 2015 18:30:04 -0700 Subject: [PATCH] [Fixes #2929] Moving IsLocalUrl implementation to UrlHelper --- .../Internal/UrlUtility.cs | 39 ---------- src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs | 12 ++- .../UrlHelperTest.cs | 76 ------------------- 3 files changed, 9 insertions(+), 118 deletions(-) delete mode 100644 src/Microsoft.AspNet.Mvc.Core/Internal/UrlUtility.cs diff --git a/src/Microsoft.AspNet.Mvc.Core/Internal/UrlUtility.cs b/src/Microsoft.AspNet.Mvc.Core/Internal/UrlUtility.cs deleted file mode 100644 index e566907cd9..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/Internal/UrlUtility.cs +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -namespace Microsoft.AspNet.Mvc.Internal -{ - public static class UrlUtility - { - /// - /// Returns a value that indicates whether the URL is local. An URL with an absolute path is considered local - /// if it does not have a host/authority part. URLs using the virtual paths ('~/') are also local. - /// - /// The URL. - /// true if the URL is local; otherwise, false. - /// - /// - /// For example, the following URLs are considered local: - /// /Views/Default/Index.html - /// ~/Index.html - /// - /// - /// The following URLs are non-local: - /// ../Index.html - /// http://www.contoso.com/ - /// http://localhost/Index.html - /// - /// - public static bool IsLocalUrl(string url) - { - return - !string.IsNullOrEmpty(url) && - - // Allows "/" or "/foo" but not "//" or "/\". - ((url[0] == '/' && (url.Length == 1 || (url[1] != '/' && url[1] != '\\'))) || - - // Allows "~/" or "~/foo". - (url.Length > 1 && url[0] == '~' && url[1] == '/')); - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs index b7821eb28f..cf6d23868e 100644 --- a/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Diagnostics; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc.Actions; -using Microsoft.AspNet.Mvc.Internal; using Microsoft.AspNet.Routing; using Microsoft.Framework.Internal; @@ -87,9 +86,16 @@ namespace Microsoft.AspNet.Mvc } /// - public bool IsLocalUrl(string url) + public virtual bool IsLocalUrl(string url) { - return UrlUtility.IsLocalUrl(url); + return + !string.IsNullOrEmpty(url) && + + // Allows "/" or "/foo" but not "//" or "/\". + ((url[0] == '/' && (url.Length == 1 || (url[1] != '/' && url[1] != '\\'))) || + + // Allows "~/" or "~/foo". + (url.Length > 1 && url[0] == '~' && url[1] == '/')); } /// diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs index 42022378bf..ef59d41eff 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs @@ -8,7 +8,6 @@ using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Mvc.Actions; -using Microsoft.AspNet.Mvc.Internal; using Microsoft.AspNet.Routing; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.Logging; @@ -66,9 +65,6 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(expectedPath, path); } - // UrlHelper.IsLocalUrl depends on the UrlUtility.IsLocalUrl method. - // To avoid duplicate tests, all the tests exercising IsLocalUrl verify - // both of UrlHelper.IsLocalUrl and UrlUtility.IsLocalUrl [Theory] [InlineData(null)] [InlineData("")] @@ -83,12 +79,6 @@ namespace Microsoft.AspNet.Mvc // Assert Assert.False(result); - - // Arrange & Act - result = UrlUtility.IsLocalUrl(url); - - // Assert - Assert.False(result); } [Theory] @@ -105,12 +95,6 @@ namespace Microsoft.AspNet.Mvc // Assert Assert.True(result); - - // Arrange & Act - result = UrlUtility.IsLocalUrl(url); - - // Assert - Assert.True(result); } [Theory] @@ -126,12 +110,6 @@ namespace Microsoft.AspNet.Mvc // Assert Assert.True(result); - - // Arrange & Act - result = UrlUtility.IsLocalUrl(url); - - // Assert - Assert.True(result); } [Theory] @@ -148,12 +126,6 @@ namespace Microsoft.AspNet.Mvc // Assert Assert.False(result); - - // Arrange & Act - result = UrlUtility.IsLocalUrl(url); - - // Assert - Assert.False(result); } [Theory] @@ -171,12 +143,6 @@ namespace Microsoft.AspNet.Mvc // Assert Assert.False(result); - - // Arrange & Act - result = UrlUtility.IsLocalUrl(url); - - // Assert - Assert.False(result); } [Theory] @@ -192,12 +158,6 @@ namespace Microsoft.AspNet.Mvc // Assert Assert.False(result); - - // Arrange & Act - result = UrlUtility.IsLocalUrl(url); - - // Assert - Assert.False(result); } [Theory] @@ -213,12 +173,6 @@ namespace Microsoft.AspNet.Mvc // Assert Assert.False(result); - - // Arrange & Act - result = UrlUtility.IsLocalUrl(url); - - // Assert - Assert.False(result); } [Theory] @@ -233,12 +187,6 @@ namespace Microsoft.AspNet.Mvc // Assert Assert.False(result); - - // Arrange & Act - result = UrlUtility.IsLocalUrl(url); - - // Assert - Assert.False(result); } [Theory] @@ -256,12 +204,6 @@ namespace Microsoft.AspNet.Mvc // Assert Assert.False(result); - - // Arrange & Act - result = UrlUtility.IsLocalUrl(url); - - // Assert - Assert.False(result); } [Theory] @@ -280,12 +222,6 @@ namespace Microsoft.AspNet.Mvc // Assert Assert.False(result); - - // Arrange & Act - result = UrlUtility.IsLocalUrl(url); - - // Assert - Assert.False(result); } [Theory] @@ -305,12 +241,6 @@ namespace Microsoft.AspNet.Mvc // Assert Assert.False(result); - - // Arrange & Act - result = UrlUtility.IsLocalUrl(url); - - // Assert - Assert.False(result); } [Theory] @@ -328,12 +258,6 @@ namespace Microsoft.AspNet.Mvc // Assert Assert.False(result); - - // Arrange & Act - result = UrlUtility.IsLocalUrl(url); - - // Assert - Assert.False(result); } [Fact]