Skip to content

Commit 0ecd17e

Browse files
committed
Make sanitization strict by default
Signed-off-by: Riddhesh Sanghvi <[email protected]>
1 parent 21aa79c commit 0ecd17e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

php/utils.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,16 +1766,27 @@ function get_value_if_flag_isset( $assoc_args, $flag, $supported_flag_values = [
17661766
* Function to sanitize and remove illegal characters for folder and filename.
17671767
*
17681768
* @param string $input_name Input name to be sanitized.
1769+
* @param bool $strict Do strict replacement, i.e, remove all special characters except `-` and `_`.
17691770
*
17701771
* @return string Sanitized name valid for file/folder creation.
17711772
*/
1772-
function sanitize_file_folder_name( $input_name ) {
1773+
function sanitize_file_folder_name( $input_name, $strict = true ) {
17731774

17741775
// Remove Illegal Chars for folder and filename.
1775-
$output = preg_replace('/[\"\*\/\:\<\>\?\'\|]+/', ' ', $input_name);
1776+
$output = preg_replace( '/[\"\*\/\:\<\>\?\'\|]+/', ' ', $input_name );
17761777

1778+
if ( $strict ) {
1779+
$output = preg_replace( '/[^A-Za-z0-9\-\_]/', '', $output );
1780+
}
17771781
// Replace Spaces with dashes.
1778-
$output = str_replace(' ', '-', $output);
1782+
$output = str_replace( ' ', '-', $output );
1783+
1784+
// Replaces multiple hyphens with single one.
1785+
$output = preg_replace( '/-+/', '-', $output );
1786+
1787+
// Replaces multiple underscores with single one.
1788+
$output = preg_replace( '/_+/', '_', $output );
17791789

1790+
// Remove starting and ending hyphens as a starting hyphen in string might be considered as parameter in bash file/folder creation.
17801791
return trim( $output, '-' );
17811792
}

0 commit comments

Comments
 (0)