Skip to content

Commit

Permalink
Fix HTTP headers when downloading files (#2031)
Browse files Browse the repository at this point in the history
#1796 introduced the usage of
Laravel's `Storage::download()` helper, which forced the browser to
download all files by default. This change was inconvenient for many
users who wanted to view simple text files.

This PR modifies the HTTP headers such that text files will be opened in
the browser, and binary files will be downloaded in all major browsers.
By setting the content-type to `text/plain`, the possibility of XSS
attacks is mitigated.

---------

Co-authored-by: Zack Galbreath <[email protected]>
  • Loading branch information
williamjallen and zackgalbreath authored Feb 14, 2024
1 parent 0460af2 commit 4361f69
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions app/Http/Controllers/BuildController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use PDO;

require_once 'include/repository.php';
Expand Down Expand Up @@ -846,7 +846,7 @@ public function files(int $build_id): View
->with('urls', $urls);
}

public function build_file(int $build_id, int $file_id) : StreamedResponse
public function build_file(int $build_id, int $file_id): BinaryFileResponse
{
$this->setBuildById($build_id);

Expand All @@ -858,7 +858,10 @@ public function build_file(int $build_id, int $file_id) : StreamedResponse
$uploadFile = new UploadFile();
$uploadFile->Id = $file_id;
$uploadFile->Fill();
return Storage::download("upload/{$uploadFile->Sha1Sum}", $uploadFile->Filename);
return response()->file(Storage::path("upload/{$uploadFile->Sha1Sum}"), [
"Content-Type" => "text/plain",
"Content-Disposition" => "inline/attachment; filename={$uploadFile->Filename}",
]);
}

public function ajaxBuildNote(): View
Expand Down

0 comments on commit 4361f69

Please sign in to comment.