T-4: M0.4b — Close sb-research.* holdout leak: 403 on dataset /export and ungranted project GETs

This commit is contained in:
Studio 2026-11-27 01:16:52 -05:00
parent 13eb8352f3
commit 420e77f837
17 changed files with 788 additions and 1123 deletions

View file

@ -1048,4 +1048,178 @@ describe.skipIf(!url)('authoritative research principals and budgets', () => {
await f.agents.update(p.agent.id, { expectedRevision: 2, allowedBacktestIds: [f.draft.id] });
expect((await f.experiments.submit(changed, receipt(p.agent.id))).trials).toHaveLength(2);
});
test('HTTP read scoping blocks dataset export, ungranted projects/narratives/templates and foreign experiments/runs/strategies', async () => {
const f = await fixture(4),
p = await provision(f);
const security = createWebApiSecurity(f.container, {
adminToken: operatorToken,
serviceToken: 'service-test-secret-123456789012345678901',
environment: 'test',
});
const app = createRoutes(f.container, security);
const req = (path: string, token: string, method: 'GET' | 'HEAD' = 'GET') =>
app.request(path, { method, headers: { Authorization: `Bearer ${token}` } });
// 1. Dataset export: 403 for agent (GET/HEAD), 200 for operator
const exportPath = `/api/v2/datasets/canonical/${f.dataset.manifest.hash}/export`;
for (const method of ['GET', 'HEAD'] as const) {
const agentExport = await req(exportPath, p.token, method);
expect(agentExport.status).toBe(403);
expect(agentExport.headers.get('cache-control')).toBe('no-store');
expect(agentExport.headers.get('content-disposition')).toBeNull();
}
expect((await req(exportPath, operatorToken)).status).toBe(200);
// 2. Projects & narratives: granted vs ungranted
const ungrantedProject = await f.research.createProject(
{ title: 'Operator secret project' },
'operator'
);
const grantedHypothesis = await f.research.createHypothesis(
p.project.id,
{ title: 'Granted H', proposition: 'Visible to agent' },
'operator'
);
const ungrantedHypothesis = await f.research.createHypothesis(
ungrantedProject.id,
{ title: 'Secret H', proposition: 'Hidden from agent' },
'operator'
);
const grantedArtifact = await f.research.createArtifact(
p.project.id,
{ kind: 'note', title: 'Granted A', body: 'Visible note', evidence: [] },
'operator'
);
const ungrantedArtifact = await f.research.createArtifact(
ungrantedProject.id,
{ kind: 'note', title: 'Secret A', body: 'Hidden note', evidence: [] },
'operator'
);
const agentProjects = await req('/api/v2/research-projects', p.token);
expect(agentProjects.status).toBe(200);
expect(((await agentProjects.json()) as { projects: { id: string }[] }).projects.map(x => x.id)).toEqual([
p.project.id,
]);
const opProjects = await req('/api/v2/research-projects', operatorToken);
expect(opProjects.status).toBe(200);
expect(((await opProjects.json()) as { projects: { id: string }[] }).projects).toHaveLength(2);
for (const allowedPath of [
`/api/v2/research-projects/${p.project.id}`,
`/api/v2/research-projects/${p.project.id}/strategies`,
`/api/v2/research-projects/${p.project.id}/hypotheses`,
`/api/v2/research-projects/${p.project.id}/analysis-artifacts`,
`/api/v2/hypotheses/${grantedHypothesis.id}`,
`/api/v2/hypotheses/${grantedHypothesis.id}/revisions`,
`/api/v2/analysis-artifacts/${grantedArtifact.id}`,
`/api/v2/analysis-artifacts/${grantedArtifact.id}/revisions`,
]) {
expect((await req(allowedPath, p.token)).status).toBe(200);
}
for (const deniedPath of [
`/api/v2/research-projects/${ungrantedProject.id}`,
`/api/v2/research-projects/${ungrantedProject.id}/strategies`,
`/api/v2/research-projects/${ungrantedProject.id}/hypotheses`,
`/api/v2/research-projects/${ungrantedProject.id}/analysis-artifacts`,
`/api/v2/hypotheses/${ungrantedHypothesis.id}`,
`/api/v2/hypotheses/${ungrantedHypothesis.id}/revisions`,
`/api/v2/analysis-artifacts/${ungrantedArtifact.id}`,
`/api/v2/analysis-artifacts/${ungrantedArtifact.id}/revisions`,
'/api/v2/research-projects/not-a-uuid',
'/api/v2/hypotheses/not-a-uuid',
'/api/v2/analysis-artifacts/not-a-uuid',
]) {
expect((await req(deniedPath, p.token)).status).toBe(403);
}
// 3. Backtests, experiments & runs (including shared granted template)
const ungrantedBacktest = await f.api.createBacktest(
{ ...f.definition, name: 'Ungranted template' },
{ autoStart: false }
);
const agentBacktests = await req('/api/v2/backtests', p.token);
expect(agentBacktests.status).toBe(200);
expect(((await agentBacktests.json()) as { id: string }[]).map(b => b.id)).toEqual([
f.draft.id,
]);
expect((await req(`/api/v2/backtests/${f.draft.id}`, p.token)).status).toBe(200);
expect((await req(`/api/v2/backtests/${ungrantedBacktest.id}`, p.token)).status).toBe(403);
expect((await req(`/api/v2/backtests/${ungrantedBacktest.id}/runs`, p.token)).status).toBe(403);
const agentExp = await f.experiments.submit(
await preparation(f, 'Agent exp', f.draft.id, { fastPeriod: [1] }),
receipt(p.agent.id)
);
const opExp = await f.experiments.submit(
await preparation(f, 'Operator exp', f.draft.id, { fastPeriod: [2] }),
receipt('operator')
);
const agentRunId = required(agentExp.trials[0]!.runId);
const opRunId = required(opExp.trials[0]!.runId);
const agentExpList = await req('/api/v2/experiments', p.token);
expect(agentExpList.status).toBe(200);
expect(
((await agentExpList.json()) as { experiments: { id: string }[] }).experiments.map(e => e.id)
).toEqual([agentExp.id]);
expect((await req(`/api/v2/experiments?backtestId=${f.draft.id}`, p.token)).status).toBe(200);
expect(
(await req(`/api/v2/experiments?backtestId=${ungrantedBacktest.id}`, p.token)).status
).toBe(403);
expect((await req(`/api/v2/experiments/${agentExp.id}`, p.token)).status).toBe(200);
expect((await req(`/api/v2/experiments/${opExp.id}`, p.token)).status).toBe(403);
expect(
(
await req(
`/api/v2/experiments/compare?leftId=${agentExp.id}&rightId=${opExp.id}`,
p.token
)
).status
).toBe(403);
const agentRuns = await req(`/api/v2/backtests/${f.draft.id}/runs`, p.token);
expect(agentRuns.status).toBe(200);
expect(((await agentRuns.json()) as { id: string }[]).map(r => r.id)).toEqual([agentRunId]);
expect((await req(`/api/v2/runs/${agentRunId}`, p.token)).status).toBe(200);
expect((await req(`/api/v2/runs/${opRunId}`, p.token)).status).toBe(403);
expect((await req('/api/v2/runs/not-a-uuid', p.token)).status).toBe(403);
// 4. Strategies & strategy-versions: granted/owned vs unlinked foreign
const foreignStrategy = await f.catalog.cloneVersion(
f.version.id,
{ name: 'Foreign operator strategy' },
'operator',
randomUUID()
);
const ownedStrategy = await f.catalog.cloneVersion(
f.version.id,
{ name: 'Agent owned strategy' },
p.agent.id as `agent:${string}`,
randomUUID()
);
const agentStrategies = await req('/api/v2/strategies', p.token);
expect(agentStrategies.status).toBe(200);
const visibleStrategyIds = (
(await agentStrategies.json()) as { strategies: { id: string }[] }
).strategies.map(s => s.id);
expect(visibleStrategyIds).toContain(f.version.strategyId);
expect(visibleStrategyIds).toContain(ownedStrategy.strategy.id);
expect(visibleStrategyIds).not.toContain(foreignStrategy.strategy.id);
expect((await req(`/api/v2/strategies/${f.version.strategyId}`, p.token)).status).toBe(200);
expect((await req(`/api/v2/strategies/${ownedStrategy.strategy.id}`, p.token)).status).toBe(200);
expect((await req(`/api/v2/strategies/${foreignStrategy.strategy.id}`, p.token)).status).toBe(
403
);
expect((await req(`/api/v2/strategy-versions/${ownedStrategy.version.id}`, p.token)).status).toBe(
200
);
expect(
(await req(`/api/v2/strategy-versions/${foreignStrategy.version.id}`, p.token)).status
).toBe(403);
expect((await req('/api/v2/strategies/not-a-uuid', p.token)).status).toBe(403);
expect((await req('/api/v2/strategy-versions/not-a-uuid', p.token)).status).toBe(403);
});
});