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

Use a middleware to protect ignition routes #93

Merged
merged 3 commits into from
Sep 5, 2019
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
13 changes: 13 additions & 0 deletions config/ignition.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,17 @@
'remote_sites_path' => env('IGNITION_REMOTE_SITES_PATH', ''),
'local_sites_path' => env('IGNITION_LOCAL_SITES_PATH', ''),



/*
|--------------------------------------------------------------------------
| Housekeeping Endpoint Prefix
|--------------------------------------------------------------------------
|
| Ignition registers a couple of routes if it is enabled. Here you can define
| the route prefix it should use.
|
*/
'housekeeping_endpoint_prefix' => '_ignition',

];
2 changes: 1 addition & 1 deletion src/ErrorPage/ErrorPageViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function toArray(): array
'config' => $this->config(),
'solutions' => $this->solutions(),
'report' => $this->report(),
'housekeepingEndpoint' => config('flare.housekeeping_endpoint_prefix', 'flare'),
'housekeepingEndpoint' => config('ignition.housekeeping_endpoint_prefix', '_ignition'),
'styles' => $this->styles(),
'scripts' => $this->scripts(),
'tabs' => $this->tabs(),
Expand Down
31 changes: 31 additions & 0 deletions src/Http/Middleware/IgnitionEnabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Facade\Ignition\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class IgnitionEnabled
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a test for this middleware, so we're 100% sure it works correctly.

{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (! $this->ignitionEnabled()) {
abort(404);
}

return $next($request);

}

protected function ignitionEnabled(): bool
{
return config('app.debug');
}
}
25 changes: 12 additions & 13 deletions src/IgnitionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Facade\Ignition;

use Facade\Ignition\Http\Middleware\IgnitionEnabled;
use Monolog\Logger;
use Illuminate\Support\Arr;
use Facade\FlareClient\Flare;
Expand Down Expand Up @@ -117,19 +118,17 @@ protected function registerViewEngines()

protected function registerHousekeepingRoutes()
{
if (! config('app.debug')) {
return $this;
}

Route::prefix(config('flare.housekeeping_endpoint_prefix', 'flare'))
->group(function () {
Route::get('health-check', HealthCheckController::class);
Route::post('execute-solution', ExecuteSolutionController::class);
Route::post('share-report', ShareReportController::class);

Route::get('scripts/{script}', ScriptController::class);
Route::get('styles/{style}', StyleController::class);
});
Route::group([
'prefix' => config('ignition.housekeeping_endpoint_prefix', '_ignition'),
'middleware' => [IgnitionEnabled::class],
], function () {
Route::get('health-check', HealthCheckController::class);
Route::post('execute-solution', ExecuteSolutionController::class);
Route::post('share-report', ShareReportController::class);

Route::get('scripts/{script}', ScriptController::class);
Route::get('styles/{style}', StyleController::class);
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't routes be registered in boot()? That goes for almost every service in the IoC container - don't resolve them until all other service providers had a chance to modify / extend / swap them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, in our case this would introduce some issues if an exception would occur in a service providers boot method prior to Ignition.
Because our whoops handler would already be registered, but the routes would not. That's why we are registering the routes here too.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about $this->app->resolving('router', function () { ... });?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marcel and I discussed this. We don't think it is necessary. The router will always have been registered when our provider gets called.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point is not so much your provider (although you shouldn't rely on the provider ordering). You also should be a good citizen and allow other packages to swap out the router / extend it in some way, before you access it.

That's at least 50% of the point of the IoC container and contracts, as I understand them. 😉


return $this;
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Http/Middleware/IgnitionEnabledTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Facade\Ignition\Tests\Http\Middleware;

use Facade\Ignition\Tests\TestCase;
use Illuminate\Support\Facades\Route;
use Facade\Ignition\Http\Middleware\IgnitionEnabled;

class IgnitionEnabledTest extends TestCase
{
/** @test */
public function it_returns_404_with_debug_mode_disabled()
{
$this->app['config']['app.debug'] = false;

Route::get('middleware-test', function () {
return 'success';
})->middleware([IgnitionEnabled::class]);

$this->get('middleware-test')->assertStatus(404);
}

/** @test */
public function it_returns_ok_with_debug_mode_enabled()
{
$this->app['config']['app.debug'] = true;

Route::get('middleware-test', function () {
return 'success';
})->middleware([IgnitionEnabled::class]);

$this->get('middleware-test')->assertStatus(200);
}
}