diff --git a/src/Controllers/TenantAssetsController.php b/src/Controllers/TenantAssetsController.php index 67478cf9..5549da0d 100644 --- a/src/Controllers/TenantAssetsController.php +++ b/src/Controllers/TenantAssetsController.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Controllers; use Illuminate\Routing\Controller; +use Throwable; class TenantAssetsController extends Controller { @@ -15,11 +16,13 @@ class TenantAssetsController extends Controller $this->middleware(static::$tenancyMiddleware); } - public function asset($path) + public function asset($path = null) { + abort_if($path === null, 404); + try { return response()->file(storage_path("app/public/$path")); - } catch (\Throwable $th) { + } catch (Throwable $th) { abort(404); } } diff --git a/tests/TenantAssetTest.php b/tests/TenantAssetTest.php index 77a130b4..703ac65e 100644 --- a/tests/TenantAssetTest.php +++ b/tests/TenantAssetTest.php @@ -126,4 +126,19 @@ class TenantAssetTest extends TestCase $this->assertSame($original, asset('foo')); } + + public function test_asset_controller_returns_a_404_when_no_path_is_provided() + { + TenantAssetsController::$tenancyMiddleware = InitializeTenancyByRequestData::class; + + $tenant = Tenant::create(); + + tenancy()->initialize($tenant); + $response = $this->get(tenant_asset(null), [ + 'X-Tenant' => $tenant->id, + ]); + + $response->assertNotFound(); + } + }