Skip to content

Commit

Permalink
Feature/better escaping (#69)
Browse files Browse the repository at this point in the history
* Widen test to allow commands as arrays.
* Allow CmdExecuteService the execute commands passed as arrays.
* Update PHP version/badge
  • Loading branch information
adam-vessey authored Jan 3, 2025
1 parent a02522e commit 0e7b0e4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
5 changes: 4 additions & 1 deletion CmdExecuteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public function execute($cmd, $data): \Closure
$pipes = [];

// Start process, telling it to use STDIN for input and STDOUT for output.
$cmd = escapeshellcmd($cmd);
$cmd = match (gettype($cmd)) {
'array' => $cmd,
default => escapeshellcmd($cmd),
};
$process = proc_open($cmd, $descr, $pipes);

// Get the data into pipe only if data is resource
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
![Crayfish](https://user-images.githubusercontent.com/2371345/48163075-11c6cf80-e2b5-11e8-8b5b-991b366014a5.png)
# Crayfish Commons

[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.4-8892BF.svg?style=flat-square)](https://php.net/)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.0-8892BF.svg?style=flat-square)](https://php.net/)
[![Build Status](https://github.com/islandora/crayfish-commons/actions/workflows/build-2.x.yml/badge.svg)](https://github.com/Islandora/crayfish-commons/actions)
[![Contribution Guidelines](http://img.shields.io/badge/CONTRIBUTING-Guidelines-blue.svg)](./CONTRIBUTING.md)
[![LICENSE](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](./LICENSE)
Expand All @@ -18,7 +18,7 @@ Shared Classes include:

## Requirements

* PHP 7.4+
* PHP 8.0+
* [Composer](https://getcomposer.org/)

## Installation
Expand Down
43 changes: 38 additions & 5 deletions Tests/CmdExecuteServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,25 @@
class CmdExecuteServiceTest extends AbstractCrayfishCommonsTestCase
{

public function testExecuteWithResource()
public function dataProviderWithResource()
{
return [
'test as string' => [
'sort -',
],
'test as array' => [
[
'sort',
'-',
],
]
];
}

/**
* @dataProvider dataProviderWithResource
*/
public function testExecuteWithResource(string|array $command)
{
$service = new CmdExecuteService($this->logger);

Expand All @@ -16,8 +34,6 @@ public function testExecuteWithResource()
fwrite($data, $string);
rewind($data);

$command = 'sort -';

$callback = $service->execute($command, $data);

$this->assertTrue(is_callable($callback), "execute() must return a callable.");
Expand All @@ -36,11 +52,28 @@ public function testExecuteWithResource()
$callback();
}

public function testExecuteWithoutResource()
public function dataProviderWithoutResource()
{
return [
'test as string' => [
'echo "derp"',
],
'test as array' => [
[
'echo',
'derp',
],
]
];
}

/**
* @dataProvider dataProviderWithoutResource
*/
public function testExecuteWithoutResource(string|array $command)
{
$service = new CmdExecuteService($this->logger);

$command = 'echo "derp"';
$callback = $service->execute($command, "");

$this->assertTrue(is_callable($callback), "execute() must return a callable.");
Expand Down

0 comments on commit 0e7b0e4

Please sign in to comment.