feat(research): read verified frozen datasets (RP28)

This commit is contained in:
Boki 2026-10-28 00:03:15 -04:00
parent 04892a59fe
commit 4b9c642817
9 changed files with 263 additions and 18 deletions

View file

@ -32,10 +32,13 @@ const payloadSql = 'SELECT payload FROM backtest_datasets WHERE hash=$1';
const baseline = createDatasetV2(datasetInput());
const path = (hash = baseline.manifest.hash) => `/api/v2/datasets/canonical/${hash}/export`;
function fixture(load: () => Promise<unknown> = async () => baseline) {
let researchEnabled = true;
const payloadRead = mock(load);
const query = mock(async (sql: string, values?: unknown[]) => {
if (sql === 'SELECT id,enabled,policy FROM research_agents WHERE token_hash=$1') {
return { rows: [{ id: agentId, enabled: true, policy: { allowedProjectIds: [] } }] };
return {
rows: [{ id: agentId, enabled: researchEnabled, policy: { allowedProjectIds: [] } }],
};
}
if (sql !== payloadSql || values?.length !== 1) {
throw new Error('Unexpected query: export must not write or read other tables');
@ -69,7 +72,18 @@ function fixture(load: () => Promise<unknown> = async () => baseline) {
headers: Record<string, string> = { Authorization: `Bearer ${tokens.admin}` },
signal?: AbortSignal
) => app.request(path(hash), { headers, signal });
return { app, request, database, query, payloadRead, sourceRead, research };
return {
app,
request,
database,
query,
payloadRead,
sourceRead,
research,
revokeResearch: () => {
researchEnabled = false;
},
};
}
function responseHeaders(response: Response) {
expect(response.headers.get('Cache-Control')).toBe('no-store');
@ -268,11 +282,11 @@ describe('validated canonical dataset export', () => {
).rejects.toThrow('cancelled after query');
});
test('existing read, admin, service and browser credentials may export; anonymous and real research credentials may not', async () => {
test('read, admin, service, browser and research credentials export; anonymous and revoked credentials do not', async () => {
const f = fixture();
expect((await f.request(baseline.manifest.hash, {})).status).toBe(401);
expect(f.query).not.toHaveBeenCalled();
for (const token of [tokens.read, tokens.admin, tokens.service]) {
for (const token of [tokens.read, tokens.admin, tokens.service, tokens.research]) {
expect(
(await f.request(baseline.manifest.hash, { Authorization: `Bearer ${token}` })).status
).toBe(200);
@ -289,20 +303,13 @@ describe('validated canonical dataset export', () => {
expect((await f.request(baseline.manifest.hash, { Cookie: cookie! })).status).toBe(200);
}
const before = f.payloadRead.mock.calls.length;
f.revokeResearch();
const denied = await f.request(baseline.manifest.hash, {
Authorization: `Bearer ${tokens.research}`,
});
expect(denied.status).toBe(403);
expect(denied.status).toBe(401);
expect(denied.headers.get('Content-Disposition')).toBeNull();
expect(f.payloadRead).toHaveBeenCalledTimes(before);
// Positive control: this token is recognized by the real resolver for an allowed route.
expect(
await f.research.authenticate(
new Request('http://localhost/api/v2/experiments', {
headers: { Authorization: `Bearer ${tokens.research}` },
})
)
).toEqual({ principal: agentId });
expect(
f.query.mock.calls.filter(call => call[0].includes('FROM research_agents'))
).toHaveLength(2);

View file

@ -118,6 +118,41 @@ test('operator-only provisioning fails closed without enabled research authentic
}
expect(query).toHaveBeenCalledTimes(0);
});
test('resolver grants only exact immutable dataset exports among dataset operations', async () => {
const query = mock(async () => ({ rows: [{ id: agent, enabled: true, policy: {} }] }));
const service = new ResearchAgentsService({
postgres: { query },
} as unknown as IServiceContainer);
const hash = 'a'.repeat(64);
const check = (path: string, method = 'GET') =>
service.authenticate(
new Request(`http://localhost${path}`, {
method,
headers: { Authorization: `Bearer ${token}` },
})
);
for (const method of ['GET', 'HEAD']) {
expect(await check(`/api/v2/datasets/canonical/${hash}/export`, method)).toEqual({
principal: agent,
});
}
for (const [path, method] of [
['/api/v2/datasets/canonical', 'GET'],
[`/api/v2/datasets/canonical/${hash}`, 'GET'],
[`/api/v2/datasets/canonical/${hash}/export/extra`, 'GET'],
[`/api/v2/datasets/canonical/${hash.toUpperCase()}/export`, 'GET'],
['/api/v2/datasets/canonical/invalid/export', 'GET'],
['/api/v2/datasets/stored-eod/coverage', 'GET'],
['/api/v2/datasets/canonical/preview', 'POST'],
['/api/v2/datasets/canonical/retain', 'POST'],
['/api/v2/datasets/canonical/stored-source', 'POST'],
['/api/v2/datasets/stored-eod/inspect', 'POST'],
[`/api/v2/datasets/canonical/${hash}/export`, 'POST'],
]) {
expect(((await check(path!, method)) as Response).status).toBe(403);
}
});
test('resolver exact write capabilities refuse trading, operators, foreign evidence and ownership impersonation', async () => {
const projectId = randomUUID(),
experimentId = randomUUID(),