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

Fix MakeViewVariableOptionalSolution to disallow stream wrappers and files that do not end in .blade.php #334

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 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,11 @@ public function run(array $parameters = [])

public function makeOptional(array $parameters = [])
{
# Only allow full or relative paths, and paths that end in .blade.php
if (!Str::startsWith($parameters['viewFile'], ['/', './']) || !Str::endsWith($parameters['viewFile'], '.blade.php')) {
cfreal marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

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

Expand Down
53 changes: 53 additions & 0 deletions tests/Solutions/MakeViewVariableOptionalSolutionTest.php
@@ -0,0 +1,53 @@
<?php

namespace Facade\Ignition\Tests\Solutions;

use Facade\Ignition\Exceptions\ViewException;
use Facade\Ignition\Solutions\MakeViewVariableOptionalSolution;
use Facade\Ignition\Support\ComposerClassMap;
use Facade\Ignition\Tests\TestCase;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;

class MakeViewVariableOptionalSolutionTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

View::addLocation(__DIR__.'/../stubs/views');

$this->app->bind(
ComposerClassMap::class,
function () {
return new ComposerClassMap(__DIR__.'/../../vendor/autoload.php');
}
);
}

/** @test */
public function it_does_not_open_scheme_paths()
{
$solution = $this->getSolutionForPath('php://filter/resource=./tests/stubs/views/blade-exception.blade.php');
$this->assertFalse($solution->isRunnable());
}

/** @test */
public function it_does_open_relative_paths()
{
$solution = $this->getSolutionForPath('./tests/stubs/views/blade-exception.blade.php');
$this->assertTrue($solution->isRunnable());
}

/** @test */
public function it_does_not_open_other_extentions()
{
$solution = $this->getSolutionForPath('./tests/stubs/views/php-exception.php');
$this->assertFalse($solution->isRunnable());
}

protected function getSolutionForPath($path): MakeViewVariableOptionalSolution
{
return new MakeViewVariableOptionalSolution('notSet', $path);
}
}