Skip to content

Commit 7bf11e1

Browse files
committed
feat: Validate file extensions in renaming logic
1 parent ad36f62 commit 7bf11e1

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/DLoad.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,16 @@ private function shouldBeExtracted(\SplFileInfo $source, array $mapping, Path $p
307307
{
308308
foreach ($mapping as $conf) {
309309
if (\preg_match($conf->pattern, $source->getFilename())) {
310+
$extension = $source->getExtension();
311+
// Validate that the "extension" looks like a real file extension
312+
// (short, alphanumeric only — e.g. "exe", "phar", "gz")
313+
// and not a version/platform artifact like "0-linux-amd64"
314+
$hasRealExtension = $extension !== '' && \preg_match('/^(?=.*[a-zA-Z])[a-zA-Z0-9]{1,10}$/', $extension) === 1;
315+
310316
$newName = match (true) {
311317
$conf->rename === null => $source->getFilename(),
312-
$source->getExtension() === '' => $conf->rename,
313-
default => $conf->rename . '.' . $source->getExtension(),
318+
!$hasRealExtension => $conf->rename,
319+
default => $conf->rename . '.' . $extension,
314320
};
315321

316322
return [new \SplFileInfo((string) $path->join($newName)), $conf];

0 commit comments

Comments
 (0)