Skip to content

Commit

Permalink
Fixes attempt to sanitize output when there is nothing to sanitize
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Aug 3, 2021
1 parent 7750049 commit 286d58b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
5 changes: 3 additions & 2 deletions app/Repositories/RemoteRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function tail($files, $callback, $options = [])
$command = collect(explode(' ', $this->ssh()))->merge(['tail', '-n', '500'])
->merge($options)
->push('$(ls -1td '.$files->implode(' ').' 2>/dev/null | head -n1)')
->filter()
->values()
->all();

Expand All @@ -92,7 +93,7 @@ public function tail($files, $callback, $options = [])
$output = [];

foreach ($process as $line) {
if (strpos($line, $this->sanitizableOutput) !== 0) {
if ($this->sanitizableOutput && strpos($line, $this->sanitizableOutput) !== 0) {
$output[] = $line;

$callback($line);
Expand All @@ -118,7 +119,7 @@ public function exec($command)

exec($this->ssh($command), $output, $exitCode);

if (isset($output[0]) && strpos($output[0], $this->sanitizableOutput) === 0) {
if ($this->sanitizableOutput && isset($output[0]) && strpos($output[0], $this->sanitizableOutput) === 0) {
unset($output[0]);
}

Expand Down
71 changes: 70 additions & 1 deletion tests/Unit/Repositories/RemoteRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,75 @@

use App\Repositories\RemoteRepository;

it('ensure current server', function () {
test('ensures current server', function () {
(new RemoteRepository('foo'))->exec('bar');
})->throws('Current server unresolvable.');

test('exec removes sanitizable output', function () {
$remote = new LocalRepository(
'manpath: can\'t set the locale'
);

$command = collect([
'manpath: can\'t set the locale',
'[00:01] FOO',
'[00:02] BAR',
])->map(function ($line) {
return 'echo "'.$line.'"';
})->implode(' && ');

expect($remote->exec($command))->toBe([0, [
'[00:01] FOO',
'[00:02] BAR',
]]);

$command = collect([
'',
'[00:01] FOO',
'[00:02] BAR',
])->map(function ($line) {
return 'echo "'.$line.'"';
})->implode(' && ');

expect($remote->exec($command))->toBe([0, [
'',
'[00:01] FOO',
'[00:02] BAR',
]]);
});

test('exec not removes sanitizable output if is empty', function () {
$remote = new LocalRepository(null);

$command = collect([
'[00:01] FOO',
'[00:02] BAR',
])->map(function ($line) {
return 'echo "'.$line.'"';
})->implode(' && ');

expect($remote->exec($command))->toBe([0, [
'[00:01] FOO',
'[00:02] BAR',
]]);
});

class LocalRepository extends RemoteRepository
{
protected $sanitizableOutput;

public function __construct($sanitizableOutput)
{
$this->sanitizableOutput = $sanitizableOutput;
}

protected function ssh($command = null)
{
return $command;
}

public function ensureSshIsConfigured()
{
// ..
}
}

0 comments on commit 286d58b

Please sign in to comment.