Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
Guard API tests against destructive non-loopback targets
Mike Cao7627c366425b
diff --git a/scripts/test-api.ts b/scripts/test-api.tsindex 982b163e3..690258159 100644--- a/scripts/test-api.ts+++ b/scripts/test-api.ts@@ -14,6 +14,7 @@ import { spawnSync } from 'node:child_process';import { mkdirSync, writeFileSync } from 'node:fs';import path from 'node:path';import { fileURLToPath } from 'node:url';+import { assertDisposableTarget } from '../tests/api/paths';const ROOT_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');const COMPOSE_FILE = 'docker-compose.test.yml';@@ -51,6 +52,8 @@ Environment:COMPOSE_PROJECT_NAME Compose project name (default umami-api-test)PLAYWRIGHT_BASE_URL Test an already-running server instead of starting theCompose stack (pass --clickhouse if it stores analytics there)+ API_ALLOW_DESTRUCTIVE=1 Required for non-loopback PLAYWRIGHT_BASE_URL targets: the suite+ recreates websites, ingests data and toggles global 2FAAPI_SKIP_SEED=1 Reuse the previous seed when re-running against a kept stack(each run seeds additively otherwise)`;@@ -240,6 +243,12 @@ async function main() {try {if (EXTERNAL_TARGET) {+ try {+ assertDisposableTarget(BASE_URL);+ } catch (error) {+ console.error((error as Error).message);+ return 2;+ }console.log(`Using the already-running server at ${BASE_URL} (PLAYWRIGHT_BASE_URL is set).`);} else {console.log(`Starting test stack (profile: ${profile}, port: ${PORT})...`);diff --git a/tests/api/global-setup.ts b/tests/api/global-setup.tsindex 67151159d..c02ed29f5 100644--- a/tests/api/global-setup.ts+++ b/tests/api/global-setup.ts@@ -3,7 +3,7 @@ import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';import { type FullConfig, request } from '@playwright/test';import { ApiClient } from './client';import { disableCoverageRecording } from './coverage/recorder';-import { COVERAGE_DIR, SEED_FILE } from './paths';+import { assertDisposableTarget, COVERAGE_DIR, SEED_FILE } from './paths';import { seedEnvironment } from './seed/setup';const HEARTBEAT_TIMEOUT_MS = 120_000;@@ -44,6 +44,8 @@ export default async function globalSetup(config: FullConfig) {throw new Error('playwright.api.config.ts must define use.baseURL');}+ assertDisposableTarget(baseURL);+// API_SKIP_SEED=1 reuses the seed from a previous run against a kept stack// (fast iteration on a single spec).const skipSeed = !!process.env.API_SKIP_SEED && existsSync(SEED_FILE);diff --git a/tests/api/paths.ts b/tests/api/paths.tsindex 07b717590..b2f4adab1 100644--- a/tests/api/paths.ts+++ b/tests/api/paths.ts@@ -20,3 +20,24 @@ export const RUNTIME_DIR = path.join(export const COVERAGE_DIR = path.join(RUNTIME_DIR, 'coverage');export const SEED_FILE = path.join(RUNTIME_DIR, 'seed.json');export const OPENAPI_FILE = path.join(ROOT_DIR, 'public/openapi.json');++const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '[::1]', '::1']);++/**+ * The suite is destructive: global setup deletes and recreates fixed-ID websites+ * and ingests analytics data, and specs toggle the global 2FA requirement. Only+ * loopback targets (the local Compose stack) run without an explicit opt-in.+ */+export function assertDisposableTarget(baseURL: string) {+ const { hostname } = new URL(baseURL);++ if (LOOPBACK_HOSTS.has(hostname) || process.env.API_ALLOW_DESTRUCTIVE === '1') {+ return;+ }++ throw new Error(+ `Refusing to run the API suite against ${baseURL}: it deletes and recreates websites, ` ++ 'ingests analytics data and changes the global 2FA setting. Point PLAYWRIGHT_BASE_URL ' ++ 'at a disposable server and set API_ALLOW_DESTRUCTIVE=1 to confirm.',+ );+}