mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
test(sites): assert start commands never leave the server
The commands that boot a framework are an internal contract between Appwrite and the runtime image. Nothing in the frameworks or templates response models declares them, so they are already stripped, but that guarantee lives only in the absence of a rule and would break the moment someone adds one. Walk both payloads and fail on a startCommand key, on any configured start command, and on any string reaching for a runtime helper script. Also pin that a site created without a start command keeps an empty one rather than being seeded with the framework default.
This commit is contained in:
1 parent
2abca834a1
commit
55e76fe537
6 files changed
+248
-1
No files matched your search
@@ -240,11 +240,15 @@ return [
|
||||
'bundleCommand' => 'bash /usr/local/server/helpers/tanstack-start/bundle.sh',
|
||||
'envCommand' => 'source /usr/local/server/helpers/tanstack-start/env.sh',
|
||||
'adapters' => [
|
||||
// TanStack Start >= 1.133 builds vite-native, emitting the server
|
||||
// entry as dist/server/server.js next to dist/client. Builds that
|
||||
// opt into the nitro plugin emit .output instead, which the runtime
|
||||
// still detects, but they have to override the directory here.
|
||||
'ssr' => [
|
||||
'key' => 'ssr',
|
||||
'buildCommand' => 'npm run build',
|
||||
'installCommand' => 'npm install',
|
||||
'outputDirectory' => './.output',
|
||||
'outputDirectory' => './dist',
|
||||
'startCommand' => 'bash helpers/tanstack-start/server.sh',
|
||||
],
|
||||
'static' => [
|
||||
|
||||
@@ -6,6 +6,7 @@ use Appwrite\Tests\Async;
|
||||
use Appwrite\Tests\Async\Exceptions\Critical;
|
||||
use CURLFile;
|
||||
use Tests\E2E\Client;
|
||||
use Utopia\Config\Config;
|
||||
use Utopia\Console;
|
||||
use Utopia\Database\Helpers\ID;
|
||||
use Utopia\Database\Query;
|
||||
@@ -18,6 +19,9 @@ trait SitesBase
|
||||
protected string $stdout = '';
|
||||
protected string $stderr = '';
|
||||
|
||||
/** @var list<string>|null */
|
||||
protected ?array $internalStartCommands = null;
|
||||
|
||||
protected function setupSite(mixed $params): string
|
||||
{
|
||||
$site = $this->client->call(Client::METHOD_POST, '/sites', array_merge([
|
||||
@@ -496,4 +500,58 @@ trait SitesBase
|
||||
|
||||
return $specification['slug'];
|
||||
}
|
||||
|
||||
/**
|
||||
* The commands that boot a framework are an internal contract between Appwrite and the
|
||||
* runtime image, and they change without notice. Leaking one lets a client copy it into
|
||||
* their own site, so it must not reach a response body at all - not as a value a console
|
||||
* could render, and not as a default a console could offer as a placeholder.
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
protected function getInternalStartCommands(): array
|
||||
{
|
||||
if ($this->internalStartCommands !== null) {
|
||||
return $this->internalStartCommands;
|
||||
}
|
||||
|
||||
$commands = [];
|
||||
|
||||
foreach (Config::getParam('frameworks', []) as $framework) {
|
||||
foreach ($framework['adapters'] ?? [] as $adapter) {
|
||||
if (!empty($adapter['startCommand'])) {
|
||||
$commands[] = $adapter['startCommand'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertNotEmpty($commands, 'Expected at least one framework to define a start command.');
|
||||
|
||||
$this->internalStartCommands = \array_values(\array_unique($commands));
|
||||
|
||||
return $this->internalStartCommands;
|
||||
}
|
||||
|
||||
protected function assertStartCommandNotExposed(mixed $payload, string $path = 'body'): void
|
||||
{
|
||||
if (\is_string($payload)) {
|
||||
foreach ($this->getInternalStartCommands() as $command) {
|
||||
$this->assertNotSame($command, $payload, 'Internal start command exposed at ' . $path);
|
||||
}
|
||||
|
||||
// Every internal command shells out to a helper script shipped in the runtime image.
|
||||
$this->assertStringNotContainsString('helpers/', $payload, 'Internal helper script exposed at ' . $path);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!\is_array($payload)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($payload as $key => $value) {
|
||||
$this->assertNotSame('startCommand', $key, 'Start command exposed at ' . $path . '.' . $key);
|
||||
$this->assertStartCommandNotExposed($value, $path . '.' . $key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,6 +117,27 @@ final class SitesCustomClientTest extends Scope
|
||||
$this->assertEquals(400, $templates['headers']['status-code']);
|
||||
}
|
||||
|
||||
public function testListTemplatesHidesStartCommand()
|
||||
{
|
||||
$templates = $this->client->call(Client::METHOD_GET, '/sites/templates', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
'limit' => 5000,
|
||||
]);
|
||||
|
||||
$this->assertEquals(200, $templates['headers']['status-code']);
|
||||
$this->assertGreaterThan(0, $templates['body']['total']);
|
||||
|
||||
$this->assertStartCommandNotExposed($templates['body']);
|
||||
|
||||
$template = $this->getTemplate('starter-for-nextjs');
|
||||
|
||||
$this->assertEquals(200, $template['headers']['status-code']);
|
||||
|
||||
$this->assertStartCommandNotExposed($template['body']);
|
||||
}
|
||||
|
||||
public function testGetTemplate()
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -2262,6 +2262,46 @@ final class SitesCustomServerTest extends Scope
|
||||
$this->assertArrayHasKey('outputDirectory', $framework['adapters'][0]);
|
||||
}
|
||||
|
||||
public function testGetFrameworksHidesStartCommand(): void
|
||||
{
|
||||
$frameworks = $this->client->call(Client::METHOD_GET, '/sites/frameworks', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
]));
|
||||
|
||||
$this->assertEquals(200, $frameworks['headers']['status-code']);
|
||||
$this->assertGreaterThan(0, $frameworks['body']['total']);
|
||||
|
||||
$this->assertStartCommandNotExposed($frameworks['body']);
|
||||
}
|
||||
|
||||
public function testCreateSiteHidesStartCommand(): void
|
||||
{
|
||||
$siteId = $this->setupSite([
|
||||
'siteId' => ID::unique(),
|
||||
'name' => 'SSR site without a start command',
|
||||
'framework' => 'nextjs',
|
||||
'adapter' => 'ssr',
|
||||
'buildRuntime' => 'node-22',
|
||||
'outputDirectory' => './.next',
|
||||
'buildCommand' => 'npm run build',
|
||||
'installCommand' => 'npm install',
|
||||
]);
|
||||
|
||||
$site = $this->client->call(Client::METHOD_GET, '/sites/' . $siteId, [
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
'x-appwrite-key' => $this->getProject()['apiKey'],
|
||||
]);
|
||||
|
||||
$this->assertEquals(200, $site['headers']['status-code']);
|
||||
|
||||
// An omitted start command stays empty; the runtime resolves its own default at boot.
|
||||
$this->assertSame('', $site['body']['startCommand']);
|
||||
|
||||
$this->cleanupSite($siteId);
|
||||
}
|
||||
|
||||
public function testSiteStatic(): void
|
||||
{
|
||||
$siteId = $this->setupSite([
|
||||
|
||||
@@ -24,4 +24,49 @@ final class DetectionTest extends TestCase
|
||||
$this->assertSame('ssr', $detection->getName());
|
||||
$this->assertNull($detection->getFallbackFile());
|
||||
}
|
||||
|
||||
/**
|
||||
* TanStack Start >= 1.133 builds vite-native, emitting the server entry as
|
||||
* dist/server/server.js alongside dist/client.
|
||||
*/
|
||||
public function testRenderingFindsTanStackStartSSR(): void
|
||||
{
|
||||
$detection = Detection::rendering('tanstack-start', [
|
||||
'./client/assets/main-BBU0Zbc3.js',
|
||||
'./server/server.js',
|
||||
]);
|
||||
|
||||
$this->assertSame('ssr', $detection->getName());
|
||||
$this->assertNull($detection->getFallbackFile());
|
||||
}
|
||||
|
||||
/**
|
||||
* Opting into the nitro plugin keeps the older layout, where the server
|
||||
* entry is .output/server/index.mjs.
|
||||
*/
|
||||
public function testRenderingFindsTanStackStartNitroSSR(): void
|
||||
{
|
||||
$detection = Detection::rendering('tanstack-start', [
|
||||
'./public/favicon.ico',
|
||||
'./server/index.mjs',
|
||||
]);
|
||||
|
||||
$this->assertSame('ssr', $detection->getName());
|
||||
$this->assertNull($detection->getFallbackFile());
|
||||
}
|
||||
|
||||
/**
|
||||
* A prerendered build ships only the client bundle, so no server entry is
|
||||
* present to match on.
|
||||
*/
|
||||
public function testRenderingFindsTanStackStartStatic(): void
|
||||
{
|
||||
$detection = Detection::rendering('tanstack-start', [
|
||||
'./index.html',
|
||||
'./assets/main-BBU0Zbc3.js',
|
||||
]);
|
||||
|
||||
$this->assertSame('static', $detection->getName());
|
||||
$this->assertSame('index.html', $detection->getFallbackFile());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Deployment;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* A framework's ssr and static adapters describe two views of one build, so
|
||||
* their output directories have to come from the same layout. Pointing an
|
||||
* adapter at a directory the build never writes yields an empty deployment
|
||||
* rather than a build failure, which is why these are asserted directly.
|
||||
*/
|
||||
final class FrameworksTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
private array $frameworks;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// Read the file rather than Config, which other suites overwrite.
|
||||
$this->frameworks = require __DIR__ . '/../../../app/config/frameworks.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* TanStack Start >= 1.133 builds vite-native: dist/server/server.js next to
|
||||
* dist/client. Only builds that opt into the nitro plugin still emit
|
||||
* .output, and those override the directory on the site.
|
||||
*/
|
||||
public function testTanStackStartAdaptersShareViteOutput(): void
|
||||
{
|
||||
$adapters = $this->frameworks['tanstack-start']['adapters'];
|
||||
|
||||
$this->assertSame('./dist', $adapters['ssr']['outputDirectory']);
|
||||
$this->assertSame('./dist/client', $adapters['static']['outputDirectory']);
|
||||
}
|
||||
|
||||
/**
|
||||
* nuxt generate writes .output/public, and leaves dist as a symlink to it.
|
||||
*/
|
||||
public function testNuxtStaticOutputIsInsideDotOutput(): void
|
||||
{
|
||||
$adapters = $this->frameworks['nuxt']['adapters'];
|
||||
|
||||
$this->assertSame('./.output', $adapters['ssr']['outputDirectory']);
|
||||
$this->assertSame('./.output/public', $adapters['static']['outputDirectory']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Next.js is the one framework whose static export genuinely leaves the ssr
|
||||
* build root, so it is excluded rather than special-cased below.
|
||||
*/
|
||||
public function testStaticOutputStaysInsideSsrOutput(): void
|
||||
{
|
||||
foreach ($this->frameworks as $key => $framework) {
|
||||
if ($key === 'nextjs') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ssr = $framework['adapters']['ssr']['outputDirectory'] ?? null;
|
||||
$static = $framework['adapters']['static']['outputDirectory'] ?? null;
|
||||
|
||||
if ($ssr === null || $static === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->assertStringStartsWith(
|
||||
\rtrim($ssr, '/') . '/',
|
||||
\rtrim($static, '/') . '/',
|
||||
"Framework '{$key}' serves static files from outside its ssr build output."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user