stream icon indicating copy to clipboard operation
stream copied to clipboard

posix_getpwuid can return false

Open trevormills-xwp opened this issue 4 months ago • 0 comments

Bug Report

Expected Behavior

When running a CLI job from a shell that does not have a user, if I do an action that causes the into the WP_Stream\Log::log() method to fire ( e.g. delete or add post meta ), I would like the action to complete without emitting a PHP Warning.

To get into a shell that doesn't have a user, imagine a docker container for WP and run:

docker exec -it -u 1234 my-wp-container-1 /bin/bash

This is a common pattern when wanting to run the shell as a non-root user.

Actual Behavior

The following warning is emitted:

PHP Warning: Trying to access array offset on value of type bool in /var/www/html/wp-content/plugin s/stream/classes/class-log.php on line 97

Suggested Fix

The issue stems from these lines:

		if ( 'wp_cli' === $agent && function_exists( 'posix_getuid' ) ) {
			$uid       = posix_getuid();
			$user_info = posix_getpwuid( $uid );

			$user_meta['system_user_id']   = (int) $uid;
			$user_meta['system_user_name'] = (string) $user_info['name'];
		}

posix_getpwuid( $uid ) returns false in this case. Suggested fix is to update the system_user_name line to:

$user_meta['system_user_name'] = is_array( $user_info ) ? (string) $user_info['name'] : '';

trevormills-xwp avatar Aug 15 '25 14:08 trevormills-xwp