1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-09-20 13:34:04 +00:00

Make TenantAssetController not depend on suffixed storage_path()

The only thing the controller now depends on is that FilesystemTenancyBootstrapper needs to be enabled (so basically, the same dependency as before, but before this, there was the extra "suffix_storage_path === true" dependency -- not literally, storage_path() just had to be suffixed in tenant context, otherwise, the controller would read from the central storage in tenant context).
This commit is contained in:
lukinovec 2026-08-17 16:52:05 +02:00 committed by Samuel Stancl
parent a9a727451e
commit 11fec0345d

View file

@ -9,6 +9,7 @@ use Exception;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Routing\Controllers\HasMiddleware; use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware; use Illuminate\Routing\Controllers\Middleware;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Throwable; use Throwable;
@ -51,12 +52,27 @@ class TenantAssetController implements HasMiddleware
? (static::$headers)($request) ? (static::$headers)($request)
: static::$headers; : static::$headers;
return response()->file(storage_path("app/public/$path"), $headers); return response()->file($this->assetRoot() . "/$path", $headers);
} catch (Throwable) { } catch (Throwable) {
abort(404); abort(404);
} }
} }
/**
* Assets are served from app/public inside the tenant's storage directory.
*
* The directory is resolved using the FilesystemTenancyBootstrapper (rather than storage_path(),
* so that it's tenant-scoped regardless of the suffix_storage_path config).
*/
protected function assetRoot(): string
{
if ($tenant = tenant()) {
return FilesystemTenancyBootstrapper::getBoundTenantStoragePath($tenant) . '/app/public';
}
return storage_path('app/public');
}
/** /**
* Prevent path traversal attacks. This is generally a non-issue on modern * Prevent path traversal attacks. This is generally a non-issue on modern
* webservers but it's still worth handling on the application level as well. * webservers but it's still worth handling on the application level as well.
@ -67,9 +83,9 @@ class TenantAssetController implements HasMiddleware
{ {
$this->abortIf($path === null, 'Empty path'); $this->abortIf($path === null, 'Empty path');
$allowedRoot = realpath(storage_path('app/public')); $allowedRoot = realpath($this->assetRoot());
// `storage_path('app/public')` doesn't exist, so it cannot contain files // The asset root doesn't exist, so it cannot contain files
$this->abortIf($allowedRoot === false, "Storage root doesn't exist"); $this->abortIf($allowedRoot === false, "Storage root doesn't exist");
$attemptedPath = realpath("{$allowedRoot}/{$path}"); $attemptedPath = realpath("{$allowedRoot}/{$path}");