#111640CommandUtility::getCommand() fails for commands only found via which
Description
Problem
CommandUtility::checkCommand() falls back to which when a command is not found in its search paths (SYS/binPath, SYS/binSetup, /usr/bin/, /usr/local/bin/). That fallback stores the result under the wrong key:
$cmd = @self::exec('which ' . self::escapeShellArgument($cmd), $output, $returnValue);
if ($returnValue === 0) {
self::$applications[$cmd]['app'] = $cmd;
self::$applications[$cmd]['path'] = PathUtility::dirname($cmd) . '/';
self::$applications[$cmd]['valid'] = true;
return true;
}
$cmd is overwritten with the output of which (e.g. /opt/bin/identify), so the entry is stored under the full path instead of under identify. As a result:
checkCommand('identify')returnstrue, butgetCommand('identify')then readsself::$applications['identify'], which does not exist. It logs two warnings and returns an empty string instead of the command:
PHP Warning: Undefined array key "identify" in .../CommandUtility.php line 300 PHP Warning: Trying to access array offset on null in .../CommandUtility.php line 300
- The entry is never found again, so every call runs
whichagain and logs the warnings again. - Even with the correct key,
path . appwould give/opt/bin//opt/bin/identify, becauseappholds the full path.
Steps to reproduce
Any command that is on PATH but not in one of the search paths above reaches the fallback. This is typical with open_basedir in effect, where the regular is_executable() check of the search paths fails.
- Put an executable
mytoolinto a directory that is onPATHbut not configured inSYS/binPath/SYS/binSetup(or enableopen_basedirwithout the binary directory). - Call
CommandUtility::checkCommand('mytool'): returnstrue. - Call
CommandUtility::getCommand('mytool').
Actual: two PHP warnings per call, return value ''.
Expected: getCommand('mytool') returns the full path found by which, without warnings, and the second call uses the cached result.
Seen in a TYPO3 14.3.12 installation on shared hosting: an extension calls CommandUtility::getCommand('identify') on each file upload and logs both warnings twice per upload.
Possible fix
Keep the requested name as the key and split the path returned by which:
$path = @self::exec('which ' . self::escapeShellArgument($cmd), $output, $returnValue);
if ($returnValue === 0 && $path !== false && $path !== '') {
self::$applications[$cmd]['app'] = basename($path);
self::$applications[$cmd]['path'] = PathUtility::dirname($path) . '/';
self::$applications[$cmd]['valid'] = true;
return true;
}
Versions
Affects main, 14.3 and 13.4. The wrong key has been there since at least 2012 (t3lib_exec merged into CommandUtility). Change https://review.typo3.org/c/Packages/TYPO3.CMS/+/79892 (#70869, #85174, “Fix command detection with open_basedir in effect”) replaced the is_executable() check after which with the exit code. This made the fallback reachable under open_basedir, and so the broken entry now shows up there as well.
Related: #70869, #85174
Reviews 1
History 01
-
Gerrit Code Review pushed patch set 1 for main to change 98106 status
NewUnder Review 11/27/26, 12:36