Skip to content

Commit

Permalink
Backport security for old PHP versions (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVanderbist committed Mar 8, 2021
1 parent 9fc6c3d commit f2dc8ba
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Solutions/MakeViewVariableOptionalSolution.php
Expand Up @@ -4,6 +4,7 @@

use Facade\IgnitionContracts\RunnableSolution;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Str;

class MakeViewVariableOptionalSolution implements RunnableSolution
{
Expand Down Expand Up @@ -72,6 +73,10 @@ public function run(array $parameters = [])

public function makeOptional(array $parameters = [])
{
if (!$this->isSafePath($parameters['viewFile'])) {
return false;
}

$originalContents = file_get_contents($parameters['viewFile']);
$newContents = str_replace('$'.$parameters['variableName'], '$'.$parameters['variableName']." ?? ''", $originalContents);

Expand All @@ -87,6 +92,19 @@ public function makeOptional(array $parameters = [])
return $newContents;
}

protected function isSafePath(string $path): bool
{
if (!Str::startsWith($path, ['/', './'])) {
return false;
}

if (!Str::endsWith($path, '.blade.php')) {
return false;
}

return true;
}

protected function generateExpectedTokens(array $originalTokens, string $variableName): array
{
$expectedTokens = [];
Expand Down

0 comments on commit f2dc8ba

Please sign in to comment.