Skip to content

Commit 28bb788

Browse files
authored
Merge pull request #30 from biigle/patch-5
Fix max size check for remote files
2 parents 8784f04 + d017605 commit 28bb788

2 files changed

Lines changed: 58 additions & 3 deletions

File tree

src/FileCache.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,21 @@ protected function existsRemote($file)
319319
}
320320

321321
$maxBytes = intval($this->config['max_file_size']);
322-
$size = intval($response->getHeaderLine('content-length'));
323322

324-
if ($maxBytes >= 0 && $size > $maxBytes) {
325-
throw new Exception("The file is too large with more than {$maxBytes} bytes.");
323+
if ($maxBytes >= 0) {
324+
$contentLength = $response->getHeaderLine('content-length');
325+
326+
$contentLength = filter_var($contentLength, FILTER_VALIDATE_INT, [
327+
'options' => ['min_range' => 0],
328+
]);
329+
330+
if ($contentLength === false) {
331+
throw new Exception("File size could not be determined (missing or invalid content-length header).");
332+
}
333+
334+
if ($contentLength > $maxBytes) {
335+
throw new Exception("The file is too large with more than {$maxBytes} bytes.");
336+
}
326337
}
327338

328339
return true;

tests/FileCacheTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,50 @@ public function testExistsRemoteMimeNotAllowed()
557557
$this->assertStringContainsString("type 'application/json' not allowed", $e->getMessage());
558558
}
559559
}
560+
561+
public function testExistsRemoteMissingContentLength()
562+
{
563+
// A response without content-length when max_file_size is set must fail,
564+
// because the size cannot be verified upfront.
565+
$mock = new MockHandler([new Response(200)]);
566+
$handlerStack = HandlerStack::create($mock);
567+
$client = new Client([
568+
'handler' => $handlerStack,
569+
'http_errors' => false,
570+
]);
571+
572+
$file = new GenericFile('https://example.com/file');
573+
$cache = new FileCache([
574+
'path' => $this->cachePath,
575+
'max_file_size' => 50,
576+
], client: $client);
577+
578+
try {
579+
$cache->exists($file);
580+
$this->fail('Expected an Exception to be thrown.');
581+
} catch (Exception $e) {
582+
$this->assertStringContainsString('content-length', $e->getMessage());
583+
}
584+
}
585+
586+
public function testExistsRemoteMissingContentLengthNoLimit()
587+
{
588+
// When max_file_size is -1 (no limit), a missing content-length is fine.
589+
$mock = new MockHandler([new Response(200)]);
590+
$handlerStack = HandlerStack::create($mock);
591+
$client = new Client([
592+
'handler' => $handlerStack,
593+
'http_errors' => false,
594+
]);
595+
596+
$file = new GenericFile('https://example.com/file');
597+
$cache = new FileCache([
598+
'path' => $this->cachePath,
599+
'max_file_size' => -1,
600+
], client: $client);
601+
602+
$this->assertTrue($cache->exists($file));
603+
}
560604
}
561605

562606
class FileCacheStub extends FileCache

0 commit comments

Comments
 (0)