Simplify the start command assertion

SitesBase carries thirty-nine helpers with no docblocks and a single
inline comment, so a getter that also asserts, a memoised trait property
and a recursion threading a path string for its failure messages all sat
well outside the file.

Encode the payload once and assert against the string. The key check gets
stronger as a result - it catches startCommand at any depth without
walking - and JSON_UNESCAPED_SLASHES keeps the helper paths intact, which
the default encoding would have hidden behind escaped slashes.
This commit is contained in:
harsh mahajan committed 2026-08-13 00:11:45 +05:30
1 parent cb64087d95
commit d6a58529d7
1 file changed
+7 -46
+7 -46
View File
@@ -19,9 +19,6 @@ 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([
@@ -501,57 +498,21 @@ 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
protected function assertStartCommandNotExposed(array $payload): void
{
if ($this->internalStartCommands !== null) {
return $this->internalStartCommands;
}
$body = \json_encode($payload, JSON_UNESCAPED_SLASHES);
$commands = [];
$this->assertStringNotContainsString('startCommand', $body);
// Every framework start command shells out to a helper script shipped in the runtime image.
$this->assertStringNotContainsString('helpers/', $body);
foreach (Config::getParam('frameworks', []) as $framework) {
foreach ($framework['adapters'] ?? [] as $adapter) {
if (!empty($adapter['startCommand'])) {
$commands[] = $adapter['startCommand'];
$this->assertStringNotContainsString($adapter['startCommand'], $body);
}
}
}
$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);
}
}
}