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
142 lines
4.4 KiB
TypeScript
142 lines
4.4 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import IndirectWebPwnProvider from '../../../src/redteam/providers/indirectWebPwn';
|
|
import { createMockProvider, createProviderResponse } from '../../factories/provider';
|
|
|
|
import type { CallApiContextParams } from '../../../src/types/index';
|
|
|
|
const mockFetchWithRetries = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock('../../../src/util/fetch/index', async (importOriginal) => ({
|
|
...(await importOriginal()),
|
|
fetchWithRetries: (...args: unknown[]) => mockFetchWithRetries(...args),
|
|
}));
|
|
|
|
vi.mock('../../../src/globalConfig/accounts', () => ({
|
|
getUserEmail: vi.fn().mockReturnValue('test@example.com'),
|
|
}));
|
|
|
|
vi.mock('../../../src/redteam/remoteGeneration', async (importOriginal) => ({
|
|
...(await importOriginal()),
|
|
getRemoteGenerationUrl: vi.fn().mockReturnValue('https://mocked.task.api'),
|
|
}));
|
|
|
|
function mockJsonResponse(payload: unknown, ok = true) {
|
|
return {
|
|
ok,
|
|
status: ok ? 200 : 500,
|
|
json: async () => payload,
|
|
text: async () => JSON.stringify(payload),
|
|
};
|
|
}
|
|
|
|
describe('IndirectWebPwnProvider', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('should count one probe per target fetch attempt', async () => {
|
|
mockFetchWithRetries
|
|
// create-web-page
|
|
.mockResolvedValueOnce(
|
|
mockJsonResponse({
|
|
uuid: 'web-123',
|
|
fullUrl: 'https://example.com/dynamic-pages/eval-1/web-123',
|
|
path: '/dynamic-pages/eval-1/web-123',
|
|
fetchPrompt: 'Please fetch https://example.com/dynamic-pages/eval-1/web-123',
|
|
}),
|
|
)
|
|
// tracking for attempt 1
|
|
.mockResolvedValueOnce(mockJsonResponse({ wasFetched: false, fetchCount: 0 }))
|
|
// tracking for attempt 2
|
|
.mockResolvedValueOnce(mockJsonResponse({ wasFetched: true, fetchCount: 1 }));
|
|
|
|
const targetProvider = createMockProvider({ id: 'mock-target' });
|
|
targetProvider.callApi
|
|
.mockReset()
|
|
.mockResolvedValueOnce(
|
|
createProviderResponse({
|
|
output: 'Attempt 1 output',
|
|
tokenUsage: { total: 10, prompt: 4, completion: 6 },
|
|
}),
|
|
)
|
|
.mockResolvedValueOnce(
|
|
createProviderResponse({
|
|
output: 'Attempt 2 output',
|
|
tokenUsage: { total: 20, prompt: 8, completion: 12 },
|
|
}),
|
|
);
|
|
|
|
const provider = new IndirectWebPwnProvider({
|
|
injectVar: 'query',
|
|
maxFetchAttempts: 3,
|
|
useLlm: false,
|
|
});
|
|
|
|
const context: CallApiContextParams = {
|
|
originalProvider: targetProvider,
|
|
vars: { query: 'Find secrets' },
|
|
prompt: { raw: '{{query}}', label: 'test' },
|
|
test: {
|
|
metadata: {
|
|
goal: 'Find secrets',
|
|
testCaseId: 'tc-1',
|
|
},
|
|
} as any,
|
|
evaluationId: 'eval-1',
|
|
};
|
|
|
|
const result = await provider.callApi('attack prompt', context);
|
|
|
|
expect(result.metadata?.fetchAttempts).toBe(2);
|
|
expect(result.metadata?.stopReason).toBe('Attack succeeded');
|
|
expect(result.tokenUsage?.numRequests).toBe(2);
|
|
expect(result.tokenUsage?.total).toBe(30);
|
|
expect(result.tokenUsage?.prompt).toBe(12);
|
|
expect(result.tokenUsage?.completion).toBe(18);
|
|
});
|
|
|
|
it('should count probe requests even when target returns an error', async () => {
|
|
mockFetchWithRetries.mockResolvedValueOnce(
|
|
mockJsonResponse({
|
|
uuid: 'web-err',
|
|
fullUrl: 'https://example.com/dynamic-pages/eval-1/web-err',
|
|
path: '/dynamic-pages/eval-1/web-err',
|
|
fetchPrompt: 'Please fetch https://example.com/dynamic-pages/eval-1/web-err',
|
|
}),
|
|
);
|
|
|
|
const targetProvider = createMockProvider({
|
|
id: 'mock-target',
|
|
response: createProviderResponse({
|
|
output: 'error output',
|
|
error: 'Target failed',
|
|
}),
|
|
});
|
|
|
|
const provider = new IndirectWebPwnProvider({
|
|
injectVar: 'query',
|
|
maxFetchAttempts: 3,
|
|
useLlm: false,
|
|
});
|
|
|
|
const context: CallApiContextParams = {
|
|
originalProvider: targetProvider,
|
|
vars: { query: 'Find secrets' },
|
|
prompt: { raw: '{{query}}', label: 'test' },
|
|
test: {
|
|
metadata: {
|
|
goal: 'Find secrets',
|
|
testCaseId: 'tc-2',
|
|
},
|
|
} as any,
|
|
evaluationId: 'eval-2',
|
|
};
|
|
|
|
const result = await provider.callApi('attack prompt', context);
|
|
|
|
expect(result.metadata?.fetchAttempts).toBe(1);
|
|
expect(result.metadata?.stopReason).toBe('Error');
|
|
expect(result.tokenUsage?.numRequests).toBe(1);
|
|
});
|
|
});
|