Skip to content

Commit 4361f69

Browse files
Fix HTTP headers when downloading files (#2031)
#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 <zack.galbreath@kitware.com>
1 parent 0460af2 commit 4361f69

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

app/Http/Controllers/BuildController.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
use Illuminate\Support\Facades\Gate;
2727
use Illuminate\Support\Facades\Storage;
2828
use Illuminate\View\View;
29-
use Symfony\Component\HttpFoundation\StreamedResponse;
29+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
3030
use PDO;
3131

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

849-
public function build_file(int $build_id, int $file_id) : StreamedResponse
849+
public function build_file(int $build_id, int $file_id): BinaryFileResponse
850850
{
851851
$this->setBuildById($build_id);
852852

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

864867
public function ajaxBuildNote(): View

0 commit comments

Comments
 (0)