Skip to content

[AssetMapper] Check asset/vendor directory is writable #54724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\AssetMapper\ImportMap;

use Symfony\Component\AssetMapper\Exception\RuntimeException;

/**
* Manages the local storage of remote/vendor importmap packages.
*/
Expand Down Expand Up @@ -52,7 +54,9 @@ public function save(ImportMapEntry $entry, string $contents): void
$vendorPath = $this->getDownloadPath($entry->packageModuleSpecifier, $entry->type);

@mkdir(\dirname($vendorPath), 0777, true);
file_put_contents($vendorPath, $contents);
if (false === @file_put_contents($vendorPath, $contents)) {
throw new RuntimeException(error_get_last()['message'] ?? sprintf('Failed to write file "%s".', $vendorPath));
}
}

public function saveExtraFile(ImportMapEntry $entry, string $extraFilename, string $contents): void
Expand All @@ -64,7 +68,9 @@ public function saveExtraFile(ImportMapEntry $entry, string $extraFilename, stri
$vendorPath = $this->getExtraFileDownloadPath($entry, $extraFilename);

@mkdir(\dirname($vendorPath), 0777, true);
file_put_contents($vendorPath, $contents);
if (false === @file_put_contents($vendorPath, $contents)) {
throw new RuntimeException(error_get_last()['message'] ?? sprintf('Failed to write file "%s".', $vendorPath));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@ public static function getRequirePackageTests(): iterable
];

yield 'single_package_with_a_path' => [
'packages' => [new PackageRequireOptions('some/module', path: self::$writableRoot.'/assets/some_file.js')],
'expectedProviderPackageArgumentCount' => 0,
'resolvedPackages' => [],
'expectedImportMap' => [
'some/module' => [
// converted to relative path
'path' => './assets/some_file.js',
'packages' => [new PackageRequireOptions('some/module', path: self::$writableRoot.'/assets/some_file.js')],
'expectedProviderPackageArgumentCount' => 0,
'resolvedPackages' => [],
'expectedImportMap' => [
'some/module' => [
// converted to relative path
'path' => './assets/some_file.js',
],
],
],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RemotePackageStorageTest extends TestCase
protected function setUp(): void
{
$this->filesystem = new Filesystem();
if (!file_exists(self::$writableRoot)) {
if (!$this->filesystem->exists(self::$writableRoot)) {
$this->filesystem->mkdir(self::$writableRoot);
}
}
Expand All @@ -41,14 +41,30 @@ public function testGetStorageDir()
$this->assertSame(realpath(self::$writableRoot.'/assets/vendor'), realpath($storage->getStorageDir()));
}

public function testSaveThrowsWhenVendorDirectoryIsNotWritable()
{
$this->filesystem->mkdir($vendorDir = self::$writableRoot.'/assets/acme/vendor');
$this->filesystem->chmod($vendorDir, 0555);

$storage = new RemotePackageStorage($vendorDir);
$entry = ImportMapEntry::createRemote('foo', ImportMapType::JS, '/does/not/matter', '1.0.0', 'module_specifier', false);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('file_put_contents('.$vendorDir.'/module_specifier/module_specifier.index.js): Failed to open stream: No such file or directory');
$storage->save($entry, 'any content');

$this->filesystem->remove($vendorDir);
}

public function testIsDownloaded()
{
$storage = new RemotePackageStorage(self::$writableRoot.'/assets/vendor');
$entry = ImportMapEntry::createRemote('foo', ImportMapType::JS, '/does/not/matter', '1.0.0', 'module_specifier', false);
$this->assertFalse($storage->isDownloaded($entry));

$targetPath = self::$writableRoot.'/assets/vendor/module_specifier/module_specifier.index.js';
@mkdir(\dirname($targetPath), 0777, true);
file_put_contents($targetPath, 'any content');
$this->filesystem->mkdir(\dirname($targetPath));
$this->filesystem->dumpFile($targetPath, 'any content');
$this->assertTrue($storage->isDownloaded($entry));
}

Expand All @@ -57,9 +73,10 @@ public function testIsExtraFileDownloaded()
$storage = new RemotePackageStorage(self::$writableRoot.'/assets/vendor');
$entry = ImportMapEntry::createRemote('foo', ImportMapType::JS, '/does/not/matter', '1.0.0', 'module_specifier', false);
$this->assertFalse($storage->isExtraFileDownloaded($entry, '/path/to/extra.woff'));

$targetPath = self::$writableRoot.'/assets/vendor/module_specifier/path/to/extra.woff';
@mkdir(\dirname($targetPath), 0777, true);
file_put_contents($targetPath, 'any content');
$this->filesystem->mkdir(\dirname($targetPath));
$this->filesystem->dumpFile($targetPath, 'any content');
$this->assertTrue($storage->isExtraFileDownloaded($entry, '/path/to/extra.woff'));
}

Expand Down Expand Up @@ -92,7 +109,7 @@ public function testGetDownloadedPath(string $packageModuleSpecifier, ImportMapT
$this->assertSame($expectedPath, $storage->getDownloadPath($packageModuleSpecifier, $importMapType));
}

public static function getDownloadPathTests()
public static function getDownloadPathTests(): iterable
{
yield 'javascript bare package' => [
'packageModuleSpecifier' => 'foo',
Expand Down