Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/better escaping #69

Merged
merged 3 commits into from
Jan 3, 2025
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
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
Loading