From fc9b3e1d920714281f623261c5265bf82d684117 Mon Sep 17 00:00:00 2001 From: gicode Date: Thu, 10 Nov 2011 18:02:38 +0000 Subject: [PATCH] Follow-up r102587. Add details to comments and add a couple more tests. --- includes/GlobalFunctions.php | 13 +++++++------ .../GlobalFunctions/wfRemoveDotSegmentsTest.php | 3 +++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 339cd090c7..0913fa2391 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -503,22 +503,23 @@ function wfRemoveDotSegments( $urlPath ) { while ( $urlPath ) { $matches = null; if ( preg_match('%^\.\.?/%', $urlPath, $matches) ) { - # Step A + # Step A, remove leading "../" or "./" $urlPath = substr( $urlPath, strlen( $matches[0] ) ); } elseif ( preg_match( '%^/\.(/|$)%', $urlPath, $matches ) ) { - # Step B + # Step B, replace leading "/.$" or "/./" with "/" $start = strlen( $matches[0] ); $urlPath = '/' . substr( $urlPath, $start ); } elseif ( preg_match( '%^/\.\.(/|$)%', $urlPath, $matches ) ) { - # Step C + # Step C, replace leading "/..$" or "/../" with "/" and + # remove last path component in output $start = strlen( $matches[0] ); $urlPath = '/' . substr( $urlPath, $start ); $output = preg_replace('%(^|/)[^/]*$%', '', $output); } elseif ( preg_match( '%^\.\.?$%', $urlPath, $matches ) ) { - # Step D - $urlPath = substr( $urlPath, strlen( $matches[0] ) ); + # Step D, remove "^..$" or "^.$" + $urlPath = ''; } else { - # Step E + # Step E, move leading path segment to output preg_match( '%^/?[^/]*%', $urlPath, $matches ); $urlPath = substr( $urlPath, strlen( $matches[0] ) ); $output .= $matches[0]; diff --git a/tests/phpunit/includes/GlobalFunctions/wfRemoveDotSegmentsTest.php b/tests/phpunit/includes/GlobalFunctions/wfRemoveDotSegmentsTest.php index c60600535a..1cf0e0fb2c 100644 --- a/tests/phpunit/includes/GlobalFunctions/wfRemoveDotSegmentsTest.php +++ b/tests/phpunit/includes/GlobalFunctions/wfRemoveDotSegmentsTest.php @@ -23,11 +23,14 @@ class wfRemoveDotSegments extends MediaWikiTestCase { array( '/a/b/c/./../../g', '/a/g' ), array( 'mid/content=5/../6', 'mid/6' ), array( '/a//../b', '/a/b' ), + array( '/.../a', '/.../a' ), + array( '.../a', '.../a' ), array( '', '' ), array( '/', '/' ), array( '//', '//' ), array( '.', '' ), array( '..', '' ), + array( '...', '...' ), array( '/.', '/' ), array( '/..', '/' ), array( './', '' ), -- 2.20.1