Skip to content

Commit 61c989d

Browse files
authored
Merge pull request #4300 from Laravel-Backpack/fix-command-in-4.0
Add fix command in 4.0
2 parents 88baaad + def2dd2 commit 61c989d

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

src/BackpackServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class BackpackServiceProvider extends ServiceProvider
1919
\Backpack\CRUD\app\Console\Commands\PublishBackpackUserModel::class,
2020
\Backpack\CRUD\app\Console\Commands\PublishBackpackMiddleware::class,
2121
\Backpack\CRUD\app\Console\Commands\PublishView::class,
22+
\Backpack\CRUD\app\Console\Commands\Fix::class,
2223
];
2324

2425
// Indicates if loading of the provider is deferred.

src/app/Console/Commands/Fix.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\app\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class Fix extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'backpack:fix';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Fix known Backpack security issues.';
22+
23+
/**
24+
* Execute the console command.
25+
*
26+
* @return mixed
27+
*/
28+
public function handle()
29+
{
30+
$this->fixErrorViews();
31+
}
32+
33+
private function fixErrorViews()
34+
{
35+
$errorsDirectory = base_path('resources/views/errors');
36+
37+
$this->line('Checking error views...');
38+
39+
// check if the `resources/views/errors` directory exists
40+
if (! is_dir($errorsDirectory)) {
41+
$this->info('Your error views are not vulnerable. Nothing to do here.');
42+
43+
return;
44+
}
45+
46+
$views = scandir($errorsDirectory);
47+
$views = array_filter($views, function ($file) {
48+
// eliminate ".", ".." and any hidden files like .gitignore or .gitkeep
49+
return substr($file, 0, 1) != '.';
50+
});
51+
52+
// check if there are actually views inside the directory
53+
if (! count($views)) {
54+
$this->info('Your error views are not vulnerable. Nothing to do here.');
55+
56+
return;
57+
}
58+
59+
$autofixed = true;
60+
foreach ($views as $key => $view) {
61+
$contents = file_get_contents($errorsDirectory.'/'.$view);
62+
63+
// does it even work with exception messages?
64+
if (strpos($contents, '->getMessage()') == false) {
65+
continue;
66+
}
67+
68+
// does it already escape the exception message?
69+
if (strpos($contents, 'e($exception->getMessage())') !== false) {
70+
$this->info($view.' was ok.');
71+
continue;
72+
}
73+
74+
// cover the most likely scenario, where the file has not been edited at all
75+
$new_contents = str_replace('$exception->getMessage()?$exception->getMessage():$default_error_message', '$exception->getMessage()?e($exception->getMessage()):$default_error_message', $contents);
76+
77+
if ($new_contents != $contents) {
78+
file_put_contents($errorsDirectory.'/'.$view, $new_contents);
79+
$this->info($view.' has been fixed.');
80+
continue;
81+
}
82+
83+
$this->error($view.' could not be fixed automatically.');
84+
$autofixed = false;
85+
}
86+
87+
if ($autofixed == false) {
88+
$this->error('Some error views could not be fixed automatically. Please look inside your "resources/views/errors" directory and make sure exception messages are escaped before outputting. It should be e($exception->getMessage()) instead of $exception->getMessage(). Alternatively, outputting should be done using {{ }} instead of {!! !!}');
89+
}
90+
}
91+
}

src/resources/error_views/400.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
@php
1313
$default_error_message = "Please <a href='javascript:history.back()''>go back</a> or return to <a href='".url('')."'>our homepage</a>.";
1414
@endphp
15-
{!! isset($exception)? ($exception->getMessage()?$e(exception->getMessage()):$default_error_message): $default_error_message !!}
15+
{!! isset($exception)? ($exception->getMessage()?e($exception->getMessage()):$default_error_message): $default_error_message !!}
1616
@endsection

0 commit comments

Comments
 (0)