CI (SIAX Cloud) / quality (push) Successful in 50s
CI (SIAX Cloud) / integration (push) Successful in 35s
CI (SIAX Cloud) / security (push) Successful in 21s
Security Scan (SIAX Cloud) / security (push) Successful in 26s
CI (SIAX Cloud) / quality (pull_request) Successful in 46s
CI (SIAX Cloud) / integration (pull_request) Successful in 35s
CI (SIAX Cloud) / security (pull_request) Successful in 19s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 16s
AnalyticsSQLSession is a SEPARATE class, not a method on SiaxAnalyticsEngine.
executeMetric has no raw-SQL surface at all; on the same object the closed DSL
would be one refactor away from growing one. ClickHouseAnalyticsEngine
.executeQuery stays a documented refusal.
FOUR LAYERS, and the guard is deliberately called the weakest:
1 role — the CL0UD analyst role is checked when the SESSION IS OPENED, so
a caller without it never obtains an object that can run SQL.
2 guard — statement admission. Comments and literals removed with
ClickHouse's own lexical rules, ASCII-only outside literals, one
statement, SELECT/WITH only, and every table reference POSITIVELY
resolved against the tenant's catalog. Deny-lists are a second
opinion, never the mechanism.
3 grants — a per-tenant ClickHouse user with SELECT on that tenant's catalog
alone, plus a restrictive row policy on the base tables.
4 views — the tenant catalog holds only VIEWS, each baking
`WHERE tenant_id = '<tenant>'`, and tenant_id is not projected.
61 bypass attempts, in one shared corpus, run TWICE: against the guard, and
against a live ClickHouse 24.8.14.39 with LAYER 2 REMOVED. The live assertion
is not "the engine errored" — it is "no canary came back", with BETA_CANARY
seeded into the other tenant's rows and SECRET_CANARY into an ungranted table.
The counterfactual runs first: as admin the same base table returns 100 rows
and BETA_CANARY IS readable, so the isolation numbers mean something.
Result: 0 of 61 leaked. Evidence: evidence/bi-p017/analyst-sql-live-bypass.json.
FOUR DEFECTS FOUND BY WRITING TESTS THAT CAN FAIL — three of them mine:
* `CREATE ROW POLICY IF NOT EXISTS` is a SILENT NO-OP on re-provisioning.
Dropping the user empties the policy's apply-to list; the next run skips
re-creation, so the policy survives with `apply_to_list = []`, applying to
nobody, while `system.row_policies` still shows the correct select_filter.
The restricted user read 100 of 100 rows — BOTH TENANTS — from the base
table. Fixed with OR REPLACE + `rowPolicyVerificationSql`, which turns
"the policy is applied" from an assumption into a query, and a test that
asserts applied_to_user = 1 per object.
* `CREATE SETTINGS PROFILE IF NOT EXISTS` has the identical shape: a changed
budget never lands. A stale 60-row ceiling survived re-provisioning and
starved every legitimate statement, with the DDL reporting success
throughout. Also OR REPLACE now.
* `describe.skipIf(!reachable)` where `reachable` is set in `beforeAll`
skips ALWAYS — vitest evaluates skipIf at collection time. Ten tests
reported green while skipping against a live, reachable server. Replaced
with the repo's existing env-var gate, which FAILS when CLICKHOUSE_URL is
set and the server is down rather than degrading to a skip.
* MEASURED, and it changes the design: a ClickHouse user whose profile
carries `readonly = 1` cannot have ANY setting changed from the request
(Code: 164). The BI-P012 ceilings therefore cannot travel with an analyst
statement; they are baked into the user's SETTINGS PROFILE at provisioning.
Proven both ways: over-budget scan fails with code 158 from the profile
with no setting sent, and an attempt to raise it from the request fails
with code 164.
Honest about what the guard does NOT catch: `SELECT tenant_id FROM
t_alpha_sql.sales` is structurally legitimate and IS admitted. It fails at the
engine because the view does not project the column. Recorded in its own
GUARD_ADMITS_ENGINE_REFUSES list and asserted live, rather than deleted from
the corpus.
evidence/bi-p017 is pinned by tests/analytics/analyst-sql-evidence.test.ts,
which never skips: adding an attack without re-running the live suite breaks
the build, and the pin also checks the counterfactual numbers so the evidence
cannot be a recording of an empty database.
246 tests pass.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
125 lines
5.3 KiB
TypeScript
125 lines
5.3 KiB
TypeScript
/**
|
|
* BI-P009 DoD: "Provider contract tests added."
|
|
*
|
|
* This is the contract every `SiaxAnalyticsEngine` provider must satisfy
|
|
* regardless of engine — ClickHouse today, DuckDB (BI-P014) and Trino
|
|
* (BI-P015) later. It is written against the INTERFACE, so a future provider
|
|
* imports `analyticsEngineContract` and gets the same bar.
|
|
*
|
|
* The identity rules here are the ones that matter most: this estate has
|
|
* already shipped a live cross-tenant hole by reading the tenant out of a
|
|
* REQUEST BODY (INF0 Wave 2). So the contract asserts that a request field
|
|
* named `tenant_id` cannot influence the engine at all.
|
|
*/
|
|
import { describe, it, expect } from 'vitest';
|
|
import { ClickHouseAnalyticsEngine } from '@/src/analytics/engine/clickhouse-engine';
|
|
import {
|
|
SIAX_ANALYTICS_ENGINE_CAPABILITIES,
|
|
type AnalyticsIdentity,
|
|
type SiaxAnalyticsEngine,
|
|
} from '@/src/analytics/engine/siax-analytics-engine';
|
|
|
|
const IDENTITY: AnalyticsIdentity = {
|
|
tenant_id: 't_alpha',
|
|
project_id: 'p_default',
|
|
principal_id: 'usr_1',
|
|
};
|
|
|
|
/** Reusable contract suite — future providers call this with their factory. */
|
|
export function analyticsEngineContract(name: string, make: () => SiaxAnalyticsEngine): void {
|
|
describe(`SiaxAnalyticsEngine contract: ${name}`, () => {
|
|
it('implements all eight BI-P009 capabilities as callable methods', () => {
|
|
const engine = make() as unknown as Record<string, unknown>;
|
|
for (const cap of SIAX_ANALYTICS_ENGINE_CAPABILITIES) {
|
|
expect(typeof engine[cap], `${cap} is not a method`).toBe('function');
|
|
}
|
|
expect(SIAX_ANALYTICS_ENGINE_CAPABILITIES).toEqual([
|
|
'health',
|
|
'executeMetric',
|
|
'executeQuery',
|
|
'estimateQuery',
|
|
'cancelQuery',
|
|
'explain',
|
|
'ingestProjection',
|
|
'refreshProjection',
|
|
]);
|
|
});
|
|
|
|
it('names its provider', () => {
|
|
expect(make().provider.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('refuses every tenant-scoped capability without a verified identity', async () => {
|
|
const engine = make();
|
|
const blank = { tenant_id: '', project_id: 'p', principal_id: 'usr_1' };
|
|
const noPrincipal = { tenant_id: 't_alpha', project_id: 'p', principal_id: '' };
|
|
for (const bad of [blank, noPrincipal]) {
|
|
await expect(engine.executeMetric(bad, { metric: 'm', aggregate: 'sum' })).rejects.toMatchObject({
|
|
code: 'UNAUTHENTICATED',
|
|
});
|
|
await expect(engine.estimateQuery(bad, { metric: 'm', aggregate: 'sum' })).rejects.toMatchObject({
|
|
code: 'UNAUTHENTICATED',
|
|
});
|
|
await expect(engine.ingestProjection(bad, { dataset_id: 'ds', rows: [] })).rejects.toMatchObject({
|
|
code: 'UNAUTHENTICATED',
|
|
});
|
|
await expect(engine.refreshProjection(bad, 'ds')).rejects.toMatchObject({ code: 'UNAUTHENTICATED' });
|
|
await expect(engine.cancelQuery(bad, 'q')).rejects.toMatchObject({ code: 'UNAUTHENTICATED' });
|
|
}
|
|
});
|
|
|
|
it('a tenant_id smuggled into the REQUEST cannot influence the engine (INF0 Wave-2 bug class)', async () => {
|
|
const engine = make();
|
|
const smuggled = { metric: 'm', aggregate: 'sum', tenant_id: 't_victim' } as never;
|
|
// The request type has no tenant field; if a provider ever started
|
|
// reading one, this call would target t_victim. The engine must still
|
|
// fail on the IDENTITY, proving the request field is inert.
|
|
await expect(
|
|
engine.executeMetric({ tenant_id: '', project_id: 'p', principal_id: 'u' }, smuggled),
|
|
).rejects.toMatchObject({ code: 'UNAUTHENTICATED' });
|
|
});
|
|
|
|
it('cancelQuery refuses a query id belonging to another tenant', async () => {
|
|
const engine = make();
|
|
await expect(engine.cancelQuery(IDENTITY, 'opt_deadbeefdead_someoneelses')).rejects.toMatchObject({
|
|
code: 'AUTHORIZATION_DENIED',
|
|
});
|
|
});
|
|
|
|
it('health() reports unhealthy rather than throwing when the engine is unreachable', async () => {
|
|
const report = await make().health();
|
|
expect(report.healthy).toBe(false);
|
|
expect(report.provider.length).toBeGreaterThan(0);
|
|
// Never leak an engine body or a DSN into the operator-facing detail.
|
|
expect(report.detail ?? '').not.toMatch(/http|password|@/i);
|
|
});
|
|
});
|
|
}
|
|
|
|
analyticsEngineContract(
|
|
'clickhouse (unreachable host — contract-level assertions only)',
|
|
() =>
|
|
new ClickHouseAnalyticsEngine({
|
|
baseUrl: 'http://clickhouse.invalid:8123',
|
|
database: 'opt_contract_test',
|
|
timeoutMs: 500,
|
|
}),
|
|
);
|
|
|
|
describe('BI-P009 refusals that are deliberate, not missing', () => {
|
|
it('executeQuery (analyst SQL) is declared and REFUSED until BI-P017/BI-P033', async () => {
|
|
const engine = new ClickHouseAnalyticsEngine({ baseUrl: 'http://clickhouse.invalid:8123' });
|
|
await expect(engine.executeQuery(IDENTITY, { sql: 'SELECT 1', dataset_ids: [] })).rejects.toMatchObject({
|
|
code: 'CAPABILITY_UNAVAILABLE',
|
|
});
|
|
});
|
|
|
|
it('the PRESERVED ClickHouseAnalytics is reachable through the engine, not replaced by it', () => {
|
|
const engine = new ClickHouseAnalyticsEngine({ baseUrl: 'http://clickhouse.invalid:8123' });
|
|
expect(engine.legacyAnalytics.constructor.name).toBe('ClickHouseAnalytics');
|
|
expect(typeof engine.legacyAnalytics.queryMetric).toBe('function');
|
|
expect(typeof engine.legacyAnalytics.ensureSchema).toBe('function');
|
|
expect(typeof engine.legacyAnalytics.ingest).toBe('function');
|
|
});
|
|
});
|