Files
promptfoo--promptfoo/test/blobs/index.test.ts
T
wehub-resource-sync 0d3cb498a3
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

218 lines
7.3 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { eq, inArray } from 'drizzle-orm';
import { afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import {
getShareAuthorizedBlob,
isBlobAllowedForShare,
recordBlobReference,
resetBlobStorageProvider,
setBlobStorageProvider,
} from '../../src/blobs';
import { getDb } from '../../src/database';
import { blobAssetsTable, blobReferencesTable, evalsTable } from '../../src/database/tables';
import { runDbMigrations } from '../../src/migrate';
import type { BlobStorageProvider } from '../../src/blobs';
describe('blob share authorization', () => {
const evalId = `eval-${randomUUID()}`;
const otherEvalId = `eval-${randomUUID()}`;
const trustedHash = 'a'.repeat(64);
const unclassifiedHash = 'b'.repeat(64);
const importedHash = 'c'.repeat(64);
const otherEvalHash = 'd'.repeat(64);
const hashes = [trustedHash, unclassifiedHash, importedHash, otherEvalHash];
beforeAll(async () => {
await runDbMigrations();
});
beforeEach(async () => {
const db = await getDb();
await db.insert(evalsTable).values([
{ id: evalId, config: {}, results: {} },
{ id: otherEvalId, config: {}, results: {} },
]);
await db.insert(blobAssetsTable).values(
hashes.map((hash) => ({
hash,
mimeType: 'image/png',
provider: 'filesystem',
sizeBytes: 1,
})),
);
await db.insert(blobReferencesTable).values([
{
id: randomUUID(),
blobHash: trustedHash,
evalId,
kind: 'image',
location: 'response.output',
},
{
id: randomUUID(),
blobHash: unclassifiedHash,
evalId,
location: 'response.output',
},
{
id: randomUUID(),
blobHash: importedHash,
evalId,
location: 'import',
},
{
id: randomUUID(),
blobHash: otherEvalHash,
evalId: otherEvalId,
kind: 'image',
location: 'response.output',
},
]);
});
afterEach(async () => {
const db = await getDb();
await db
.delete(blobReferencesTable)
.where(inArray(blobReferencesTable.evalId, [evalId, otherEvalId]));
await db.delete(blobAssetsTable).where(inArray(blobAssetsTable.hash, hashes));
await db.delete(evalsTable).where(inArray(evalsTable.id, [evalId, otherEvalId]));
});
it('allows only trusted references associated with the local eval', async () => {
await expect(isBlobAllowedForShare(trustedHash, evalId)).resolves.toBe(true);
await expect(isBlobAllowedForShare(importedHash, evalId)).resolves.toBe(true);
await expect(isBlobAllowedForShare(unclassifiedHash, evalId)).resolves.toBe(false);
await expect(isBlobAllowedForShare(otherEvalHash, evalId)).resolves.toBe(false);
});
it('does not authorize a trusted hash after its eval association is removed', async () => {
const db = await getDb();
await db.delete(blobReferencesTable).where(eq(blobReferencesTable.blobHash, trustedHash));
await expect(isBlobAllowedForShare(trustedHash, evalId)).resolves.toBe(false);
});
it('reads local bytes only for share-authorized references', async () => {
setBlobStorageProvider({
providerId: 'test-stub',
store: async () => {
throw new Error('not implemented');
},
getByHash: async (hash: string) => ({
data: Buffer.from('trusted-bytes'),
metadata: {
createdAt: '2026-06-08T00:00:00.000Z',
key: hash,
mimeType: 'image/png',
provider: 'test-stub',
sizeBytes: 13,
},
}),
exists: async () => true,
deleteByHash: async () => {},
getUrl: async () => null,
});
try {
await expect(getShareAuthorizedBlob(unclassifiedHash, evalId)).resolves.toBeNull();
await expect(getShareAuthorizedBlob(otherEvalHash, evalId)).resolves.toBeNull();
const blob = await getShareAuthorizedBlob(trustedHash, evalId);
expect(blob?.data.toString()).toBe('trusted-bytes');
} finally {
resetBlobStorageProvider();
}
});
});
describe('recordBlobReference provenance upgrades', () => {
const evalId = `eval-${randomUUID()}`;
const hash = 'e'.repeat(64);
const stubProvider: BlobStorageProvider = {
providerId: 'test-stub',
store: async () => {
throw new Error('not implemented');
},
getByHash: async () => {
throw new Error('not implemented');
},
exists: async () => true,
deleteByHash: async () => {},
getUrl: async () => null,
};
beforeAll(async () => {
await runDbMigrations();
});
beforeEach(async () => {
setBlobStorageProvider(stubProvider);
const db = await getDb();
await db.insert(evalsTable).values([{ id: evalId, config: {}, results: {} }]);
await db
.insert(blobAssetsTable)
.values([{ hash, mimeType: 'image/png', provider: 'filesystem', sizeBytes: 1 }]);
});
afterEach(async () => {
resetBlobStorageProvider();
const db = await getDb();
await db.delete(blobReferencesTable).where(eq(blobReferencesTable.evalId, evalId));
await db.delete(blobAssetsTable).where(eq(blobAssetsTable.hash, hash));
await db.delete(evalsTable).where(eq(evalsTable.id, evalId));
});
async function getReferenceRows() {
const db = await getDb();
return db.select().from(blobReferencesTable).where(eq(blobReferencesTable.evalId, evalId));
}
it('upgrades an unclassified reference once the blob is independently classified', async () => {
await recordBlobReference(hash, { evalId, location: 'response.output' });
await expect(isBlobAllowedForShare(hash, evalId)).resolves.toBe(false);
await recordBlobReference(hash, { evalId, kind: 'image', location: 'response.images[0].data' });
await expect(isBlobAllowedForShare(hash, evalId)).resolves.toBe(true);
const rows = await getReferenceRows();
expect(rows).toHaveLength(1);
expect(rows[0].kind).toBe('image');
});
it('does not authorize re-recorded scan references that carry no kind', async () => {
await recordBlobReference(hash, { evalId, location: 'response.output' });
await recordBlobReference(hash, { evalId, location: 'response.metadata' });
await expect(isBlobAllowedForShare(hash, evalId)).resolves.toBe(false);
const rows = await getReferenceRows();
expect(rows).toHaveLength(1);
expect(rows[0].kind).toBeNull();
});
it('does not downgrade a classified reference when re-recorded without a kind', async () => {
await recordBlobReference(hash, { evalId, kind: 'image', location: 'response.output' });
await recordBlobReference(hash, { evalId, location: 'response.metadata' });
await expect(isBlobAllowedForShare(hash, evalId)).resolves.toBe(true);
const rows = await getReferenceRows();
expect(rows).toHaveLength(1);
expect(rows[0].kind).toBe('image');
});
it('upgrades a scan reference to import provenance', async () => {
await recordBlobReference(hash, { evalId, location: 'response.output' });
await expect(isBlobAllowedForShare(hash, evalId)).resolves.toBe(false);
await recordBlobReference(hash, { evalId, location: 'import' });
await expect(isBlobAllowedForShare(hash, evalId)).resolves.toBe(true);
const rows = await getReferenceRows();
expect(rows).toHaveLength(1);
expect(rows[0].location).toBe('import');
});
});