Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,20 @@ private function command_prefix( string $bin ): string|WP_Error {
if ( is_wp_error( $node ) ) {
return $node;
}

$entrypoint = dirname( dirname( $bin ) ) . DIRECTORY_SEPARATOR . 'packages' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'dist' . DIRECTORY_SEPARATOR . 'index.js';
if ( ! is_file( $entrypoint ) ) {
return new WP_Error(
'wp_codebox_bin_missing',
'The bundled WP Codebox JavaScript entrypoint is missing.',
array(
'status' => 500,
'path' => $entrypoint,
)
);
}

return escapeshellarg( $node ) . ' ' . escapeshellarg( $entrypoint );
}

return escapeshellarg( $bin );
Expand Down
33 changes: 33 additions & 0 deletions scripts/php-agent-runtime-execution-smoke.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

declare(strict_types=1);

$plugin_root = sys_get_temp_dir() . '/wp-codebox-nonexecutable-cli-' . bin2hex( random_bytes( 4 ) );
$wrapper = $plugin_root . '/vendor/wp-codebox-cli/bin/wp-codebox';
$entrypoint = $plugin_root . '/vendor/wp-codebox-cli/packages/cli/dist/index.js';
mkdir( dirname( $wrapper ), 0777, true );
mkdir( dirname( $entrypoint ), 0777, true );
file_put_contents( $wrapper, "#!/usr/bin/env bash\n" );
file_put_contents( $entrypoint, "#!/usr/bin/env node\n" );
chmod( $wrapper, 0644 );

define( 'ABSPATH', __DIR__ );
define( 'WP_CODEBOX_PLUGIN_PATH', $plugin_root . '/' );

final class WP_Error {
private string $code;
Expand Down Expand Up @@ -151,4 +161,27 @@ function smoke_assert( bool $condition, string $message ): void {
$case['assert']( $result );
}

$runner = new WP_Codebox_Agent_Sandbox_Runner(
array(
'command_resolver' => static fn( string $command ): string => 'node' === $command ? '/usr/bin/node' : '',
)
);
$method = new ReflectionMethod( $runner, 'command_prefix' );
$prefix = $method->invoke( $runner, $wrapper );
smoke_assert( ! is_wp_error( $prefix ), 'non-executable bundled wrapper resolves to a command prefix' );
smoke_assert(
escapeshellarg( '/usr/bin/node' ) . ' ' . escapeshellarg( $entrypoint ) === $prefix,
'non-executable bundled wrapper runs its JavaScript entrypoint through Node'
);

unlink( $wrapper );
unlink( $entrypoint );
rmdir( dirname( $wrapper ) );
rmdir( dirname( $entrypoint ) );
rmdir( dirname( dirname( $entrypoint ) ) );
rmdir( dirname( dirname( dirname( $entrypoint ) ) ) );
rmdir( dirname( dirname( dirname( dirname( $entrypoint ) ) ) ) );
rmdir( dirname( dirname( dirname( $wrapper ) ) ) );
rmdir( $plugin_root );

echo "agent runtime execution smoke passed\n";