|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import sys |
| 15 | +from unittest import mock |
| 16 | + |
| 17 | +# Mock the retry module before importing rater_lib |
| 18 | +mock_retry_module = mock.MagicMock() |
| 19 | + |
| 20 | + |
| 21 | +def mock_retry_decorator(*args, **kwargs): |
| 22 | + def decorator(func): |
| 23 | + return func |
| 24 | + |
| 25 | + return decorator |
| 26 | + |
| 27 | + |
| 28 | +mock_retry_module.retry = mock_retry_decorator |
| 29 | +sys.modules["retry"] = mock_retry_module |
| 30 | + |
| 31 | +from gepa import rater_lib |
| 32 | + |
| 33 | + |
| 34 | +def test_rater_escapes_html_inputs_to_prevent_xss(): |
| 35 | + """Rater escapes HTML tags in user and model inputs to prevent XSS. |
| 36 | +
|
| 37 | + Setup: Mock genai.Client to return a dummy rating response. |
| 38 | + Act: Call Rater with messages containing HTML tags. |
| 39 | + Assert: Verify that the rendered prompt template contains escaped HTML. |
| 40 | + """ |
| 41 | + # Arrange |
| 42 | + with mock.patch("google.genai.Client") as mock_client_cls: |
| 43 | + mock_client = mock_client_cls.return_value |
| 44 | + mock_generate = mock_client.models.generate_content |
| 45 | + mock_generate.return_value.text = ( |
| 46 | + "Property: The agent fulfilled the user's primary request.\n" |
| 47 | + "Evidence: mock evidence\n" |
| 48 | + "Rationale: mock rationale\n" |
| 49 | + "Verdict: yes" |
| 50 | + ) |
| 51 | + |
| 52 | + template_path = ( |
| 53 | + "contributing/samples/integrations/gepa/rubric_validation_template.txt" |
| 54 | + ) |
| 55 | + rater = rater_lib.Rater( |
| 56 | + tool_declarations="[]", validation_template_path=template_path |
| 57 | + ) |
| 58 | + |
| 59 | + messages = [ |
| 60 | + { |
| 61 | + "role": "user", |
| 62 | + "parts": [{"text": "<script>alert('XSS')</script>"}], |
| 63 | + }, |
| 64 | + { |
| 65 | + "role": "model", |
| 66 | + "parts": [{"text": "Hello <img src=x onerror=alert(1)>"}], |
| 67 | + }, |
| 68 | + ] |
| 69 | + |
| 70 | + # Act |
| 71 | + rater(messages) |
| 72 | + |
| 73 | + # Assert |
| 74 | + assert mock_generate.called |
| 75 | + call_args = mock_generate.call_args |
| 76 | + contents = call_args.kwargs["contents"] |
| 77 | + |
| 78 | + assert "<script>alert('XSS')</script>" in contents |
| 79 | + assert "Hello <img src=x onerror=alert(1)>" in contents |
0 commit comments