UrlUtils.cs: Fixed some bugs in Canonize method. Add several tests
authorNoam Lampert <noaml@mono-cvs.ximian.com>
Mon, 16 Jun 2008 11:52:01 +0000 (11:52 -0000)
committerNoam Lampert <noaml@mono-cvs.ximian.com>
Mon, 16 Jun 2008 11:52:01 +0000 (11:52 -0000)
svn path=/trunk/mcs/; revision=105894

mcs/class/System.Web/System.Web.Util/ChangeLog
mcs/class/System.Web/System.Web.Util/UrlUtils.cs
mcs/class/System.Web/System.Web_test.dll.sources
mcs/class/System.Web/Test/System.Web.Util/UrlUtilsTest.cs [new file with mode: 0644]

index 2d200efbe0379feb3e4ab2d4a3050f51ec5d4caa..76105d3b716df06b81a931dac2083034005cd6eb 100644 (file)
@@ -1,3 +1,7 @@
+2008-06-16  Noam Lampert  <noaml@mainsoft.com>
+
+       * UrlUtils.cs: Fixed some bugs in Canonize method. Add several tests
+
 2008-06-04  Marek Habersack  <mhabersack@novell.com>
 
        * UrlUtils.cs: added internal method HasSessionId
index e2ad551ea0b10be871d38b33ad97c2851b554504..4f8aa5c2792eb146298da9628fc39025c29153ff 100644 (file)
@@ -80,7 +80,7 @@ namespace System.Web.Util {
 
                        return (StrUtils.StartsWith (path, "/(") && path.IndexOf ("/)") > 2);
                }
-               
+
                internal static string RemoveSessionId (string base_path, string file_path)
                {
                        // Caller did a GetSessionId first
@@ -151,6 +151,8 @@ namespace System.Web.Util {
                
                internal static string Canonic (string path)
                {
+                       bool isRooted = IsRooted(path);
+                       bool endsWithSlash = path.EndsWith("/");
                        string [] parts = path.Split (path_sep);
                        int end = parts.Length;
                        
@@ -158,23 +160,27 @@ namespace System.Web.Util {
                        
                        for (int i = 0; i < end; i++) {
                                string current = parts [i];
+
+                               if (current == "")
+                                       continue;
+
                                if (current == "." )
                                        continue;
 
                                if (current == "..") {
-                                       if (dest == 0) {
-                                               if (i == 1) // see bug 52599
-                                                       continue;
-
-                                               throw new HttpException ("Invalid path.");
-                                       }
-
                                        dest --;
                                        continue;
                                }
+                               if (dest < 0)
+                                       if (!isRooted)
+                                               throw new HttpException ("Invalid path.");
+                                       else
+                                               dest = 0;
 
                                parts [dest++] = current;
                        }
+                       if (dest < 0)
+                               throw new HttpException ("Invalid path.");
 
                        if (dest == 0)
                                return "/";
@@ -183,6 +189,11 @@ namespace System.Web.Util {
 #if NET_2_0
                        str = RemoveDoubleSlashes (str);
 #endif
+                       if (isRooted)
+                               str = "/" + str;
+                       if (endsWithSlash)
+                               str = str + "/";
+
                        return str;
                }
                
index 8b90050cb546a36aa73359527052048f584fad73..d53130a32423676553839f3f77e66ba148bdac5d 100644 (file)
@@ -508,4 +508,5 @@ System.Web.UI.WebControls/WebControlCas.cs
 System.Web.UI.WebControls/XmlCas.cs
 System.Web.UI.WebControls/XmlDataSourceCas.cs
 System.Web.Util/TransactionsCas.cs
+System.Web.Util/UrlUtilsTest.cs
 System.Web.Util/WorkItemCas.cs
diff --git a/mcs/class/System.Web/Test/System.Web.Util/UrlUtilsTest.cs b/mcs/class/System.Web/Test/System.Web.Util/UrlUtilsTest.cs
new file mode 100644 (file)
index 0000000..8d37df3
--- /dev/null
@@ -0,0 +1,69 @@
+//
+// System.Web.Util.UrlUtilsTest.cs - Unit tests for System.Web.Util.UrlUtils
+//
+// Author:
+//     Noam Lampert <noaml@mainsoft.com>
+//
+// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Text;
+using System.Web;
+using System.Web.Util;
+using System.Collections.Specialized;
+using NUnit.Framework;
+using System.Diagnostics;
+\r
+namespace MonoTests.System.Web.Util\r
+{\r
+       [TestFixture]
+       public class UrlUtilsTest\r
+       {\r
+               [Test]
+               public void CanonicTest()
+               {
+                       Assert.AreEqual("/Sample.aspx",UrlUtils.Canonic("/WebApplication1//../Sample.aspx"));
+               }
+               [Test]
+               public void CanonicTest2()
+               {
+                       Assert.AreEqual("Sample.aspx",UrlUtils.Canonic("Path1/../Sample.aspx"));
+               }
+               [Test]
+               public void CanonicTest3()
+               {
+                       Assert.AreEqual("/Path1/Sample.aspx",UrlUtils.Canonic("/../Path1/Sample.aspx"));
+               }
+               [Test]
+               public void CanonicTest4()
+               {
+                       Assert.AreEqual("/Sample.aspx",UrlUtils.Canonic("/../Path1/../../Sample.aspx"));
+               }
+               [Test]
+               [ExpectedException(typeof(HttpException))]
+               public void CanonicTest5()
+               {
+                       UrlUtils.Canonic("../Path1/../../Sample.aspx");
+               }
+       }\r
+}\r