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:30:17 -05:00
parent 7f4d3c7543
commit 48ae151260
3 changed files with 305 additions and 37 deletions

View file

@ -236,13 +236,21 @@ test('resolver scopes GET and HEAD reads to granted projects, templates and owne
};
}
if (sql.startsWith('SELECT project_id FROM research_hypotheses')) {
if (values?.[0] === grantedHypothesis) return { rows: [{ project_id: grantedProject }] };
if (values?.[0] === ungrantedHypothesis) return { rows: [{ project_id: ungrantedProject }] };
if (values?.[0] === grantedHypothesis) {
return { rows: [{ project_id: grantedProject }] };
}
if (values?.[0] === ungrantedHypothesis) {
return { rows: [{ project_id: ungrantedProject }] };
}
return { rows: [] };
}
if (sql.startsWith('SELECT project_id FROM analysis_artifacts')) {
if (values?.[0] === grantedArtifact) return { rows: [{ project_id: grantedProject }] };
if (values?.[0] === ungrantedArtifact) return { rows: [{ project_id: ungrantedProject }] };
if (values?.[0] === grantedArtifact) {
return { rows: [{ project_id: grantedProject }] };
}
if (values?.[0] === ungrantedArtifact) {
return { rows: [{ project_id: ungrantedProject }] };
}
return { rows: [] };
}
if (sql.startsWith('SELECT id,submission_principal FROM research_experiments')) {
@ -257,19 +265,30 @@ test('resolver scopes GET and HEAD reads to granted projects, templates and owne
};
}
if (sql.startsWith('SELECT submission_principal FROM research_experiments')) {
if (values?.[0] === ownedExperiment) return { rows: [{ submission_principal: agent }] };
if (values?.[0] === foreignExperiment) return { rows: [{ submission_principal: 'operator' }] };
if (values?.[0] === ownedExperiment) {
return { rows: [{ submission_principal: agent }] };
}
if (values?.[0] === foreignExperiment) {
return { rows: [{ submission_principal: 'operator' }] };
}
return { rows: [] };
}
if (sql.startsWith('SELECT submission_principal FROM research_walk_forwards')) {
if (values?.[0] === ownedWalkForward) return { rows: [{ submission_principal: agent }] };
if (values?.[0] === foreignWalkForward)
if (values?.[0] === ownedWalkForward) {
return { rows: [{ submission_principal: agent }] };
}
if (values?.[0] === foreignWalkForward) {
return { rows: [{ submission_principal: 'operator' }] };
}
return { rows: [] };
}
if (sql.startsWith('SELECT research_agent_id FROM runs')) {
if (values?.[0] === ownedRun) return { rows: [{ research_agent_id: agent }] };
if (values?.[0] === foreignRun) return { rows: [{ research_agent_id: null }] };
if (values?.[0] === ownedRun) {
return { rows: [{ research_agent_id: agent }] };
}
if (values?.[0] === foreignRun) {
return { rows: [{ research_agent_id: null }] };
}
return { rows: [] };
}
if (sql.includes('FROM strategies s')) {
@ -468,14 +487,24 @@ test('artifact reads check run ownership before resolving evidence for research
};
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.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(sql).toContain('ORDER BY rev.ordinal DESC');
expect(sql).toContain('LIMIT 1');
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_artifacts WHERE project_id=$1')) {
return { rows: [artifactRow] };
}
if (sql.includes('FROM analysis_artifact_revisions WHERE artifact_id=$1')) {
return { rows: [revisionRow] };
}
@ -546,3 +575,145 @@ test('artifact reads check run ownership before resolving evidence for research
expect(opBody.artifact.latestRevision.evidence[0]?.value).toBe(2.45);
expect(resolveAll).toHaveBeenCalledTimes(1);
});
test('mixed artifact revision history allows latest-revision list and item reads while blocking history reads of foreign evidence', async () => {
const projectId = '11111111-1111-4111-8111-111111111111';
const artifactId = '22222222-2222-4222-8222-222222222222';
const rev1Id = '33333333-3333-4333-8333-333333333333';
const rev2Id = '33333333-3333-4333-8333-333333333334';
const foreignRunId = '44444444-4444-4444-8444-444444444444';
const ownedRunId = '44444444-4444-4444-8444-444444444445';
const now = new Date('2026-01-01T00:00:00Z');
const resolveAll = mock(async (refs: { runId: string }[]) =>
refs.map(ref => ({
runId: ref.runId,
datasetHash: 'a'.repeat(64),
metricsVersion: 'canonical-analytics/v1',
metricPath: 'analytics.metrics.sharpeRatio',
value: ref.runId === foreignRunId ? 2.45 : 1.15,
strategyVersionId: null,
}))
);
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 rev1Row = {
id: rev1Id,
artifact_id: artifactId,
ordinal: 1,
title: 'Initial holdout finding',
body: 'Operator holdout sharpe',
submitted_by: 'operator',
content_origin: 'human',
source: null,
created_at: now,
};
const rev2Row = {
id: rev2Id,
artifact_id: artifactId,
ordinal: 2,
title: 'Sanitized finding',
body: 'Agent-visible 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(sql).toContain('ORDER BY rev.ordinal DESC');
expect(sql).toContain('LIMIT 1');
expect(values).toEqual([projectId, false, agent]);
return { rows: [artifactRow] };
}
if (sql.includes('FROM analysis_artifact_revisions WHERE artifact_id=$1 ORDER BY ordinal DESC LIMIT 1')) {
return { rows: [rev2Row] };
}
if (sql.includes('FROM analysis_artifact_revisions WHERE artifact_id=$1 ORDER BY ordinal')) {
return { rows: [rev1Row, rev2Row] };
}
if (sql.includes('FROM analysis_evidence WHERE revision_id=$1')) {
const runId = values?.[0] === rev1Id ? foreignRunId : ownedRunId;
return {
rows: [
{
run_id: runId,
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[])')) {
const ids = (values?.[0] as string[]) ?? [];
return {
rows: ids.map(id => ({
id,
research_agent_id: id === ownedRunId ? agent : 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));
// Project list and item read succeed for the agent because latest revision (ordinal 2) uses ownedRunId
const listResp = await f.app.request(`/api/v2/research-projects/${projectId}/analysis-artifacts`, {
headers: { Authorization: `Bearer ${token}` },
});
expect(listResp.status).toBe(200);
expect(
((await listResp.json()) as { artifacts: { id: string; latestRevision: { ordinal: number } }[] })
.artifacts
).toMatchObject([{ id: artifactId, latestRevision: { ordinal: 2 } }]);
const itemResp = await f.app.request(`/api/v2/analysis-artifacts/${artifactId}`, {
headers: { Authorization: `Bearer ${token}` },
});
expect(itemResp.status).toBe(200);
expect(
((await itemResp.json()) as { artifact: { latestRevision: { evidence: { runId: string }[] } } })
.artifact.latestRevision.evidence[0]?.runId
).toBe(ownedRunId);
// History endpoint still fails closed with 403 because revision 1 references foreignRunId
const historyResp = await f.app.request(`/api/v2/analysis-artifacts/${artifactId}/revisions`, {
headers: { Authorization: `Bearer ${token}` },
});
expect(historyResp.status).toBe(403);
expect(historyResp.headers.get('cache-control')).toBe('no-store');
});