T-4: M0.4b — Close sb-research.* holdout leak: 403 on dataset /export and ungranted project GETs
This commit is contained in:
parent
420e77f837
commit
7f4d3c7543
4 changed files with 383 additions and 30 deletions
|
|
@ -8,9 +8,11 @@ import {
|
|||
} from '@stock-bot/di';
|
||||
import type { IServiceContainer } from '@stock-bot/services';
|
||||
import { createResearchAgentSchema } from '@stock-bot/stock-config/research-agents';
|
||||
import { createResearchRoutes } from '../../apps/stock/web-api/src/routes/research.routes';
|
||||
import { createResearchAgentRoutes } from '../../apps/stock/web-api/src/routes/research-agents.routes';
|
||||
import { researchAgentDatabaseError } from '../../apps/stock/web-api/src/services/research-agent-budget';
|
||||
import { ResearchAgentsService } from '../../apps/stock/web-api/src/services/research-agents.service';
|
||||
import { ResearchService } from '../../apps/stock/web-api/src/services/research.service';
|
||||
|
||||
const agent = `agent:${randomUUID()}` as const;
|
||||
const token = 'sb-research.fixture-token';
|
||||
|
|
@ -416,3 +418,131 @@ test('quota diagnostics remain distinguishable from arbitrary database failures;
|
|||
}).success
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
test('artifact reads check run ownership before resolving evidence for research agents while preserving operator access', async () => {
|
||||
const projectId = randomUUID(),
|
||||
artifactId = randomUUID(),
|
||||
revisionId = randomUUID(),
|
||||
foreignRunId = randomUUID(),
|
||||
now = new Date().toISOString();
|
||||
|
||||
const resolveAll = mock(async () => [
|
||||
{
|
||||
runId: foreignRunId,
|
||||
backtestId: randomUUID(),
|
||||
metricsVersion: 'canonical-analytics/v1' as const,
|
||||
metricPath: 'analytics.metrics.sharpeRatio' as const,
|
||||
strategyVersionId: null,
|
||||
value: 2.45,
|
||||
unit: 'ratio' as const,
|
||||
availability: 'available' as const,
|
||||
unavailableReason: null,
|
||||
resultKind: 'canonical' as const,
|
||||
financialEngine: 'rust' as const,
|
||||
datasetHash: 'b'.repeat(64),
|
||||
limitations: [],
|
||||
},
|
||||
]);
|
||||
|
||||
const artifactRow = {
|
||||
id: artifactId,
|
||||
project_id: projectId,
|
||||
hypothesis_revision_id: null,
|
||||
kind: 'finding',
|
||||
owner_principal: 'operator',
|
||||
updated_by: 'operator',
|
||||
archived_at: null,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
};
|
||||
const revisionRow = {
|
||||
id: revisionId,
|
||||
artifact_id: artifactId,
|
||||
ordinal: 1,
|
||||
title: 'Holdout finding',
|
||||
body: 'Operator holdout sharpe',
|
||||
submitted_by: 'operator',
|
||||
content_origin: 'human',
|
||||
source: null,
|
||||
created_at: now,
|
||||
};
|
||||
|
||||
const clientQuery = mock(async (sql: string, values?: unknown[]) => {
|
||||
if (sql.startsWith('SET TRANSACTION')) return { rows: [] };
|
||||
if (sql.includes('FROM research_projects WHERE id=$1')) return { rows: [{ id: projectId }] };
|
||||
if (sql.includes('FROM analysis_artifacts WHERE id=$1')) return { rows: [artifactRow] };
|
||||
if (sql.includes('FROM analysis_artifacts a') && sql.includes('NOT EXISTS')) {
|
||||
expect(values).toEqual([projectId, false, agent]);
|
||||
return { rows: [] };
|
||||
}
|
||||
if (sql.includes('FROM analysis_artifacts WHERE project_id=$1')) return { rows: [artifactRow] };
|
||||
if (sql.includes('FROM analysis_artifact_revisions WHERE artifact_id=$1')) {
|
||||
return { rows: [revisionRow] };
|
||||
}
|
||||
if (sql.includes('FROM analysis_evidence WHERE revision_id=$1')) {
|
||||
return {
|
||||
rows: [
|
||||
{
|
||||
run_id: foreignRunId,
|
||||
metrics_version: 'canonical-analytics/v1',
|
||||
metric_path: 'analytics.metrics.sharpeRatio',
|
||||
strategy_version_id: null,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
if (sql.includes('SELECT id, research_agent_id FROM runs WHERE id = ANY($1::uuid[])')) {
|
||||
return { rows: [{ id: foreignRunId, research_agent_id: null }] };
|
||||
}
|
||||
throw new Error(`Unexpected SQL: ${sql}`);
|
||||
});
|
||||
|
||||
const container = {
|
||||
postgres: {
|
||||
query: clientQuery,
|
||||
transaction: async <T>(fn: (client: { query: typeof clientQuery }) => Promise<T>) =>
|
||||
fn({ query: clientQuery }),
|
||||
},
|
||||
logger: { error: mock(() => {}) },
|
||||
} as unknown as IServiceContainer;
|
||||
|
||||
const service = new ResearchService(container, {
|
||||
evidence: { resolveAll },
|
||||
catalog: { getStrategy: mock(async () => ({}) as never) },
|
||||
});
|
||||
const f = boundary();
|
||||
f.app.route('/', createResearchRoutes(container, service));
|
||||
|
||||
// Agent item and revision reads must fail closed with 403 before resolving evidence
|
||||
for (const method of ['GET', 'HEAD'] as const) {
|
||||
for (const path of [
|
||||
`/api/v2/analysis-artifacts/${artifactId}`,
|
||||
`/api/v2/analysis-artifacts/${artifactId}/revisions`,
|
||||
]) {
|
||||
const response = await f.app.request(path, {
|
||||
method,
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
expect(response.status).toBe(403);
|
||||
expect(response.headers.get('cache-control')).toBe('no-store');
|
||||
}
|
||||
}
|
||||
// Agent project-list read omits artifacts referencing foreign runs without resolving evidence
|
||||
const agentList = await f.app.request(`/api/v2/research-projects/${projectId}/analysis-artifacts`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
expect(agentList.status).toBe(200);
|
||||
expect(await agentList.json()).toEqual({ artifacts: [] });
|
||||
expect(resolveAll).toHaveBeenCalledTimes(0);
|
||||
|
||||
// Operator access remains intact and resolves evidence
|
||||
const opItem = await f.app.request(`/api/v2/analysis-artifacts/${artifactId}`, {
|
||||
headers: { Authorization: `Bearer ${adminToken}` },
|
||||
});
|
||||
expect(opItem.status).toBe(200);
|
||||
const opBody = (await opItem.json()) as {
|
||||
artifact: { latestRevision: { evidence: { value: number }[] } };
|
||||
};
|
||||
expect(opBody.artifact.latestRevision.evidence[0]?.value).toBe(2.45);
|
||||
expect(resolveAll).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue