Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
🔒️ Fix a security risk (Coverage)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thecode764 committed Jun 8, 2024
1 parent 341f2fb commit 2e36cd4
Showing 1 changed file with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,22 @@ public function __construct(
public function __invoke(Request $request, Response $response): ResponseInterface
{
$path = $request->getQueryParams()['info'];

$file = new SplFileInfo(
(string) realpath($this->config->get('base_path') . '/' . $path)
(string)realpath($this->config->get('base_path'). '/'. $path)
);

if (! $file->isFile()) {
if (!$file->isFile()) {
return $response->withStatus(404, $this->translator->trans('error.file_not_found'));
}

if ($file->getSize() >= (int) $this->config->get('max_hash_size')) {
if ($file->getSize() >= (int)$this->config->get('max_hash_size')) {
return $response->withStatus(500, $this->translator->trans('error.file_size_exceeded'));
}

$response->getBody()->write($this->cache->get(
sprintf('file-info-%s', sha1((string) $file->getRealPath())),
sprintf('file-info-%s', sha256((string)$file->getRealPath())), // Changed to sha256
function () use ($file): string {
return (string) json_encode(['hashes' => $this->calculateHashes($file)]);
return (string)json_encode(['hashes' => $this->calculateHashes($file)]);
}
));

Expand All @@ -50,9 +49,8 @@ function () use ($file): string {
protected function calculateHashes(SplFileInfo $file): array
{
return [
'md5' => hash_file('md5', (string) $file->getRealPath()),
'sha1' => hash_file('sha1', (string) $file->getRealPath()),
'sha256' => hash_file('sha256', (string) $file->getRealPath()),
'md5' => hash_file('md5', (string)$file->getRealPath()),
'sha256' => hash_file('sha256', (string)$file->getRealPath()), // Ensure this is the only sha variant used
];
}
}

0 comments on commit 2e36cd4

Please sign in to comment.