diff --git a/app/config/frameworks.php b/app/config/frameworks.php index 342657017f..d7cd958ece 100644 --- a/app/config/frameworks.php +++ b/app/config/frameworks.php @@ -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' => [ diff --git a/tests/e2e/Services/Sites/SitesBase.php b/tests/e2e/Services/Sites/SitesBase.php index 00cc0a6cc6..c48814332d 100644 --- a/tests/e2e/Services/Sites/SitesBase.php +++ b/tests/e2e/Services/Sites/SitesBase.php @@ -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|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 + */ + 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); + } + } } diff --git a/tests/e2e/Services/Sites/SitesCustomClientTest.php b/tests/e2e/Services/Sites/SitesCustomClientTest.php index b87faec691..97f5df1e0e 100644 --- a/tests/e2e/Services/Sites/SitesCustomClientTest.php +++ b/tests/e2e/Services/Sites/SitesCustomClientTest.php @@ -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() { /** diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index 59d252af87..02a2562627 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -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([ diff --git a/tests/unit/Deployment/DetectionTest.php b/tests/unit/Deployment/DetectionTest.php index 42ec656830..984fae98a9 100644 --- a/tests/unit/Deployment/DetectionTest.php +++ b/tests/unit/Deployment/DetectionTest.php @@ -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()); + } } diff --git a/tests/unit/Deployment/FrameworksTest.php b/tests/unit/Deployment/FrameworksTest.php new file mode 100644 index 0000000000..3d3a2d34ec --- /dev/null +++ b/tests/unit/Deployment/FrameworksTest.php @@ -0,0 +1,79 @@ + + */ + 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." + ); + } + } +}