Files
Deathgarden_Rebirth-Rewrite/dist/tests/Feature/LauncherReleaseTest.php
Shaggy84675 8bb72bb2b7 Deliver launcher updates from signed CI release zips
Replace the manually set launcher version with releases uploaded from the
CI zip (exes, launcher-manifest.json and launcher-manifest.json.sig).

- New "Launcher" admin tool (renamed from Launcher Message): upload a
  release, switch the current one, delete old ones, set the launcher message
- Uploads are validated: manifest rules, size + SHA-256 of every exe, and
  the ECDSA manifest signature when LAUNCHER_UPDATE_PUBLIC_KEYS is set
- The same zip, manifest or version cannot be published twice; only the
  newest 5 releases are kept and the current one is never removed
- Serve the current release to the launcher at /launcher/{file},
  byte-for-byte and uncached, with range support for resumed downloads
- /download-launcher serves the newest framework build; the legacy
  /api/v1/launcher-version returns the current release version
- Remove the launcher version from the Versioning tool
- Fix the file input button highlighting when hovering the whole input
2026-11-14 14:15:45 +01:00

302 lines
12 KiB
PHP

<?php
use App\Classes\Launcher\LauncherReleasePublisher;
use App\Models\Admin\LauncherRelease;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
/** Fixed test-only P-256 keys: openssl_pkey_new() needs an openssl.cnf that Windows PHP builds often lack. */
const LAUNCHER_TEST_KEY = <<<'PEM'
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgZEQJaLjE6rZhjDuh
Ofp1DdMCYgfO/20lyhV4VsaJjemhRANCAARUqO7T6ahPsfOhJZiYkDdhw8E0ME+q
v1JqIh30Md6t3k+0vvywa9WlOPu7MXiboWE5Y0ZB3LUTmlVz3ENjrAVJ
-----END PRIVATE KEY-----
PEM;
const LAUNCHER_OTHER_TEST_KEY = <<<'PEM'
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgS1Q08GC6k/0qZuPK
LbIl9bjWol2b78qCrt5Xt57F4DShRANCAATcjjNUKkLWioSCtv9cpR8N53AJYbnL
LVIldwGu5n+s0MISfVECwrbjGd6zm3PghXbDxdY6JLO369l7Ey8mDi4l
-----END PRIVATE KEY-----
PEM;
beforeEach(function () {
if (! extension_loaded('zip')) {
$this->markTestSkipped('The zip extension is not loaded.');
}
Storage::fake('launcher');
});
/**
* Builds a CI-style release zip and returns its path.
*
* @param array<string, mixed> $manifestOverrides
* @param array<string, string> $exes File name => contents.
*/
function makeLauncherReleaseZip(
array $manifestOverrides = [],
array $exes = ['DeathgardenLauncher-net11.0-windows.exe' => 'net11 exe bytes', 'DeathgardenLauncher-net9.0-windows.exe' => 'net9 exe bytes'],
string $signingKey = LAUNCHER_TEST_KEY,
string $folder = '',
): string {
$builds = collect($exes)->mapWithKeys(fn (string $contents, string $file) => [
Str::match('/-(net[\d.]+-windows)\.exe$/', $file) => ['file' => $file, 'size' => strlen($contents), 'sha256' => hash('sha256', $contents)],
])->all();
$manifest = json_encode(array_merge([
'product' => 'deathgarden-launcher',
'channel' => 'stable',
'version' => '1.200.1',
'notes' => 'Test update',
'published' => '2026-11-11T16:40:12Z',
'builds' => $builds,
], $manifestOverrides), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
openssl_sign($manifest, $derSignature, $signingKey, OPENSSL_ALGO_SHA256);
$path = tempnam(sys_get_temp_dir(), 'launcher-release').'.zip';
$zip = new ZipArchive;
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFromString($folder.'launcher-manifest.json', $manifest);
$zip->addFromString($folder.'launcher-manifest.json.sig', base64_encode(derSignatureToP1363($derSignature)));
foreach ($exes as $file => $contents) {
$zip->addFromString($folder.$file, $contents);
}
$zip->close();
return $path;
}
function derSignatureToP1363(string $der): string
{
$offset = 2;
$integers = [];
for ($i = 0; $i < 2; $i++) {
$length = ord($der[$offset + 1]);
$integers[] = str_pad(ltrim(substr($der, $offset + 2, $length), "\x00"), 32, "\x00", STR_PAD_LEFT);
$offset += 2 + $length;
}
return $integers[0].$integers[1];
}
function publicKeyBase64(string $privateKey): string
{
$pem = openssl_pkey_get_details(openssl_pkey_get_private($privateKey))['key'];
return preg_replace('/-----[^-]+-----|\s/', '', $pem);
}
function publishLauncherRelease(string $zipPath, array $trustedPublicKeys = []): LauncherRelease
{
return (new LauncherReleasePublisher($trustedPublicKeys))->publish($zipPath);
}
function expectReleaseRejected(string $zipPath, string $expectedMessage, array $trustedPublicKeys = []): void
{
try {
publishLauncherRelease($zipPath, $trustedPublicKeys);
} catch (ValidationException $exception) {
expect($exception->errors()['release'][0])->toContain($expectedMessage);
return;
}
test()->fail('The release was not rejected.');
}
test('a valid release zip is published and served byte for byte to the launcher', function () {
$zipPath = makeLauncherReleaseZip();
$release = publishLauncherRelease($zipPath);
expect($release->version)->toBe('1.200.1')
->and(LauncherRelease::current()->version)->toBe('1.200.1');
$zip = new ZipArchive;
$zip->open($zipPath);
$manifestResponse = $this->get('/launcher/launcher-manifest.json');
$manifestResponse->assertOk()->assertHeader('Content-Type', 'application/json');
expect($manifestResponse->streamedContent())->toBe($zip->getFromName('launcher-manifest.json'))
->and($this->get('/launcher/launcher-manifest.json.sig')->streamedContent())->toBe($zip->getFromName('launcher-manifest.json.sig'));
$exeResponse = $this->get('/launcher/DeathgardenLauncher-net11.0-windows.exe');
$exeResponse->assertOk();
expect($exeResponse->streamedContent())->toBe('net11 exe bytes')
->and($exeResponse->headers->get('Cache-Control'))->toContain('no-cache');
});
test('exe downloads can be resumed with a range request', function () {
publishLauncherRelease(makeLauncherReleaseZip());
$response = $this->get('/launcher/DeathgardenLauncher-net11.0-windows.exe', ['Range' => 'bytes=6-']);
$response->assertStatus(206)->assertHeader('Content-Range', 'bytes 6-14/15');
expect($response->streamedContent())->toBe('exe bytes');
});
test('only files listed in the current manifest are served', function () {
publishLauncherRelease(makeLauncherReleaseZip());
$this->get('/launcher/current')->assertNotFound();
$this->get('/launcher/other.exe')->assertNotFound();
$this->get('/launcher/..%2Fcurrent')->assertNotFound();
});
test('launcher files are not found before any release is published', function () {
$this->get('/launcher/launcher-manifest.json')->assertNotFound();
$this->get('/download-launcher')->assertNotFound();
});
test('a zip with the release inside a single top level folder is accepted', function () {
expect(publishLauncherRelease(makeLauncherReleaseZip(folder: 'launcher-1.200.1/'))->version)->toBe('1.200.1');
});
test('exes named differently from the manifest are matched by size and hash and stored under the manifest name', function () {
$zipPath = makeLauncherReleaseZip();
$zip = new ZipArchive;
$zip->open($zipPath);
$zip->renameName('DeathgardenLauncher-net11.0-windows.exe', 'DeathgardenLauncher_1.200.1.0-net11.0.exe');
$zip->close();
publishLauncherRelease($zipPath);
expect($this->get('/launcher/DeathgardenLauncher-net11.0-windows.exe')->streamedContent())->toBe('net11 exe bytes');
});
test('a release whose exe does not match the manifest is rejected and the current release is kept', function () {
publishLauncherRelease(makeLauncherReleaseZip(['version' => '1.100.0']));
$zipPath = makeLauncherReleaseZip(['version' => '1.200.0']);
$zip = new ZipArchive;
$zip->open($zipPath);
$zip->addFromString('DeathgardenLauncher-net9.0-windows.exe', 'NET9 EXE BYTES');
$zip->close();
expectReleaseRejected($zipPath, 'does not match the size or SHA-256');
expect(LauncherRelease::current()->version)->toBe('1.100.0')
->and(LauncherRelease::all()->pluck('version')->all())->toBe(['1.100.0']);
});
test('invalid manifests are rejected', function (array $manifestOverrides, string $expectedMessage) {
expectReleaseRejected(makeLauncherReleaseZip($manifestOverrides), $expectedMessage);
expect(LauncherRelease::current())->toBeNull();
})->with([
'wrong product' => [['product' => 'something-else'], 'product must be'],
'bad version' => [['version' => '../1.0'], 'version must look like'],
'no builds' => [['builds' => []], 'lists no builds'],
'path in file name' => [['builds' => ['net11.0-windows' => ['file' => '../evil.exe', 'size' => 1, 'sha256' => str_repeat('a', 64)]]], 'invalid file name'],
]);
test('the same zip cannot be published twice', function () {
$zipPath = makeLauncherReleaseZip();
publishLauncherRelease($zipPath);
expectReleaseRejected($zipPath, 'already been published as version 1.200.1');
});
test('a version cannot be published twice even with different files', function () {
publishLauncherRelease(makeLauncherReleaseZip(['notes' => 'First build']));
expectReleaseRejected(makeLauncherReleaseZip(['notes' => 'Rebuilt']), 'Version 1.200.1 has already been published');
});
test('the manifest signature is verified when trusted keys are configured', function () {
$trustedKeys = [publicKeyBase64(LAUNCHER_TEST_KEY)];
expectReleaseRejected(makeLauncherReleaseZip(signingKey: LAUNCHER_OTHER_TEST_KEY), 'does not match any trusted key', $trustedKeys);
$publisher = new LauncherReleasePublisher($trustedKeys);
$publisher->publish(makeLauncherReleaseZip());
expect($publisher->warnings)->toBe([]);
});
test('trusted keys are read from config when the publisher is resolved from the container', function () {
config(['services.launcher.update_public_keys' => [publicKeyBase64(LAUNCHER_TEST_KEY)]]);
$publisher = app(LauncherReleasePublisher::class);
$publisher->publish(makeLauncherReleaseZip());
expect($publisher->warnings)->toBe([]);
});
test('a non stable channel and an older version only produce warnings', function () {
publishLauncherRelease(makeLauncherReleaseZip(['version' => '1.200.1']));
$publisher = new LauncherReleasePublisher;
$publisher->publish(makeLauncherReleaseZip(['version' => '1.95.0', 'channel' => 'test']));
expect(LauncherRelease::current()->version)->toBe('1.95.0')
->and(implode("\n", $publisher->warnings))
->toContain('channel is "test"')
->toContain('older than 1.200.1')
->toContain('LAUNCHER_UPDATE_PUBLIC_KEYS');
});
test('an older release can be made current again', function () {
publishLauncherRelease(makeLauncherReleaseZip(['version' => '1.100.0']));
publishLauncherRelease(makeLauncherReleaseZip(['version' => '1.200.0']));
expect(LauncherRelease::all()->pluck('version')->all())->toBe(['1.200.0', '1.100.0']);
LauncherRelease::findOrFail('1.100.0')->makeCurrent();
expect(LauncherRelease::current()->version)->toBe('1.100.0');
});
test('the download page serves the newest framework build with a versioned name', function () {
publishLauncherRelease(makeLauncherReleaseZip());
$response = $this->get('/download-launcher');
$response->assertOk()->assertDownload('Deathgarden Launcher_1.200.1.exe');
expect($response->streamedContent())->toBe('net11 exe bytes');
});
test('the legacy launcher version endpoint returns the current release version', function () {
publishLauncherRelease(makeLauncherReleaseZip());
expect($this->get('/api/v1/launcher-version')->getContent())->toBe('"1.200.1"');
});
test('a release can be deleted but the current release cannot', function () {
publishLauncherRelease(makeLauncherReleaseZip(['version' => '1.100.0']));
publishLauncherRelease(makeLauncherReleaseZip(['version' => '1.200.0']));
expect(LauncherRelease::findOrFail('1.200.0')->delete())->toBeFalse()
->and(LauncherRelease::findOrFail('1.100.0')->delete())->toBeTrue()
->and(LauncherRelease::all()->pluck('version')->all())->toBe(['1.200.0'])
->and(Storage::disk('launcher')->exists('releases/1.100.0'))->toBeFalse();
});
test('only the newest releases are kept and the oldest are removed on publish', function () {
foreach (['1.1.0', '1.2.0', '1.3.0', '1.4.0', '1.5.0'] as $version) {
publishLauncherRelease(makeLauncherReleaseZip(['version' => $version]));
}
$publisher = new LauncherReleasePublisher;
$publisher->publish(makeLauncherReleaseZip(['version' => '1.6.0']));
expect($publisher->prunedVersions)->toBe(['1.1.0'])
->and(LauncherRelease::all()->pluck('version')->all())->toBe(['1.6.0', '1.5.0', '1.4.0', '1.3.0', '1.2.0']);
});
test('pruning never removes the current release, even when it is the oldest', function () {
foreach (['1.2.0', '1.3.0', '1.4.0', '1.5.0', '1.6.0'] as $version) {
publishLauncherRelease(makeLauncherReleaseZip(['version' => $version]));
}
$publisher = new LauncherReleasePublisher;
$publisher->publish(makeLauncherReleaseZip(['version' => '1.1.0']));
expect(LauncherRelease::current()->version)->toBe('1.1.0')
->and($publisher->prunedVersions)->toBe(['1.2.0'])
->and(LauncherRelease::all())->toHaveCount(LauncherRelease::MAX_RELEASES);
});