mirror of
https://github.com/projectsend/projectsend.git
synced 2026-09-16 16:45:07 +00:00
88c182cf3b
v1 could preview four kinds of file in a modal — images, video, audio and PDF. v2 previewed only images, and not by decision: preview shipped as part of the image *thumbnail* work (1c68aa1), so "previewable" quietly became a synonym for "GD can decode it". FileThumbnailController::preview() gated on ThumbnailGenerator::SUPPORTED_MIME_TYPES, the frontend mirrored the same four types, and the dialog was a hardcoded <img>. Rather than widen that list — it drives pathFor(), extensionFor(), generate() and FileDiskCleanup, and a video reaching getimagesize() is a 500 — this separates the two questions. PreviewKind now answers "may these bytes be served inline, and what element renders them?", while ThumbnailGenerator keeps answering the narrower "can this app decode it itself?", which is what renditions, the cache and the watermark hook actually depend on. Image delegates to it so the two cannot drift. The allowlist stays a security boundary: mime_type is sniffed from the bytes, so text/html and image/svg+xml remain excluded, and PreviewKind is deliberately narrower than "formats a browser might cope with" — no quicktime, avi or matroska, because an embedded player for those shows a black rectangle. Those still download exactly as before. docs/security-audit-2026-08-05.md finding 1 recorded that adding application/pdf "should be a conscious decision". This is that decision, and three things were measured rather than assumed: - An <iframe sandbox> cannot be used. Chrome refuses to run its PDF viewer in a sandboxed frame at all (ERR_BLOCKED_BY_CLIENT, with or without allow-same-origin) — the attribute removes the feature, it does not harden it. - nginx's `Content-Security-Policy: sandbox; default-src 'none'` on /protected-files/ does work (a <video> frame lands in an opaque origin), but Chrome exempts its PDF viewer from it, so it is not what protects the PDF case. - What does is the allowlist plus the browser's own PDF sandbox, where PDF JavaScript has no DOM and no cookies. Range requests were verified end to end: 206 with a correct Content-Range, a byte-perfect file reassembled from three ranges, and a real browser seeking to 10s of a 20s clip. nginx drops the upstream Content-Length on the X-Accel path, so there is no collision. Two settings, both defaulting on so no installation loses what it has: clients_can_preview_files and public_listing_preview_enabled. Staff are never gated. The anonymous side needed a route of its own — there was no public preview endpoint — with its own throttle bucket, since a bare throttle: shares one counter across that whole block. A preview now logs at most one FilePreviewed per viewer per file per five minutes: a <video> turns one deliberate act into a long tail of Range requests, and a row each would bury the log. Also fixes a layout bug the tests could never catch. A portal file row was flex justify-between with three children — name, comment trigger, download — so the middle one settled wherever the name happened to end and the comment icon sat at a different place on every row. The name block now takes the slack and every action lives in one trailing group, with the comment trigger in a fixed-width slot so the icons form a column. And because half the previewable files have no thumbnail to click — a PDF, an mp3 and an mp4 all render as a generic icon — every row gains an explicit PreviewAction beside DownloadAction, matching whatever style that theme gives its download control.
133 lines
5.3 KiB
PHP
133 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Audit\Action;
|
|
use App\Modules\Audit\ActivityLog;
|
|
use App\Modules\Files\Models\File;
|
|
use App\Modules\Platform\Settings\Setting;
|
|
use App\Modules\Platform\Settings\Settings;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* Preview beyond images: the types a browser plays natively, which this
|
|
* app never decodes and therefore never renders, caches or watermarks.
|
|
* The allowlist that admits them is PreviewKind, deliberately separate
|
|
* from the rendition allowlist asserted in FileThumbnailsTest.
|
|
*/
|
|
beforeEach(function () {
|
|
Storage::fake('files');
|
|
$this->admin = User::factory()->create();
|
|
|
|
// Settings survive RefreshDatabase's rollback through their cache, so
|
|
// a test that means "the default" has to say so rather than assume it.
|
|
app(Settings::class)->set(Setting::ClientsCanPreviewFiles, true);
|
|
});
|
|
|
|
function uploadMediaFile(User $as, string $name, string $mimeType): File
|
|
{
|
|
test()->actingAs($as)->post('/files', [
|
|
'file' => UploadedFile::fake()->create($name, 16, $mimeType),
|
|
'name' => '',
|
|
'description' => '',
|
|
]);
|
|
|
|
return File::query()->latest('id')->firstOrFail();
|
|
}
|
|
|
|
test('a media file is served inline, as itself, from the local disk', function (string $name, string $mimeType) {
|
|
$file = uploadMediaFile($this->admin, $name, $mimeType);
|
|
|
|
$this->actingAs($this->admin)->get("/files/{$file->id}/preview")
|
|
->assertOk()
|
|
->assertHeader('Content-Type', $mimeType)
|
|
->assertHeader('Content-Disposition', 'inline; filename="'.$name.'"')
|
|
->assertHeader('X-Accel-Redirect', '/protected-files/'.$file->path)
|
|
->assertHeader('Content-Length', (string) $file->size);
|
|
})->with([
|
|
'mp4 video' => ['clip.mp4', 'video/mp4'],
|
|
'webm video' => ['clip.webm', 'video/webm'],
|
|
'mp3 audio' => ['song.mp3', 'audio/mpeg'],
|
|
'wav audio' => ['song.wav', 'audio/x-wav'],
|
|
'flac audio' => ['song.flac', 'audio/flac'],
|
|
'pdf' => ['contract.pdf', 'application/pdf'],
|
|
]);
|
|
|
|
// A format the browser would only show a black rectangle for is not
|
|
// previewable, however happily it uploads and downloads.
|
|
test('a media container no browser decodes is not previewable', function (string $name, string $mimeType) {
|
|
$file = uploadMediaFile($this->admin, $name, $mimeType);
|
|
|
|
$this->actingAs($this->admin)->get("/files/{$file->id}/preview")->assertNotFound();
|
|
})->with([
|
|
'quicktime' => ['clip.mov', 'video/quicktime'],
|
|
'avi' => ['clip.avi', 'video/x-msvideo'],
|
|
'matroska' => ['clip.mkv', 'video/x-matroska'],
|
|
]);
|
|
|
|
// One deliberate act, however many Range requests the browser turns it
|
|
// into. Without the guard, watching one video buries the activity log.
|
|
test('replaying a preview logs it once, not once per request', function () {
|
|
$file = uploadMediaFile($this->admin, 'clip.mp4', 'video/mp4');
|
|
|
|
foreach (range(1, 5) as $ignored) {
|
|
$this->actingAs($this->admin)->get("/files/{$file->id}/preview")->assertOk();
|
|
}
|
|
|
|
expect(ActivityLog::query()->where('action', Action::FilePreviewed)->where('subject_id', $file->id)->count())->toBe(1);
|
|
});
|
|
|
|
// Keyed by viewer, so one person's playback cannot swallow the record of
|
|
// somebody else looking at the same file.
|
|
test('two viewers of the same file are each logged', function () {
|
|
$client = User::factory()->client()->create();
|
|
$file = uploadMediaFile($this->admin, 'clip.mp4', 'video/mp4');
|
|
shareFileWith($file, $client);
|
|
|
|
$this->actingAs($this->admin)->get("/files/{$file->id}/preview")->assertOk();
|
|
$this->actingAs($client)->get("/files/{$file->id}/preview")->assertOk();
|
|
|
|
$actors = ActivityLog::query()->where('action', Action::FilePreviewed)->where('subject_id', $file->id)
|
|
->pluck('actor_id')->sort()->values()->all();
|
|
|
|
expect($actors)->toBe(collect([$this->admin->id, $client->id])->sort()->values()->all());
|
|
});
|
|
|
|
test('with client preview switched off a client cannot preview, and staff still can', function () {
|
|
app(Settings::class)->set(Setting::ClientsCanPreviewFiles, false);
|
|
|
|
$client = User::factory()->client()->create();
|
|
$file = uploadMediaFile($this->admin, 'contract.pdf', 'application/pdf');
|
|
shareFileWith($file, $client);
|
|
|
|
$this->actingAs($client)->get("/files/{$file->id}/preview")->assertNotFound();
|
|
$this->actingAs($this->admin)->get("/files/{$file->id}/preview")->assertOk();
|
|
});
|
|
|
|
// The switch is about looking, never about having: a client who is
|
|
// refused a preview downloads exactly as before.
|
|
test('a client refused a preview can still download the file', function () {
|
|
app(Settings::class)->set(Setting::ClientsCanPreviewFiles, false);
|
|
|
|
$client = User::factory()->client()->create();
|
|
$file = uploadMediaFile($this->admin, 'contract.pdf', 'application/pdf');
|
|
shareFileWith($file, $client);
|
|
|
|
$this->actingAs($client)->get("/files/{$file->id}/download")->assertOk();
|
|
});
|
|
|
|
test('the portal tells its theme whether preview is offered', function () {
|
|
app(Settings::class)->set(Setting::Theme, 'default');
|
|
$client = User::factory()->client()->create();
|
|
|
|
$this->actingAs($client)->get('/my-files')
|
|
->assertInertia(fn ($page) => $page->where('preview_enabled', true));
|
|
|
|
app(Settings::class)->set(Setting::ClientsCanPreviewFiles, false);
|
|
|
|
$this->actingAs($client)->get('/my-files')
|
|
->assertInertia(fn ($page) => $page->where('preview_enabled', false));
|
|
});
|