asset handling cleanup

This commit is contained in:
robert dennis
2026-10-06 12:20:52 -04:00
parent e1a1483217
commit e284c381aa
7 changed files with 178 additions and 24 deletions
+4
View File
@@ -17,6 +17,10 @@ server {
}
location ^~ /assets/ {
location ~ /\. {
deny all;
}
try_files $uri =404;
}
+4
View File
@@ -49,6 +49,10 @@ server {
}
location ^~ /assets/ {
location ~ /\. {
deny all;
}
try_files $uri =404;
}
+18
View File
@@ -64,6 +64,24 @@ if [ ! -f /etc/nginx/http.d/panel.conf ]; then
fi
mv /etc/nginx/http.d/panel.conf.tmp /etc/nginx/http.d/panel.conf
fi
# Configs persisted by older images hand files under /assets/ to PHP-FPM; serve that tree as static files only.
if ! grep -qF 'location ^~ /assets/' /etc/nginx/http.d/panel.conf; then
ASSETS_BLOCK=$(cat <<'EOF'
location ^~ /assets/ {
location ~ /\. {
deny all;
}
try_files $uri =404;
}
EOF
) awk '!done && index($0, "location ~ \\.php$") { print ENVIRON["ASSETS_BLOCK"]; print ""; done = 1 } { print }' \
/etc/nginx/http.d/panel.conf > /etc/nginx/http.d/panel.conf.tmp
mv /etc/nginx/http.d/panel.conf.tmp /etc/nginx/http.d/panel.conf
grep -qF 'location ^~ /assets/' /etc/nginx/http.d/panel.conf \
|| echo 'warning: /etc/nginx/http.d/panel.conf has no PHP location; add the /assets/ block from .github/docker/default.conf by hand.' >&2
fi
rm -f /etc/nginx/http.d/default.conf
cp .github/docker/health.conf /etc/nginx/http.d/health.conf
@@ -9,7 +9,6 @@ use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Pterodactyl\Exceptions\Extensions\InvalidExtensionException;
use Pterodactyl\Support\JsonValueGuard;
use Symfony\Component\Finder\SplFileInfo;
use Throwable;
class ExtensionAssetPublisher
@@ -56,13 +55,11 @@ class ExtensionAssetPublisher
}
throw_if($reason = $this->unusableBuildReason($manifest), InvalidExtensionException::class, $reason);
$dist = $manifest->path('dist');
$files = File::allFiles($dist);
usort($files, fn (SplFileInfo $left, SplFileInfo $right): int => strcmp($left->getRelativePathname(), $right->getRelativePathname()));
$files = ExtensionDistFiles::list($manifest->path('dist'));
$hash = hash_init('sha256');
foreach ($files as $file) {
hash_update($hash, $file->getRelativePathname()."\0");
hash_update_file($hash, $file->getPathname());
foreach ($files as $relative => $path) {
hash_update($hash, $relative."\0");
hash_update_file($hash, $path);
}
$version = hash_final($hash);
@@ -72,7 +69,12 @@ class ExtensionAssetPublisher
$staged = $root.DIRECTORY_SEPARATOR.'.staging-'.Str::random(12);
File::ensureDirectoryExists($root);
try {
throw_unless(File::copyDirectory($dist, $staged), InvalidExtensionException::class, 'Unable to stage extension assets.');
// Copy exactly the files that were checked, never the directory itself.
foreach ($files as $relative => $path) {
File::ensureDirectoryExists(dirname($staged.DIRECTORY_SEPARATOR.$relative));
throw_unless(File::copy($path, $staged.DIRECTORY_SEPARATOR.$relative), InvalidExtensionException::class, 'Unable to stage extension assets.');
}
throw_unless(File::moveDirectory($staged, $target), InvalidExtensionException::class, 'Unable to publish extension assets.');
} finally {
File::deleteDirectory($staged);
@@ -128,7 +130,7 @@ class ExtensionAssetPublisher
return null;
}
return $this->missingBuildReason($manifest) ?? $this->serverScriptReason($manifest) ?? $this->stylesheets->conflictReason($manifest);
return $this->missingBuildReason($manifest) ?? $this->distFilesReason($manifest) ?? $this->stylesheets->conflictReason($manifest);
}
public function publishedPath(string $identifier): string
@@ -159,18 +161,13 @@ class ExtensionAssetPublisher
return ['url' => '/assets/extensions/'.$identifier.'/_development', 'version' => $version];
}
/** Published assets are web-served, so nothing in dist may be executable by PHP-FPM. */
private function serverScriptReason(ExtensionManifest $manifest): ?string
/** Published assets are web-served, so dist may only hold static browser assets. */
private function distFilesReason(ExtensionManifest $manifest): ?string
{
$dist = $manifest->path('dist');
if (! is_dir($dist)) {
return null;
}
foreach (File::allFiles($dist, true) as $file) {
if (preg_match('/\.(php\d?|phtml|phar|pht|phps)$/i', $file->getFilename()) || mb_strtolower($file->getFilename()) === '.user.ini') {
return "Extension \"{$manifest->id}\" ships a server-side script in dist ({$file->getRelativePathname()}); dist may only contain browser assets.";
}
try {
ExtensionDistFiles::list($manifest->path('dist'));
} catch (InvalidExtensionException $invalidExtensionException) {
return "Extension \"{$manifest->id}\" {$invalidExtensionException->getMessage()}";
}
return null;
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace Pterodactyl\Services\Extensions;
use Pterodactyl\Exceptions\Extensions\InvalidExtensionException;
use Symfony\Component\Finder\Finder;
/**
* The files of an extension build that may be published under the web root. dist is served
* as-is, so it is walked with none of Finder's default ignore rules (which skip `CVS/`, `.git/`
* and dotfiles) and anything a web server could treat as more than a static asset fails the build.
*/
final class ExtensionDistFiles
{
/** @var list<string> */
public const array EXTENSIONS = [
'js', 'mjs', 'css', 'map', 'json', 'wasm', 'txt',
'png', 'jpg', 'jpeg', 'gif', 'webp', 'avif', 'svg', 'ico',
'woff', 'woff2', 'ttf', 'otf', 'eot',
'mp3', 'ogg', 'wav', 'mp4', 'webm',
];
/**
* @return array<string, string> absolute paths keyed by their path relative to dist, sorted
*
* @throws InvalidExtensionException
*/
public static function list(string $dist): array
{
$files = [];
// Directories are listed too, so a linked directory is refused rather than silently skipped.
$finder = Finder::create()->in($dist)->ignoreVCS(false)->ignoreDotFiles(false);
foreach ($finder as $file) {
$relative = str_replace('\\', '/', $file->getRelativePathname());
$problem = match (true) {
$file->isLink() => 'a symbolic link',
preg_match('~(^|/)\.~', $relative) === 1 => 'a dotfile',
$file->isDir() => null,
! $file->isFile() => 'not a regular file',
! in_array(mb_strtolower($file->getExtension()), self::EXTENSIONS, true) => 'not a browser asset',
default => null,
};
throw_if($problem !== null, InvalidExtensionException::class, "ships dist/{$relative}, which is {$problem}; dist may only contain browser assets.");
if ($file->isFile()) {
$files[$relative] = $file->getPathname();
}
}
ksort($files, SORT_STRING);
return $files;
}
}
@@ -28,12 +28,12 @@ final class ExtensionStylesheetInspector
return null;
}
foreach (File::allFiles($manifest->path('dist')) as $file) {
if ($file->getExtension() !== 'css') {
foreach (ExtensionDistFiles::list($manifest->path('dist')) as $relative => $path) {
if (pathinfo($path, PATHINFO_EXTENSION) !== 'css') {
continue;
}
$foreign = $this->foreign($file->getContents(), $manifest->uiPrefix);
$foreign = $this->foreign(File::get($path), $manifest->uiPrefix);
$found = [...$foreign['utilities'], ...$foreign['theme']];
if ($found === []) {
continue;
@@ -50,7 +50,7 @@ final class ExtensionStylesheetInspector
? "but declares no Tailwind prefix - add \"prefix\": \"{$prefix}\" to \"ui\" in ".ExtensionManifest::FILENAME.', build'
: "without its \"{$prefix}\" prefix, which override the panel's or another extension's styles - build";
return "Extension \"{$manifest->id}\" ships {$what} in dist/{$file->getRelativePathname()} ({$listed}) {$problem} Tailwind with `prefix({$prefix})`, write its classes as `{$prefix}:flex` (see the SDK README, \"Styling\") and rebuild.";
return "Extension \"{$manifest->id}\" ships {$what} in dist/{$relative} ({$listed}) {$problem} Tailwind with `prefix({$prefix})`, write its classes as `{$prefix}:flex` (see the SDK README, \"Styling\") and rebuild.";
}
return null;
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Pterodactyl\Tests\Pest\Unit\Services\Extensions\ExtensionDistFilesTest;
use Illuminate\Support\Facades\File;
use Pterodactyl\Exceptions\Extensions\InvalidExtensionException;
use Pterodactyl\Services\Extensions\ExtensionAssetPublisher;
use Pterodactyl\Services\Extensions\ExtensionDistFiles;
use Pterodactyl\Services\Extensions\ExtensionManifestValidator;
use Pterodactyl\Services\Extensions\ExtensionStylesheetInspector;
use Pterodactyl\Tests\TestCase;
uses(TestCase::class);
beforeEach(function () {
$this->directory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'ptero-dist-'.uniqid();
File::ensureDirectoryExists($this->directory.'/dist');
File::put($this->directory.'/extension.json', json_encode(['id' => 'probe', 'name' => 'Probe', 'version' => '1.0.0', 'ui' => ['entry' => 'dist/client.js']], JSON_THROW_ON_ERROR));
File::put($this->directory.'/dist/client.js', 'export default {};');
});
afterEach(fn () => File::deleteDirectory($this->directory));
function put(string $root, string $relative, string $contents = ''): void
{
File::ensureDirectoryExists(dirname($root.'/'.$relative));
File::put($root.'/'.$relative, $contents);
}
it('lists every build file, including folders Finder would skip', function () {
put($this->directory, 'dist/chunks/lazy.js');
put($this->directory, 'dist/CVS/legacy.css');
expect(array_keys(ExtensionDistFiles::list($this->directory.'/dist')))->toBe(['CVS/legacy.css', 'chunks/lazy.js', 'client.js']);
});
it('rejects anything that is not a static browser asset', function (string $file, string $reported, string $problem) {
put($this->directory, 'dist/'.$file, '<?php echo 1;');
expect(fn () => ExtensionDistFiles::list($this->directory.'/dist'))->toThrow(InvalidExtensionException::class, "dist/{$reported}, which is {$problem}");
})->with([
'php in a VCS-named folder' => ['CVS/x.php', 'CVS/x.php', 'not a browser asset'],
'php in a dot folder' => ['.git/y.php', '.git', 'a dotfile'],
'apache override' => ['.htaccess', '.htaccess', 'a dotfile'],
'php-fpm override' => ['assets/.user.ini', 'assets/.user.ini', 'a dotfile'],
'upper-case php' => ['assets/X.PHP', 'assets/X.PHP', 'not a browser asset'],
'phtml' => ['x.phtml', 'x.phtml', 'not a browser asset'],
'html document' => ['index.html', 'index.html', 'not a browser asset'],
]);
it('rejects symbolic links', function () {
symlink($this->directory.'/extension.json', $this->directory.'/dist/manifest.json');
expect(fn () => ExtensionDistFiles::list($this->directory.'/dist'))->toThrow(InvalidExtensionException::class, 'dist/manifest.json, which is a symbolic link');
});
it('refuses to publish a build with a server-side file and publishes exactly the checked files', function () {
$publisher = new ExtensionAssetPublisher(new ExtensionStylesheetInspector);
config()->set('extensions.assets_directory', $this->directory.'/public');
put($this->directory, 'dist/CVS/x.php', '<?php echo 1;');
$manifest = (new ExtensionManifestValidator)->fromDirectory($this->directory);
expect($publisher->unusableBuildReason($manifest))->toBe('Extension "probe" ships dist/CVS/x.php, which is not a browser asset; dist may only contain browser assets.')
->and(fn () => $publisher->stage($manifest))->toThrow(InvalidExtensionException::class)
->and($this->directory.'/public')->not->toBeDirectory();
File::delete($this->directory.'/dist/CVS/x.php');
put($this->directory, 'dist/CVS/legacy.css', 'a{}');
$version = $publisher->stage($manifest);
expect(array_keys(ExtensionDistFiles::list($this->directory.'/public/probe/'.$version)))->toBe(['CVS/legacy.css', 'client.js']);
});