Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.0.1

- Fixed guides breaking after the first scheduled sync on a standard install. The panel's scheduler runs from root's crontab, so the synced copy was owned by root and the web server could not read it ("Permission denied" on Sync now). Synced files now belong to the same user as the panel's storage folder. After updating, the next scheduled sync fixes the folder on its own.

## 1.0.0

- First release: a Guides tab on every server with help articles for its game, grouped into general guides and the ones for the server's mod framework, with the server's name, address, IP, port and domain filled in. Guides come from the community repo (Rustix-Development/pterodactyl-egg-guides) and any GitHub repos you add above it, public or private, synced on a schedule into the panel's own copy. An admin page lists the repos, sync status and skipped files, and lets you pin a guide folder to any egg or turn guides off for it. Subusers need the new **View guides** permission.
2 changes: 1 addition & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "./node_modules/@pterodactyl/sdk/manifest.schema.json",
"id": "guides",
"name": "Server Guides",
"version": "1.0.0",
"version": "1.0.1",
"description": "A Guides tab on every server with help articles for its game and mod framework, synced from GitHub repos the community writes together.",
"author": "RustixDev",
"icon": "book-open",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "guides",
"private": true,
"version": "1.0.0",
"version": "1.0.1",
"type": "module",
"scripts": {
"build": "vite build",
Expand Down
36 changes: 36 additions & 0 deletions src/Services/GuideSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function syncRepo(Repo $repo): string
{
$root = self::root();
File::ensureDirectoryExists($root, 0750);
self::matchStorageOwner($root);
$archive = $root.DIRECTORY_SEPARATOR.'download-'.Str::lower(Str::random(16)).'.zip';
$name = Str::lower(Str::random(20));
$target = $root.DIRECTORY_SEPARATOR.$name;
Expand All @@ -100,6 +101,7 @@ public function syncRepo(Repo $repo): string
$this->source->download($repo->repo, $repo->branch, $repo->token, $archive);
$revision = ArchiveExtractor::revision($archive);
$problems = $this->extractor->extract($archive, $target);
self::matchStorageOwner($target);
$parsed = $this->parser->parse($target);
$problems = array_slice([...$problems, ...$parsed['problems']], 0, self::MAX_PROBLEMS);

Expand Down Expand Up @@ -149,6 +151,40 @@ public function syncRepo(Repo $repo): string
}
}

/**
* The panel's scheduler usually runs from root's crontab, while the web server
* reads the synced copy as its own user. Files written by root would then be
* unreadable to the panel, so hand them to whoever owns storage/app.
*/
public static function matchStorageOwner(string $path): void
{
if (! function_exists('posix_geteuid') || posix_geteuid() !== 0 || ! file_exists($path)) {
return;
}
$owner = @fileowner(storage_path('app'));
$group = @filegroup(storage_path('app'));
if ($owner === false || $group === false || $owner === 0) {
return;
}

$paths = [$path];
if (is_dir($path)) {
$items = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST,
);
foreach ($items as $item) {
if (! $item->isLink()) {
$paths[] = $item->getPathname();
}
}
}
foreach ($paths as $item) {
@chown($item, $owner);
@chgrp($item, $group);
}
}

/** Copies no repo points at any more: removed repos, or a sync that died halfway. */
private function removeStaleCopies(): void
{
Expand Down
13 changes: 13 additions & 0 deletions tests/guides.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,19 @@ function throws(callable $fn, ?string $contains = null): bool
check('sync: only one at a time', throws(fn () => $sync->syncAll(), 'already running'));
$lock->release();

// Run as root (cron) with storage owned by the web user: synced files must end up the web user's.
if (function_exists('posix_geteuid') && posix_geteuid() === 0 && ($web = posix_getpwnam('www-data')) !== false) {
chown($tmp.'/storage/app', $web['uid']);
$sync->syncRepo($host->fresh()->forceFill(['repo' => 'myhost/host-guides']));
$copy = (string) GuideSync::directory($host->fresh());
$owners = [fileowner(GuideSync::root()), fileowner($copy)];
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($copy, FilesystemIterator::SKIP_DOTS)) as $item) {
$owners[] = $item->getOwner();
}
check('sync as root: files belong to the storage owner', array_unique($owners) === [$web['uid']], json_encode(array_unique($owners)));
chown($tmp.'/storage/app', 0);
}

EggRule::query()->create(['egg_id' => 1, 'mode' => 'off']);
check('egg rules are read', GuideCatalog::ruleFor(1) === ['mode' => 'off', 'folder' => null] && GuideCatalog::ruleFor(2) === null);

Expand Down
Loading