paths = $paths ?? new WordPressPathProvider(); } public function write( string $path, string $json ): void { if ( strlen( $json ) > self::MAX_BYTES || '' === $json ) { throw new InvalidArgumentException( 'cli_artifact_size_invalid' ); } if ( ! $this->absolute_path( $path ) || '.json' !== strtolower( substr( $path, -5 ) ) ) { throw new InvalidArgumentException( 'cli_artifact_path_invalid' ); } $parent = realpath( dirname( $path ) ); $name = basename( $path ); if ( false === $parent || ! is_dir( $parent ) || '' === $name || strlen( $name ) > 128 || in_array( $name, array( '.', '..' ), true ) ) { throw new InvalidArgumentException( 'cli_artifact_path_invalid' ); } $target = $parent . DIRECTORY_SEPARATOR . $name; if ( file_exists( $target ) || is_link( $target ) || $this->inside_wordpress( $target ) ) { throw new InvalidArgumentException( 'cli_artifact_target_denied' ); } // Set a private process umask before exclusive creation so the artifact is // never briefly group/world-readable between fopen() and chmod(). $previous_umask = umask( 0077 ); try { // Exclusive creation prevents overwrite and symlink races. This explicit // owner-run CLI artifact is outside WordPress, so WP_Filesystem is not used. $handle = $this->without_php_warning( static function () use ( $target ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- Owner-run terminal artifact with exclusive creation. return fopen( $target, 'x+b' ); } ); } finally { umask( $previous_umask ); } if ( false === $handle ) { throw new RuntimeException( 'cli_artifact_create_failed' ); } $written = 0; $length = strlen( $json ); try { $private = $this->without_php_warning( static function () use ( $target ): bool { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_chmod -- Owner-run terminal artifact must be mode 0600. return chmod( $target, 0600 ); } ); if ( ! $private ) { throw new RuntimeException( 'cli_artifact_permissions_failed' ); } while ( $written < $length ) { $bytes = $this->without_php_warning( static function () use ( $handle, $json, $written ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite -- Bounded owner-run terminal artifact. return fwrite( $handle, substr( $json, $written ) ); } ); if ( false === $bytes || 0 === $bytes ) { throw new RuntimeException( 'cli_artifact_write_failed' ); } $written += $bytes; } } catch ( \Throwable $exception ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $handle ); // The target is this class's freshly created partial artifact only. $this->without_php_warning( static function () use ( $target ): bool { // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink -- Delete only this writer's incomplete private artifact. return unlink( $target ); } ); throw $exception; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $handle ); } private function absolute_path( string $path ): bool { return 1 === preg_match( '#^(?:/|[A-Za-z]:[\\\\/])#', $path ); } /** * Keep an expected local filesystem refusal from exposing the selected path. * The caller still receives the plugin's closed exception code. * * @template T * @param callable():T $operation One bounded native filesystem operation. * @return T */ private function without_php_warning( callable $operation ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- Prevent an expected filesystem refusal exposing the selected path. set_error_handler( static function (): bool { return true; } ); try { return $operation(); } finally { restore_error_handler(); } } private function inside_wordpress( string $path ): bool { $roots = array(); foreach ( array( $this->paths->wordpress_root(), $this->paths->content_root() ) as $root ) { if ( '' !== $root ) { $roots[] = $root; } } $path = str_replace( '\\', '/', $path ); foreach ( array_unique( $roots ) as $candidate ) { $root = realpath( $candidate ); if ( false === $root ) { return true; } $root = rtrim( str_replace( '\\', '/', $root ), '/' ) . '/'; if ( 0 === strpos( $path, $root ) ) { return true; } } return false; } }