| 1 |
<?php |
| 2 |
|
| 3 |
namespace Shellbox\Action; |
| 4 |
|
| 5 |
use GuzzleHttp\Client as GuzzleClient; |
| 6 |
use Shellbox\Command\BoxedCommand; |
| 7 |
use Shellbox\Command\InputFile; |
| 8 |
use Shellbox\Command\InputFileFromUrl; |
| 9 |
use Shellbox\Command\OutputFile; |
| 10 |
use Shellbox\Command\OutputFileToUrl; |
| 11 |
use Shellbox\Command\OutputGlob; |
| 12 |
use Shellbox\Command\OutputGlobToUrl; |
| 13 |
use Shellbox\Command\ServerBoxedExecutor; |
| 14 |
use Shellbox\Command\ServerUnboxedExecutor; |
| 15 |
use Shellbox\ShellboxError; |
| 16 |
|
| 17 |
/** |
| 18 |
* Shell command handler |
| 19 |
*/ |
| 20 |
class ShellAction extends MultipartAction { |
| 21 |
/** |
| 22 |
* @param string[] $pathParts |
| 23 |
*/ |
| 24 |
protected function execute( $pathParts ) { |
| 25 |
$commandData = $this->getRequiredParam( 'command' ); |
| 26 |
$command = $this->createCommand( $commandData ); |
| 27 |
$this->validateInputFiles( $command->getInputFiles() ); |
| 28 |
$this->validateRoute( $command->getRouteName(), $pathParts ); |
| 29 |
$this->validateOutputs( $command->getOutputFiles(), $command->getOutputGlobs() ); |
| 30 |
$result = $command->execute(); |
| 31 |
$binaryData = []; |
| 32 |
if ( $result->getStdout() !== null ) { |
| 33 |
$binaryData['stdout'] = $result->getStdout(); |
| 34 |
} |
| 35 |
if ( $result->getStderr() !== null ) { |
| 36 |
$binaryData['stderr'] = $result->getStderr(); |
| 37 |
} |
| 38 |
$this->writeResult( |
| 39 |
[ |
| 40 |
'exitCode' => $result->getExitCode(), |
| 41 |
'uploadedFiles' => $result->getUploadedFileNames(), |
| 42 |
], |
| 43 |
$binaryData, |
| 44 |
$result->getReceivedFileNames() |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
protected function getActionName() { |
| 49 |
return 'shell'; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @return ServerBoxedExecutor |
| 54 |
*/ |
| 55 |
private function createExecutor() { |
| 56 |
$unboxedExecutor = new ServerUnboxedExecutor( $this->tempDirManager ); |
| 57 |
$unboxedExecutor->setLogger( $this->logger ); |
| 58 |
$unboxedExecutor->addWrappersFromConfiguration( [ |
| 59 |
'useSystemd' => $this->getConfig( 'useSystemd' ), |
| 60 |
'useBashWrapper' => $this->getConfig( 'useBashWrapper' ), |
| 61 |
'useFirejail' => $this->getConfig( 'useFirejail' ), |
| 62 |
'firejailPath' => $this->getConfig( 'firejailPath' ), |
| 63 |
'firejailProfile' => $this->getConfig( 'firejailProfile' ) |
| 64 |
] ); |
| 65 |
|
| 66 |
$executor = new ServerBoxedExecutor( $unboxedExecutor, $this->tempDirManager ); |
| 67 |
$executor->setLogger( $this->logger ); |
| 68 |
$executor->setValidationConfig( [ |
| 69 |
'allowedRoutes' => $this->getConfig( 'allowedRoutes' ), |
| 70 |
'routeSpecs' => $this->getConfig( 'routeSpecs' ) |
| 71 |
] ); |
| 72 |
|
| 73 |
if ( $this->getConfig( 'allowUrlFiles' ) ) { |
| 74 |
$executor->setUrlFileClient( |
| 75 |
new GuzzleClient( [ |
| 76 |
'connect_timeout' => $this->getConfig( 'urlFileConnectTimeout' ), |
| 77 |
'timeout' => $this->getConfig( 'urlFileRequestTimeout' ), |
| 78 |
'exceptions' => false, |
| 79 |
] ), |
| 80 |
[ |
| 81 |
'concurrency' => $this->getConfig( 'urlFileConcurrency' ), |
| 82 |
'uploadAttempts' => $this->getConfig( 'urlFileUploadAttempts' ), |
| 83 |
'retryDelay' => $this->getConfig( 'urlFileRetryDelay' ), |
| 84 |
], |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
return $executor; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @param array $commandData |
| 93 |
* @return BoxedCommand |
| 94 |
*/ |
| 95 |
private function createCommand( $commandData ) { |
| 96 |
$executor = $this->createExecutor(); |
| 97 |
$command = $executor->createCommand(); |
| 98 |
$command->setClientData( $commandData ); |
| 99 |
$stdin = $this->getParam( 'stdin' ); |
| 100 |
if ( $stdin !== null ) { |
| 101 |
$command->stdin( $stdin ); |
| 102 |
} |
| 103 |
return $command; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Confirm that the received input file list matches the one in the command |
| 108 |
* client data. This ensures that the input file list will be correctly |
| 109 |
* validated by Validator. |
| 110 |
* |
| 111 |
* @param InputFile[] $clientDataFiles |
| 112 |
* @throws ShellboxError |
| 113 |
*/ |
| 114 |
private function validateInputFiles( $clientDataFiles ) { |
| 115 |
$allowUrlFiles = $this->getConfig( 'allowUrlFiles' ); |
| 116 |
$sent = []; |
| 117 |
foreach ( $clientDataFiles as $path => $file ) { |
| 118 |
if ( $file instanceof InputFileFromUrl ) { |
| 119 |
if ( !$allowUrlFiles ) { |
| 120 |
throw new ShellboxError( |
| 121 |
'Received a URL download request but allowUrlFiles is ' . |
| 122 |
'false in the server config' ); |
| 123 |
} |
| 124 |
} else { |
| 125 |
$sent[] = $path; |
| 126 |
} |
| 127 |
} |
| 128 |
$received = $this->getReceivedFileNames(); |
| 129 |
sort( $received ); |
| 130 |
sort( $sent ); |
| 131 |
|
| 132 |
if ( $sent !== $received ) { |
| 133 |
throw new ShellboxError( |
| 134 |
"The received file list does not match the command client data" ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Confirm that the path route matches the command parameter route |
| 140 |
* |
| 141 |
* @param string $commandRoute |
| 142 |
* @param string[] $pathParts |
| 143 |
* @throws ShellboxError |
| 144 |
*/ |
| 145 |
private function validateRoute( $commandRoute, $pathParts ) { |
| 146 |
if ( count( $pathParts ) !== 1 || $pathParts[0] !== $commandRoute ) { |
| 147 |
throw new ShellboxError( |
| 148 |
"The request path does not match the route in the command" ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Check for URL output files if they are not allowed. |
| 154 |
* |
| 155 |
* We don't inject an HTTP client when download/upload is not allowed, so |
| 156 |
* there are two layers of protection, but we can give a better error |
| 157 |
* message here. |
| 158 |
* |
| 159 |
* @param OutputFile[] $files |
| 160 |
* @param OutputGlob[] $globs |
| 161 |
*/ |
| 162 |
private function validateOutputs( $files, $globs ) { |
| 163 |
if ( !$this->getConfig( 'allowUrlFiles' ) ) { |
| 164 |
foreach ( $files as $file ) { |
| 165 |
if ( $file instanceof OutputFileToUrl ) { |
| 166 |
throw new ShellboxError( |
| 167 |
'Received a URL upload request but allowUrlFiles is ' . |
| 168 |
'false in the server config' |
| 169 |
); |
| 170 |
} |
| 171 |
} |
| 172 |
foreach ( $globs as $glob ) { |
| 173 |
if ( $glob instanceof OutputGlobToUrl ) { |
| 174 |
throw new ShellboxError( |
| 175 |
'Received a URL upload request but allowUrlFiles is ' . |
| 176 |
'false in the server config' |
| 177 |
); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
} |