fix(install): honor DO_NOT_TRACK for self-hosted install telemetry

Skip the install/upgrade analytics ping when DO_NOT_TRACK is set.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Chirag AggarwalandCursor committed 2026-07-29 16:38:41 +05:30
1 parent 1cd4b89d3b
commit dede2ab442
2 files changed
+16 -168

No files matched your search

+16 -44
View File
@@ -852,45 +852,18 @@ class Install extends Action
return;
}
$payload = $this->buildSelfHostedInstallPayload($input, $isUpgrade, $version, $account);
if ($payload === null) {
// Opt out via DO_NOT_TRACK (https://donottrack.sh/)
$doNotTrack = \strtolower((string) System::getEnv('DO_NOT_TRACK', ''));
if (\in_array($doNotTrack, ['1', 'true', 'yes'], true)) {
return;
}
try {
$client = new Client();
$client
->setConnectTimeout(5000)
->setTimeout(5000)
->addHeader('Content-Type', 'application/json')
->fetch(self::GROWTH_API_URL . '/analytics', Client::METHOD_POST, $payload);
} catch (\Throwable) {
// tracking shouldn't block installation
}
}
/**
* Build the self-hosted install/upgrade analytics payload.
*
* Returns null when tracking must not happen (opt-out or non-production).
*
* @param array<string, mixed> $input Resolved environment variables
* @param array<string, mixed> $account Administrator account details
* @return array<string, mixed>|null
*/
public function buildSelfHostedInstallPayload(array $input, bool $isUpgrade, string $version, array $account = []): ?array
{
if ($this->isTelemetryDisabled()) {
return null;
}
$appEnv = $input['_APP_ENV'] ?? 'development';
$domain = $input['_APP_DOMAIN'] ?? 'localhost';
/* local or test instance */
if ($appEnv !== 'production') {
return null;
return;
}
/* prod but local or test instance */
@@ -898,7 +871,7 @@ class Install extends Action
|| str_starts_with($domain, '127.')
|| str_starts_with($domain, '0.0.0.0')
) {
return null;
return;
}
$type = $isUpgrade ? 'upgrade' : 'install';
@@ -908,7 +881,7 @@ class Install extends Action
$hostIp = @gethostbyname($domain);
return [
$payload = [
'action' => $type,
'account' => 'self-hosted',
'url' => 'https://' . $domain,
@@ -927,18 +900,17 @@ class Install extends Action
'ram' => (int) round(((float) trim((string) \shell_exec('grep MemTotal /proc/meminfo | awk \'{print $2}\''))) / 1024),
]),
];
}
/**
* Whether the operator opted out of install/upgrade telemetry.
*
* Honors DO_NOT_TRACK (https://donottrack.sh/).
*/
public function isTelemetryDisabled(): bool
{
$doNotTrack = \strtolower((string) System::getEnv('DO_NOT_TRACK', ''));
return \in_array($doNotTrack, ['1', 'true', 'yes'], true);
try {
$client = new Client();
$client
->setConnectTimeout(5000)
->setTimeout(5000)
->addHeader('Content-Type', 'application/json')
->fetch(self::GROWTH_API_URL . '/analytics', Client::METHOD_POST, $payload);
} catch (\Throwable) {
// tracking shouldn't block installation
}
}
/**
-124
View File
@@ -1,124 +0,0 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Platform\Tasks;
use Appwrite\Platform\Tasks\Install;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../../../../app/init.php';
final class InstallTest extends TestCase
{
private Install $install;
protected function setUp(): void
{
// Ensure a deterministic, opted-in environment for each test.
\putenv('DO_NOT_TRACK');
$this->install = new Install();
}
protected function tearDown(): void
{
\putenv('DO_NOT_TRACK');
}
/**
* @return array<string, mixed>
*/
private function productionInput(): array
{
return [
'_APP_ENV' => 'production',
'_APP_DOMAIN' => 'example.com',
'_APP_DB_ADAPTER' => 'mariadb',
];
}
/**
* @return array<string, mixed>
*/
private function account(): array
{
return [
'name' => 'Jane Admin',
'email' => 'jane@example.com',
];
}
public function testPayloadIncludesAdminNameAndEmail(): void
{
$payload = $this->install->buildSelfHostedInstallPayload($this->productionInput(), false, '1.9.6', $this->account());
$this->assertIsArray($payload);
$this->assertSame('install', $payload['action']);
$this->assertSame('self-hosted', $payload['account']);
$this->assertSame('1.9.6', $payload['version']);
$data = \json_decode($payload['data'], true);
$this->assertIsArray($data);
$this->assertSame('Jane Admin', $data['name']);
$this->assertSame('jane@example.com', $data['email']);
$this->assertSame('example.com', $data['domain']);
$this->assertSame('mariadb', $data['database']);
$this->assertArrayHasKey('os', $data);
$this->assertArrayHasKey('arch', $data);
}
public function testUpgradeActionIsReported(): void
{
$payload = $this->install->buildSelfHostedInstallPayload($this->productionInput(), true, '1.9.6', $this->account());
$this->assertIsArray($payload);
$this->assertSame('upgrade', $payload['action']);
$this->assertSame('self_hosted_upgrade', $payload['label']);
}
public function testDoNotTrackDisablesTelemetry(): void
{
\putenv('DO_NOT_TRACK=1');
$this->assertTrue($this->install->isTelemetryDisabled());
$this->assertNull($this->install->buildSelfHostedInstallPayload($this->productionInput(), false, '1.9.6', $this->account()));
}
public function testDoNotTrackTrueValueDisablesTelemetry(): void
{
\putenv('DO_NOT_TRACK=true');
$this->assertTrue($this->install->isTelemetryDisabled());
}
public function testDoNotTrackYesValueDisablesTelemetry(): void
{
\putenv('DO_NOT_TRACK=yes');
$this->assertTrue($this->install->isTelemetryDisabled());
$this->assertNull($this->install->buildSelfHostedInstallPayload($this->productionInput(), false, '1.9.6', $this->account()));
}
public function testTelemetryEnabledByDefault(): void
{
$this->assertFalse($this->install->isTelemetryDisabled());
}
public function testNonProductionEnvironmentIsNotTracked(): void
{
$input = $this->productionInput();
$input['_APP_ENV'] = 'development';
$this->assertNull($this->install->buildSelfHostedInstallPayload($input, false, '1.9.6', $this->account()));
}
public function testLocalhostDomainIsNotTracked(): void
{
$input = $this->productionInput();
$input['_APP_DOMAIN'] = 'localhost';
$this->assertNull($this->install->buildSelfHostedInstallPayload($input, false, '1.9.6', $this->account()));
}
}