Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

indirectPromptInjection.test.ts 2.6 KB

You have to be logged in to leave a comment. Sign In
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
  1. import { IndirectPromptInjectionGrader } from '../../../src/redteam/plugins/indirectPromptInjection';
  2. describe('IndirectPromptInjectionGrader', () => {
  3. let grader: IndirectPromptInjectionGrader;
  4. beforeEach(() => {
  5. grader = new IndirectPromptInjectionGrader();
  6. });
  7. it('should have the correct plugin ID', () => {
  8. expect(grader.id).toBe('promptfoo:redteam:indirect-prompt-injection');
  9. });
  10. describe('getSuggestions', () => {
  11. it('should throw error if renderedValue is not provided', () => {
  12. expect(() => {
  13. grader.getSuggestions({
  14. rawPrompt: 'test prompt',
  15. });
  16. }).toThrow('renderedValue string is expected in indirect prompt injection grader');
  17. });
  18. it('should throw error if renderedValue is not a string', () => {
  19. expect(() => {
  20. grader.getSuggestions({
  21. rawPrompt: 'test prompt',
  22. renderedValue: 123 as any,
  23. });
  24. }).toThrow('renderedValue string is expected in indirect prompt injection grader');
  25. });
  26. it('should return array with datamarking and encoding suggestions', () => {
  27. const rawPrompt = 'Original prompt with {input} here';
  28. const renderedValue = 'malicious input';
  29. const result = grader.getSuggestions({
  30. rawPrompt,
  31. renderedValue,
  32. });
  33. expect(result).toHaveLength(2);
  34. expect(result[0].type).toBe('datamark');
  35. expect(result[1].type).toBe('encoding');
  36. expect(result[0].action).toBe('replace-prompt');
  37. expect(result[1].action).toBe('replace-prompt');
  38. });
  39. it('should generate correct datamarking suggestion', () => {
  40. const userInput = 'test input';
  41. const rawPrompt = `Original prompt with ${userInput} here`;
  42. const result = grader.getSuggestions({
  43. rawPrompt,
  44. renderedValue: userInput,
  45. });
  46. const datamarkSuggestion = result[0];
  47. const expectedDatamarked = 'test^input';
  48. expect(datamarkSuggestion.value).toContain('^');
  49. expect(datamarkSuggestion.value).toContain(`Original prompt with ${expectedDatamarked} here`);
  50. });
  51. it('should generate correct encoding suggestion', () => {
  52. const userInput = 'test input';
  53. const rawPrompt = `Original prompt with ${userInput} here`;
  54. const expectedEncoded = Buffer.from(userInput).toString('base64');
  55. const result = grader.getSuggestions({
  56. rawPrompt,
  57. renderedValue: userInput,
  58. });
  59. const encodingSuggestion = result[1];
  60. expect(encodingSuggestion.value).toContain('base64');
  61. expect(encodingSuggestion.value).toContain(`Original prompt with ${expectedEncoded} here`);
  62. });
  63. });
  64. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...