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:21:54 -05:00
parent 420e77f837
commit 7f4d3c7543
4 changed files with 383 additions and 30 deletions

View file

@ -166,7 +166,8 @@ export function createResearchRoutes(
c.json({
artifacts: await service.listArtifacts(
c.req.param('id'),
includeArchived(c.req.query('includeArchived'))
includeArchived(c.req.query('includeArchived')),
agentScope(c.req.raw)
),
})
);
@ -183,10 +184,12 @@ export function createResearchRoutes(
)
);
app.get('/api/v2/analysis-artifacts/:id', async c =>
c.json({ artifact: await service.getArtifact(c.req.param('id')) })
c.json({ artifact: await service.getArtifact(c.req.param('id'), agentScope(c.req.raw)) })
);
app.get('/api/v2/analysis-artifacts/:id/revisions', async c =>
c.json({ revisions: await service.listArtifactRevisions(c.req.param('id')) })
c.json({
revisions: await service.listArtifactRevisions(c.req.param('id'), agentScope(c.req.raw)),
})
);
app.post('/api/v2/analysis-artifacts/:id/revisions', async c =>
c.json(

View file

@ -14,21 +14,22 @@ import {
reviseResearchHypothesisSchema,
updateResearchProjectSchema,
} from '@stock-bot/stock-config/research';
import type {
ResearchActor,
ResearchArtifact,
ResearchArtifactRevision,
ResearchAuthorship,
ResearchContentOrigin,
ResearchEvidence,
ResearchEvidenceReference,
ResearchFindingSource,
ResearchHypothesis,
ResearchHypothesisRevision,
ResearchProject,
StrategyRecord,
import {
isResearchAgent,
type ResearchActor,
type ResearchArtifact,
type ResearchArtifactRevision,
type ResearchAuthorship,
type ResearchContentOrigin,
type ResearchEvidence,
type ResearchEvidenceReference,
type ResearchFindingSource,
type ResearchHypothesis,
type ResearchHypothesisRevision,
type ResearchProject,
type StrategyRecord,
} from '@stock-bot/types';
import { lockResearchAgent } from './research-agent-budget';
import { lockResearchAgent, ResearchAgentError } from './research-agent-budget';
import { ResearchError } from './research-errors';
import type { ResearchEvidenceResolver } from './research-evidence';
import type { StrategyCatalogService } from './strategy-catalog.service';
@ -382,18 +383,38 @@ export class ResearchService {
});
}
async listArtifacts(projectId: string, includeArchived = false): Promise<ResearchArtifact[]> {
async listArtifacts(
projectId: string,
includeArchived = false,
agentId?: string
): Promise<ResearchArtifact[]> {
researchIdSchema.parse(projectId);
return this.database.transaction(async client => {
await client.query('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY');
await this.projectRow(client, projectId);
const rows = (
await client.query(
'SELECT * FROM analysis_artifacts WHERE project_id=$1 AND ($2::boolean OR archived_at IS NULL) ORDER BY created_at DESC,id',
[projectId, includeArchived]
)
agentId !== undefined
? await client.query(
`SELECT a.* FROM analysis_artifacts a
WHERE a.project_id=$1
AND ($2::boolean OR a.archived_at IS NULL)
AND NOT EXISTS (
SELECT 1
FROM analysis_artifact_revisions rev
JOIN analysis_evidence ev ON ev.revision_id = rev.id
LEFT JOIN runs r ON r.id = ev.run_id
WHERE rev.artifact_id = a.id
AND r.research_agent_id IS DISTINCT FROM $3
)
ORDER BY a.created_at DESC, a.id`,
[projectId, includeArchived, agentId]
)
: await client.query(
'SELECT * FROM analysis_artifacts WHERE project_id=$1 AND ($2::boolean OR archived_at IS NULL) ORDER BY created_at DESC,id',
[projectId, includeArchived]
)
).rows;
return Promise.all(rows.map(row => this.artifact(client, row)));
return Promise.all(rows.map(row => this.artifact(client, row, undefined, agentId)));
});
}
async createArtifact(
@ -440,14 +461,14 @@ export class ResearchService {
return this.artifact(client, row, revision);
});
}
async getArtifact(id: string): Promise<ResearchArtifact> {
async getArtifact(id: string, agentId?: string): Promise<ResearchArtifact> {
researchIdSchema.parse(id);
return this.database.transaction(async client => {
await client.query('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY');
return this.artifact(client, await this.artifactRow(client, id));
return this.artifact(client, await this.artifactRow(client, id), undefined, agentId);
});
}
async listArtifactRevisions(id: string): Promise<ResearchArtifactRevision[]> {
async listArtifactRevisions(id: string, agentId?: string): Promise<ResearchArtifactRevision[]> {
researchIdSchema.parse(id);
return this.database.transaction(async client => {
await client.query('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY');
@ -458,7 +479,7 @@ export class ResearchService {
[id]
)
).rows;
return Promise.all(rows.map(row => this.artifactRevision(client, row)));
return Promise.all(rows.map(row => this.artifactRevision(client, row, agentId)));
});
}
async reviseArtifact(
@ -621,7 +642,8 @@ export class ResearchService {
private async artifact(
database: Database,
row: QueryResultRow,
resolved?: ResearchArtifactRevision
resolved?: ResearchArtifactRevision,
agentId?: string
): Promise<ResearchArtifact> {
const latest =
resolved ??
@ -635,7 +657,8 @@ export class ResearchService {
)
).rows[0],
'Artifact revision'
)
),
agentId
));
return {
id: row.id,
@ -650,9 +673,35 @@ export class ResearchService {
latestRevision: latest,
};
}
private async assertAgentOwnsEvidenceRuns(
database: Database,
references: ResearchEvidenceReference[],
agentId: string
): Promise<void> {
const uniqueRunIds = [...new Set(references.map(ref => ref.runId))];
if (uniqueRunIds.length === 0) {
return;
}
const rows = (
await database.query<{ id: string; research_agent_id: string | null }>(
'SELECT id, research_agent_id FROM runs WHERE id = ANY($1::uuid[])',
[uniqueRunIds]
)
).rows;
if (
rows.length !== uniqueRunIds.length ||
rows.some(item => item.research_agent_id !== agentId)
) {
throw new ResearchAgentError(
403,
'Research credential does not permit accessing foreign run evidence'
);
}
}
private async artifactRevision(
database: Database,
row: QueryResultRow
row: QueryResultRow,
agentId?: string
): Promise<ResearchArtifactRevision> {
const references: ResearchEvidenceReference[] = (
await database.query(
@ -665,6 +714,9 @@ export class ResearchService {
metricPath: item.metric_path,
strategyVersionId: item.strategy_version_id,
}));
if (agentId !== undefined && references.length > 0) {
await this.assertAgentOwnsEvidenceRuns(database, references, agentId);
}
const source = findingSource(row.source);
let evidence;
try {
@ -699,6 +751,9 @@ export class ResearchService {
...reference,
strategyVersionId: reference.strategyVersionId ?? null,
}));
if (isResearchAgent(author.submittedBy) && references.length > 0) {
await this.assertAgentOwnsEvidenceRuns(client, references, author.submittedBy);
}
const evidence = await this.evidence.resolveAll(references, data.source, client);
const revisionId = randomUUID();
const row = required(