From a8c5fe2d4be53ff6e1d58409e80649e37b622efc Mon Sep 17 00:00:00 2001 From: Studio Date: Fri, 27 Nov 2026 01:55:19 -0500 Subject: [PATCH] =?UTF-8?q?T-4:=20M0.4b=20=E2=80=94=20Close=20sb-research.?= =?UTF-8?q?*=20holdout=20leak:=20403=20on=20dataset=20/export=20and=20ungr?= =?UTF-8?q?anted=20project=20GETs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web-api/src/services/research-evidence.ts | 6 +- .../web-api/src/services/research.service.ts | 12 +- test/integration/research-agents-api.test.ts | 120 ++++++++++++++++-- 3 files changed, 121 insertions(+), 17 deletions(-) diff --git a/apps/stock/web-api/src/services/research-evidence.ts b/apps/stock/web-api/src/services/research-evidence.ts index 4acef96b..2a1ee4b9 100644 --- a/apps/stock/web-api/src/services/research-evidence.ts +++ b/apps/stock/web-api/src/services/research-evidence.ts @@ -138,7 +138,11 @@ export class ResearchEvidenceResolver { database: BacktestResultQuery ): Promise { if (!source) { - return Promise.all(references.map(reference => this.resolve(reference, database))); + const resolved: ResearchEvidence[] = []; + for (const reference of references) { + resolved.push(await this.resolve(reference, database)); + } + return resolved; } const results = await verifyResearchSource(source, references, database, (id, query) => this.results.getRunResults(id, query) diff --git a/apps/stock/web-api/src/services/research.service.ts b/apps/stock/web-api/src/services/research.service.ts index a2a14dc3..64189cae 100644 --- a/apps/stock/web-api/src/services/research.service.ts +++ b/apps/stock/web-api/src/services/research.service.ts @@ -419,7 +419,11 @@ export class ResearchService { [projectId, includeArchived] ) ).rows; - return Promise.all(rows.map(row => this.artifact(client, row, undefined, agentId))); + const artifacts: ResearchArtifact[] = []; + for (const row of rows) { + artifacts.push(await this.artifact(client, row, undefined, agentId)); + } + return artifacts; }); } async createArtifact( @@ -484,7 +488,11 @@ export class ResearchService { [id] ) ).rows; - return Promise.all(rows.map(row => this.artifactRevision(client, row, agentId))); + const revisions: ResearchArtifactRevision[] = []; + for (const row of rows) { + revisions.push(await this.artifactRevision(client, row, agentId)); + } + return revisions; }); } async reviseArtifact( diff --git a/test/integration/research-agents-api.test.ts b/test/integration/research-agents-api.test.ts index 38f36275..da6089a6 100644 --- a/test/integration/research-agents-api.test.ts +++ b/test/integration/research-agents-api.test.ts @@ -523,14 +523,49 @@ test('artifact reads check run ownership before resolving evidence for research if (sql.includes('SELECT id, research_agent_id FROM runs WHERE id = ANY($1::uuid[])')) { return { rows: [{ id: foreignRunId, research_agent_id: null }] }; } + if (sql.includes('SELECT id,backtest_id,status FROM runs WHERE id=$1')) { + return { + rows: [ + { + id: values?.[0], + backtest_id: '55555555-5555-4555-8555-555555555555', + status: 'completed', + }, + ], + }; + } throw new Error(`Unexpected SQL: ${sql}`); }); + let queriesAfterRelease = 0; const container = { postgres: { query: clientQuery, - transaction: async (fn: (client: { query: typeof clientQuery }) => Promise) => - fn({ query: clientQuery }), + transaction: async (fn: (client: { query: typeof clientQuery }) => Promise) => { + let released = false; + let queue = Promise.resolve(); + const txClient = { + query: ((sql: string, values?: unknown[]) => { + const run = queue.then(async () => { + if (released) { + queriesAfterRelease += 1; + throw new Error(`Query executed on released transaction client: ${sql}`); + } + return clientQuery(sql, values); + }); + queue = run.then( + () => undefined, + () => undefined + ); + return run; + }) as typeof clientQuery, + }; + try { + return await fn(txClient); + } finally { + released = true; + } + }, }, logger: { error: mock(() => {}) }, } as unknown as IServiceContainer; @@ -574,6 +609,7 @@ 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); + expect(queriesAfterRelease).toBe(0); }); test('mixed artifact revision history allows latest-revision list and item reads while blocking history reads of foreign evidence', async () => { @@ -584,15 +620,24 @@ test('mixed artifact revision history allows latest-revision list and item reads 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 resolveAll = mock( + async ( + refs: { runId: string }[], + _source: unknown, + db?: { query: (sql: string, values?: unknown[]) => Promise<{ rows: unknown[] }> } + ) => { + if (db) { + await db.query('SELECT id,backtest_id,status FROM runs WHERE id=$1', [refs[0]?.runId]); + } + return 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, @@ -672,14 +717,49 @@ test('mixed artifact revision history allows latest-revision list and item reads })), }; } + if (sql.includes('SELECT id,backtest_id,status FROM runs WHERE id=$1')) { + return { + rows: [ + { + id: values?.[0], + backtest_id: '55555555-5555-4555-8555-555555555555', + status: 'completed', + }, + ], + }; + } throw new Error(`Unexpected SQL: ${sql}`); }); + let queriesAfterRelease = 0; const container = { postgres: { query: clientQuery, - transaction: async (fn: (client: { query: typeof clientQuery }) => Promise) => - fn({ query: clientQuery }), + transaction: async (fn: (client: { query: typeof clientQuery }) => Promise) => { + let released = false; + let queue = Promise.resolve(); + const txClient = { + query: ((sql: string, values?: unknown[]) => { + const run = queue.then(async () => { + if (released) { + queriesAfterRelease += 1; + throw new Error(`Query executed on released transaction client: ${sql}`); + } + return clientQuery(sql, values); + }); + queue = run.then( + () => undefined, + () => undefined + ); + return run; + }) as typeof clientQuery, + }; + try { + return await fn(txClient); + } finally { + released = true; + } + }, }, logger: { error: mock(() => {}) }, } as unknown as IServiceContainer; @@ -710,10 +790,22 @@ test('mixed artifact revision history allows latest-revision list and item reads .artifact.latestRevision.evidence[0]?.runId ).toBe(ownedRunId); - // History endpoint still fails closed with 403 because revision 1 references foreignRunId + // History endpoint still fails closed with 403 because revision 1 references foreignRunId, + // and must not leak dangling queries onto the released transaction client. 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'); + await new Promise(resolve => setTimeout(resolve, 10)); + expect(queriesAfterRelease).toBe(0); + + // Subsequent operator history read succeeds cleanly on a fresh transaction + const opHistoryResp = await f.app.request(`/api/v2/analysis-artifacts/${artifactId}/revisions`, { + headers: { Authorization: `Bearer ${adminToken}` }, + }); + expect(opHistoryResp.status).toBe(200); + expect( + ((await opHistoryResp.json()) as { revisions: { ordinal: number }[] }).revisions + ).toHaveLength(2); });