verifiedUrl($link); $path = storage_path('app'.DIRECTORY_SEPARATOR.'extensions-tmp'.DIRECTORY_SEPARATOR.Str::random(12).'.pteroext'); File::ensureDirectoryExists(dirname($path)); try { $response = Http::withHeaders([self::INSTALL_HEADER => config()->string('extensions.panel_version')]) ->connectTimeout(10) ->timeout(120) ->sink($path) ->withOptions(['on_headers' => $this->assertWithinSizeLimit(...)]) ->get($url); throw_unless($response->successful(), InvalidExtensionException::class, $this->failureMessage($path, $response->status())); throw_if(File::size($path) > self::MAX_PACKAGE_BYTES, InvalidExtensionException::class, 'The extension package is larger than the panel accepts.'); } catch (Throwable $throwable) { rescue(fn () => File::delete($path)); // Guzzle wraps whatever the on_headers check throws. $refusal = $throwable instanceof InvalidExtensionException ? $throwable : ($throwable->getPrevious() ?? $throwable); throw_if($refusal instanceof InvalidExtensionException, $refusal); throw_unless($throwable instanceof ConnectionException || $throwable instanceof TransferException, $throwable); throw new InvalidExtensionException('Could not download the extension. Please try again.', $throwable->getCode(), previous: $throwable); } return $path; } /** * The address to fetch a package from, taken from the URL's signed token. * * @throws InvalidExtensionException */ public function verifiedUrl(string $link): string { throw_unless(config('extensions.signed_urls.enabled'), InvalidExtensionException::class, 'Installing extensions from a URL is disabled on this panel.'); throw_unless(extension_loaded('sodium'), InvalidExtensionException::class, 'The PHP sodium extension is required to verify signed install URLs.'); $jwt = Str::afterLast(Str::before(Str::before(mb_trim($link), '?'), '#'), '/'); $token = null; $signed = false; if ($jwt !== '') { try { $token = (new Parser(new JoseEncoder))->parse($jwt); $publicKey = config()->string('extensions.signed_urls.public_key'); throw_if($publicKey === '', UnexpectedValueException::class, 'Signed install URLs need a public key.'); $key = InMemory::base64Encoded($publicKey); $signed = $token instanceof UnencryptedToken && (new Validator)->validate($token, new SignedWith(new Eddsa, $key)); } catch (Throwable) { $signed = false; } } // Nothing in the token is trusted, or even reported back, before its signature verifies. throw_unless( $signed && $token instanceof UnencryptedToken, InvalidExtensionException::class, 'This is not a signed install URL. Extensions can only be installed from a URL that Pterodactyl has signed.', ); $claims = $token->claims(); $issuer = config()->string('extensions.signed_urls.issuer'); $audience = config()->string('extensions.signed_urls.audience'); throw_if($issuer === '' || $audience === '', UnexpectedValueException::class, 'Signed install URLs need an issuer and an audience.'); throw_unless( $token->hasBeenIssuedBy($issuer) && $token->isPermittedFor($audience), InvalidExtensionException::class, 'This signed URL is not an extension install URL.', ); // A token without an expiry never reports itself as expired, so one is required. throw_if( ! $claims->has('exp') || $token->isExpired(CarbonImmutable::now()), InvalidExtensionException::class, 'This install URL has expired. Copy a new install command.', ); $base = rescue(fn (): string => Arr::string($claims->all(), 'url'), report: false); throw_unless($base !== null && $this->isUrl($base), InvalidExtensionException::class, 'This install URL does not say where to download the extension from.'); return $base.$jwt; } /** Refuse a package that declares itself too large before any of it is written to disk. */ private function assertWithinSizeLimit(ResponseInterface $response): void { $length = filter_var($response->getHeaderLine('Content-Length'), FILTER_VALIDATE_INT); throw_if( $length !== false && $length > self::MAX_PACKAGE_BYTES, InvalidExtensionException::class, 'The extension package is larger than the panel accepts.', ); } /** The server explains refusals in a JSON body, which was written to the sink file. */ private function failureMessage(string $path, int $status): string { $message = rescue(fn (): string => Arr::string(File::json($path), 'error.message'), report: false); return $message !== null ? "The download was refused: {$message}" : "The download was refused (HTTP {$status})."; } }