Skip to content

Commit 8a61760

Browse files
committed
Strip backslashes, not just forward slashes, from untrailingslashit().
trailingslashit() will now remove any forward or backslashes from the end of a string before appending a forward slash. props knutsp, willmot. fixes #22267. git-svn-id: https://develop.svn.wordpress.org/trunk@27344 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2b03ef7 commit 8a61760

3 files changed

Lines changed: 35 additions & 13 deletions

File tree

src/wp-includes/formatting.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,35 +1482,34 @@ function backslashit($string) {
14821482
/**
14831483
* Appends a trailing slash.
14841484
*
1485-
* Will remove trailing slash if it exists already before adding a trailing
1486-
* slash. This prevents double slashing a string or path.
1485+
* Will remove trailing forward and backslashes if it exists already before adding
1486+
* a trailing forward slash. This prevents double slashing a string or path.
14871487
*
14881488
* The primary use of this is for paths and thus should be used for paths. It is
14891489
* not restricted to paths and offers no specific path support.
14901490
*
14911491
* @since 1.2.0
1492-
* @uses untrailingslashit() Unslashes string if it was slashed already.
14931492
*
14941493
* @param string $string What to add the trailing slash to.
14951494
* @return string String with trailing slash added.
14961495
*/
1497-
function trailingslashit($string) {
1498-
return untrailingslashit($string) . '/';
1496+
function trailingslashit( $string ) {
1497+
return untrailingslashit( $string ) . '/';
14991498
}
15001499

15011500
/**
1502-
* Removes trailing slash if it exists.
1501+
* Removes trailing forward slashes and backslashes if they exist.
15031502
*
15041503
* The primary use of this is for paths and thus should be used for paths. It is
15051504
* not restricted to paths and offers no specific path support.
15061505
*
15071506
* @since 2.2.0
15081507
*
1509-
* @param string $string What to remove the trailing slash from.
1510-
* @return string String without the trailing slash.
1508+
* @param string $string What to remove the trailing slashes from.
1509+
* @return string String without the trailing slashes.
15111510
*/
1512-
function untrailingslashit($string) {
1513-
return rtrim($string, '/');
1511+
function untrailingslashit( $string ) {
1512+
return rtrim( $string, '/\\' );
15141513
}
15151514

15161515
/**

src/wp-includes/functions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,17 +1455,17 @@ function get_temp_dir() {
14551455
return trailingslashit(WP_TEMP_DIR);
14561456

14571457
if ( $temp )
1458-
return trailingslashit( rtrim( $temp, '\\' ) );
1458+
return trailingslashit( $temp );
14591459

14601460
if ( function_exists('sys_get_temp_dir') ) {
14611461
$temp = sys_get_temp_dir();
14621462
if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
1463-
return trailingslashit( rtrim( $temp, '\\' ) );
1463+
return trailingslashit( $temp );
14641464
}
14651465

14661466
$temp = ini_get('upload_tmp_dir');
14671467
if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
1468-
return trailingslashit( rtrim( $temp, '\\' ) );
1468+
return trailingslashit( $temp );
14691469

14701470
$temp = WP_CONTENT_DIR . '/';
14711471
if ( is_dir( $temp ) && wp_is_writable( $temp ) )

tests/phpunit/tests/formatting/Slashit.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,34 @@ function test_removes_trailing_slashes() {
2121
$this->assertEquals("a", untrailingslashit("a////"));
2222
}
2323

24+
/**
25+
* @ticket 22267
26+
*/
27+
function test_removes_trailing_backslashes() {
28+
$this->assertEquals("a", untrailingslashit("a\\"));
29+
$this->assertEquals("a", untrailingslashit("a\\\\\\\\"));
30+
}
31+
32+
/**
33+
* @ticket 22267
34+
*/
35+
function test_removes_trailing_mixed_slashes() {
36+
$this->assertEquals("a", untrailingslashit("a/\\"));
37+
$this->assertEquals("a", untrailingslashit("a\\/\\///\\\\//"));
38+
}
39+
2440
function test_adds_trailing_slash() {
2541
$this->assertEquals("a/", trailingslashit("a"));
2642
}
2743

2844
function test_does_not_add_trailing_slash_if_one_exists() {
2945
$this->assertEquals("a/", trailingslashit("a/"));
3046
}
47+
48+
/**
49+
* @ticket 22267
50+
*/
51+
function test_converts_trailing_backslash_to_slash_if_one_exists() {
52+
$this->assertEquals("a/", trailingslashit("a\\"));
53+
}
3154
}

0 commit comments

Comments
 (0)