Skip to content

Commit

Permalink
Allow exceptions to PHP's escapeshellcmd()
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Jun 18, 2022
1 parent 84cc92a commit a23834b
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Dist_Archive_Command.php
Expand Up @@ -196,7 +196,7 @@ function( $ignored_file ) {
}

WP_CLI::debug( "Running: {$cmd}", 'dist-archive' );
$ret = WP_CLI::launch( escapeshellcmd( $cmd ), false, true );
$ret = WP_CLI::launch( $this->escapeshellcmd( $cmd, array( '^' ) ), false, true );
if ( 0 === $ret->return_code ) {
$filename = pathinfo( $archive_file, PATHINFO_BASENAME );
WP_CLI::success( "Created {$filename}" );
Expand Down Expand Up @@ -294,4 +294,27 @@ private function parse_doc_block( $docblock ) {
}
return $tags;
}

/**
* Run PHP's escapeshellcmd() then undo escaping known intentional characters.
*
* Escaped by default: &#;`|*?~<>^()[]{}$\, \x0A and \xFF. ' and " are escaped when not paired.
*
* @see escapeshellcmd()
*
* @param string $cmd The shell command to escape.
* @param string[] $whitelist Array of exceptions to allow in the escaped command.
*
* @return string
*/
protected function escapeshellcmd( $cmd, $whitelist ) {

$escaped_command = escapeshellcmd( $cmd );

foreach ( $whitelist as $undo_escape ) {
$escaped_command = str_replace( '\\' . $undo_escape, $undo_escape, $escaped_command );
}

return $escaped_command;
}
}

0 comments on commit a23834b

Please sign in to comment.