1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
- import { fetchWithCache } from '../../src/cache';
- import {
- extractGoalFromPrompt,
- getShortPluginId,
- isBasicRefusal,
- isEmptyResponse,
- normalizeApostrophes,
- removePrefix,
- } from '../../src/redteam/util';
- jest.mock('../../src/cache');
- describe('removePrefix', () => {
- it('should remove a simple prefix', () => {
- expect(removePrefix('Prompt: Hello world', 'Prompt')).toBe('Hello world');
- });
- it('should be case insensitive', () => {
- expect(removePrefix('PROMPT: Hello world', 'prompt')).toBe('Hello world');
- });
- it('should remove asterisks from the prefix', () => {
- expect(removePrefix('**Prompt:** Hello world', 'Prompt')).toBe('Hello world');
- });
- it('should handle multiple asterisks', () => {
- expect(removePrefix('***Prompt:*** Hello world', 'Prompt')).toBe('Hello world');
- });
- it('should return the same string if prefix is not found', () => {
- expect(removePrefix('Hello world', 'Prefix')).toBe('Hello world');
- });
- it('should handle empty strings', () => {
- expect(removePrefix('', 'Prefix')).toBe('');
- });
- it('should handle prefix that is the entire string', () => {
- expect(removePrefix('Prompt:', 'Prompt')).toBe('');
- });
- });
- describe('normalizeApostrophes', () => {
- it('should normalize different types of apostrophes', () => {
- expect(normalizeApostrophes("I'm")).toBe("I'm");
- expect(normalizeApostrophes('I′m')).toBe("I'm");
- expect(normalizeApostrophes("I'm")).toBe("I'm");
- expect(normalizeApostrophes("I'm")).toBe("I'm");
- });
- it('should handle strings without apostrophes', () => {
- expect(normalizeApostrophes('Hello world')).toBe('Hello world');
- });
- });
- describe('isEmptyResponse', () => {
- it('should return true for empty responses', () => {
- expect(isEmptyResponse('')).toBe(true);
- expect(isEmptyResponse(' ')).toBe(true);
- expect(isEmptyResponse('{}')).toBe(true);
- expect(isEmptyResponse(' {} ')).toBe(true);
- });
- it('should return false for non-empty responses', () => {
- expect(isEmptyResponse('Hello')).toBe(false);
- expect(isEmptyResponse('{"key": "value"}')).toBe(false);
- });
- });
- describe('isBasicRefusal', () => {
- it('should detect refusal prefixes', () => {
- expect(isBasicRefusal('I am sorry, I cannot help with that')).toBe(true);
- expect(isBasicRefusal("I'm not able to assist with this request")).toBe(true);
- expect(isBasicRefusal('I apologize, but I cannot do that')).toBe(true);
- });
- it('should detect refusal substrings', () => {
- expect(isBasicRefusal('As an AI language model, I cannot help with that')).toBe(true);
- expect(isBasicRefusal('I cannot assist with that request')).toBe(true);
- expect(isBasicRefusal('That would not be appropriate')).toBe(true);
- });
- it('should normalize apostrophes in responses', () => {
- expect(isBasicRefusal("I′m sorry, I can't help")).toBe(true);
- expect(isBasicRefusal("I'm unable to assist")).toBe(true);
- });
- it('should handle case insensitivity', () => {
- expect(isBasicRefusal('I AM SORRY, I CANNOT HELP')).toBe(true);
- expect(isBasicRefusal('as an ai language model')).toBe(true);
- });
- it('should return false for non-refusal responses', () => {
- expect(isBasicRefusal('I will help you with that')).toBe(false);
- expect(isBasicRefusal('Here is the information you requested')).toBe(false);
- expect(isBasicRefusal('The answer is 42')).toBe(false);
- });
- });
- describe('getShortPluginId', () => {
- it('should remove promptfoo:redteam: prefix', () => {
- expect(getShortPluginId('promptfoo:redteam:test')).toBe('test');
- });
- it('should return original if no prefix', () => {
- expect(getShortPluginId('test')).toBe('test');
- });
- });
- describe('extractGoalFromPrompt', () => {
- beforeEach(() => {
- jest.resetAllMocks();
- });
- it('should successfully extract goal', async () => {
- jest.mocked(fetchWithCache).mockResolvedValue({
- cached: false,
- status: 200,
- statusText: 'OK',
- headers: {},
- data: { intent: 'test goal' },
- deleteFromCache: async () => {},
- });
- const result = await extractGoalFromPrompt('test prompt', 'test purpose');
- expect(result).toBe('test goal');
- });
- it('should return null on HTTP error', async () => {
- jest.mocked(fetchWithCache).mockResolvedValue({
- cached: false,
- status: 500,
- statusText: 'Internal Server Error',
- headers: {},
- data: {},
- deleteFromCache: async () => {},
- });
- const result = await extractGoalFromPrompt('test prompt', 'test purpose');
- expect(result).toBeNull();
- });
- it('should return null when no intent returned', async () => {
- jest.mocked(fetchWithCache).mockResolvedValue({
- cached: false,
- status: 200,
- statusText: 'OK',
- headers: {},
- data: {},
- deleteFromCache: async () => {},
- });
- const result = await extractGoalFromPrompt('test prompt', 'test purpose');
- expect(result).toBeNull();
- });
- it('should return null when API throws error', async () => {
- jest.mocked(fetchWithCache).mockRejectedValue(new Error('API error'));
- const result = await extractGoalFromPrompt('test prompt', 'test purpose');
- expect(result).toBeNull();
- });
- it('should handle empty prompt and purpose', async () => {
- jest.mocked(fetchWithCache).mockResolvedValue({
- cached: false,
- status: 200,
- statusText: 'OK',
- headers: {},
- data: { intent: 'empty goal' },
- deleteFromCache: async () => {},
- });
- const result = await extractGoalFromPrompt('', '');
- expect(result).toBe('empty goal');
- });
- it('should include plugin context when pluginId is provided', async () => {
- jest.mocked(fetchWithCache).mockResolvedValue({
- cached: false,
- status: 200,
- statusText: 'OK',
- headers: {},
- data: { intent: 'plugin-specific goal' },
- deleteFromCache: async () => {},
- });
- const result = await extractGoalFromPrompt(
- 'innocent prompt',
- 'test purpose',
- 'indirect-prompt-injection',
- );
- expect(result).toBe('plugin-specific goal');
- // Verify that the API was called with plugin context
- expect(fetchWithCache).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- body: expect.stringContaining('pluginContext'),
- }),
- expect.any(Number),
- );
- });
- it('should skip remote call when remote generation is disabled', async () => {
- // Preserve original environment setting
- const originalValue = process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION;
- process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION = 'true';
- const result = await extractGoalFromPrompt('test prompt', 'test purpose');
- expect(result).toBeNull();
- expect(fetchWithCache).not.toHaveBeenCalled();
- // Cleanup: restore or delete the env var to avoid leaking into other tests
- if (originalValue === undefined) {
- delete process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION;
- } else {
- process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION = originalValue;
- }
- });
- });
|