Преглед на файлове

Fix for "/.." in URIs

Go back only one level in URI parsing.
See #816 (Comments from May 2020)
bel2125 преди 5 години
родител
ревизия
00dceeae70
променени са 2 файла, в които са добавени 55 реда и са изтрити 47 реда
  1. 2 2
      src/civetweb.c
  2. 53 45
      unittest/private.c

+ 2 - 2
src/civetweb.c

@@ -8230,8 +8230,8 @@ remove_dot_segments(char *inout)
 				/* remove last segment */
 				do {
 					out_end--;
-					*out_end = 0;
 				} while ((out_begin != out_end) && (*out_end != '/'));
+				*out_end = 0;
 			}
 		} else if (!strcmp(in, "/..")) {
 			in[1] = 0;
@@ -8239,8 +8239,8 @@ remove_dot_segments(char *inout)
 				/* remove last segment */
 				do {
 					out_end--;
-					*out_end = 0;
 				} while ((out_begin != out_end) && (*out_end != '/'));
+				*out_end = 0;
 			}
 		}
 		/* otherwise */

+ 53 - 45
unittest/private.c

@@ -331,51 +331,59 @@ START_TEST(test_remove_dot_segments)
 	struct {
 		const char *input;
 		const char *expected_output;
-	} tests[] = {{"/path/to/file.ext", "/path/to/file.ext"},
-	             {"/file.ext", "/file.ext"},
-	             {"/path/../file.ext", "/file.ext"},
-	             {"/../to/file.ext", "/to/file.ext"},
-	             {"/../../file.ext", "/file.ext"},
-	             {"/./../file.ext", "/file.ext"},
-	             {"/.././file.ext", "/file.ext"},
-	             {"/././file.ext", "/file.ext"},
-	             {"/././file.ext", "/file.ext"},
-	             {"/path/.to/..file.ext", "/path/.to/..file.ext"},
-	             {"/file", "/file"},
-	             {"/path/", "/path/"},
-
-	             {"file.ext", "file.ext"},
-	             {"./file.ext", "file.ext"},
-	             {"../file.ext", "file.ext"},
-	             {".file.ext", ".file.ext"},
-	             {"..file.ext", "..file.ext"},
-	             {"file", "file"},
-	             {"/x/../", "/"},
-	             {"/x/../../", "/"},
-	             {"/x/.././", "/"},
-	             {"/./x/.././", "/"},
-
-	             /* Windows specific */
-	             {"\\file.ext", "/file.ext"},
-	             {"\\..\\file.ext", "/file.ext"},
-	             {"/file.", "/file"},
-	             {"/path\\to.\\.\\file.", "/path/to/file"},
-
-	             /* Multiple dots and slashes */
-	             {"\\//\\\\x", "/x"},
-	             {"//", "/"},
-	             {"/./", "/"},
-	             {"/../", "/"},
-	             {"/.../", "/"},
-	             {"/..../", "/"},
-	             {"/...../", "/"},
-	             {"/...../", "/"},
-	             {"/...//", "/"},
-	             {"/..././", "/"},
-	             {"/.../../", "/"},
-	             {"/.../.../", "/"},
-
-	             {NULL, NULL}};
+	} tests[] = {
+	    {"/path/to/file.ext", "/path/to/file.ext"},
+	    {"/file.ext", "/file.ext"},
+	    {"/path/../file.ext", "/file.ext"},
+	    {"/../to/file.ext", "/to/file.ext"},
+	    {"/../../file.ext", "/file.ext"},
+	    {"/./../file.ext", "/file.ext"},
+	    {"/.././file.ext", "/file.ext"},
+	    {"/././file.ext", "/file.ext"},
+	    {"/././file.ext", "/file.ext"},
+	    {"/path/.to/..file.ext", "/path/.to/..file.ext"},
+	    {"/file", "/file"},
+	    {"/path/", "/path/"},
+
+	    {"file.ext", "file.ext"},
+	    {"./file.ext", "file.ext"},
+	    {"../file.ext", "file.ext"},
+	    {".file.ext", ".file.ext"},
+	    {"..file.ext", "..file.ext"},
+	    {"file", "file"},
+	    {"/x/../", "/"},
+	    {"/x/../../", "/"},
+	    {"/x/.././", "/"},
+	    {"/./x/.././", "/"},
+
+	    /* Windows specific */
+	    {"\\file.ext", "/file.ext"},
+	    {"\\..\\file.ext", "/file.ext"},
+	    {"/file.", "/file"},
+	    {"/path\\to.\\.\\file.", "/path/to/file"},
+
+	    /* Multiple dots and slashes */
+	    {"\\//\\\\x", "/x"},
+	    {"//", "/"},
+	    {"/./", "/"},
+	    {"/../", "/"},
+	    {"/.../", "/"},
+	    {"/..../", "/"},
+	    {"/...../", "/"},
+	    {"/...../", "/"},
+	    {"/...//", "/"},
+	    {"/..././", "/"},
+	    {"/.../../", "/"},
+	    {"/.../.../", "/"},
+
+	    /* Test cases from issues */
+	    {"/foo/bar/baz/../qux.txt", "/foo/bar/qux.txt"},
+	    {"/../alice/bob/../carol/david/frank/../../grace.ext/hugo/../irene.jpg",
+	     "/alice/carol/grace.ext/irene.jpg"},
+	    {"/a/b/..", "/a/"},
+	    {"/a/b/c/../d/../..", "/a/"},
+
+	    {NULL, NULL}};
 
 	mark_point();