mirror of
https://github.com/projectsend/projectsend.git
synced 2026-09-17 00:55:07 +00:00
757fba19ca
Two pieces of groundwork, no behaviour change.
FileWasStored is dispatched from StoreUploadedFile, which every upload
path converges on — the chunked flow staff and clients share, and the
synchronous POST beside it. A listener therefore sees each upload once
without knowing which route produced it, which is the property that makes
it usable from outside this repository. A notification, not a filter:
nothing on it is mutable, and anything that needs to influence an upload
has to do so before the bytes land, which is what ResolvingUploadDisk is
already for.
CreateShareLink is the other half. Minting a link was a ShareLinksController
private concern, and the controller is an HTTP handler behind `staff`
middleware — so a link now needs making from outside a request as well.
Two copies of "make a token, write the row, log it" would drift, and the
half most likely to drift is the token, which is the entire authorization
for /s/{token}: there is no session behind it and no second factor, so
being unguessable is its only defence. Anything minted through the action
gets Str::random(32) — about 190 bits, more than a UUID's 122 — and never
a chosen value. The chosen-token path stays in the controller, where a
person is typing one into a form and its minimum length can be argued
about in a validation rule.
The permission questions stay in the controller too. Whether somebody may
set an expiry or a download cap is a fact about them, and the action has
no viewer to ask; it takes both already resolved, including the expiry,
because "the end of the 12th" depends on whose timezone you are in.
Five tests, including that the file a listener receives is complete and
readable rather than half-built, and that the staff form still refuses an
expiry to somebody without the permission after the extraction.
63 lines
2.0 KiB
PHP
63 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Files\Sharing;
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Audit\Action;
|
|
use App\Modules\Audit\ActivityLogger;
|
|
use App\Modules\Files\Models\File;
|
|
use App\Modules\Files\Models\ShareLink;
|
|
use Carbon\CarbonInterface;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Minting a public link for a file, in one place.
|
|
*
|
|
* Extracted from ShareLinksController rather than invented: the
|
|
* controller is an HTTP handler behind `staff` middleware, and a link now
|
|
* needs creating from outside a request as well. Two copies of "make a
|
|
* token, write the row, log it" would drift, and the half most likely to
|
|
* drift is the token.
|
|
*
|
|
* **The token is the whole authorization.** There is nothing behind
|
|
* /s/{token} — no session, no second factor — so its only defence is
|
|
* being unguessable. Str::random(32) is about 190 bits, which is more
|
|
* than a UUID's 122; anything minted here gets that and never a chosen
|
|
* value. A caller that wants a chosen token is a person typing one into a
|
|
* form, and that path stays in the controller where its minimum length
|
|
* can be argued about in a validation rule.
|
|
*
|
|
* Expiry and download caps are the caller's to decide and are passed in
|
|
* already resolved, because "the end of the 12th" depends on whose zone
|
|
* you are in and this class has no viewer.
|
|
*/
|
|
class CreateShareLink
|
|
{
|
|
public function __construct(
|
|
private readonly ActivityLogger $activity,
|
|
) {}
|
|
|
|
public function for(
|
|
File $file,
|
|
User $creator,
|
|
?CarbonInterface $expiresAt = null,
|
|
?int $maxDownloads = null,
|
|
?string $token = null,
|
|
): ShareLink {
|
|
$link = ShareLink::query()->create([
|
|
'shareable_type' => $file->getMorphClass(),
|
|
'shareable_id' => $file->id,
|
|
'token' => $token ?? Str::random(32),
|
|
'created_by' => $creator->id,
|
|
'expires_at' => $expiresAt,
|
|
'max_downloads' => $maxDownloads,
|
|
]);
|
|
|
|
$this->activity->log(Action::ShareLinkCreated, subject: $file);
|
|
|
|
return $link;
|
|
}
|
|
}
|