|
| 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 | + |
| 15 | +import signal |
| 16 | + |
| 17 | +from google.adk.code_executors import code_execution_utils |
| 18 | +from google.genai import types |
| 19 | + |
| 20 | + |
| 21 | +def test_extract_code_and_truncate_content_basic(): |
| 22 | + """Tests basic code extraction and content truncation.""" |
| 23 | + content = types.Content( |
| 24 | + role="model", |
| 25 | + parts=[ |
| 26 | + types.Part( |
| 27 | + text=( |
| 28 | + "Here is some code:\n```python\nx = 1\n```\nAnd some text" |
| 29 | + " after." |
| 30 | + ) |
| 31 | + ) |
| 32 | + ], |
| 33 | + ) |
| 34 | + delimiters = [("```python\n", "\n```")] |
| 35 | + code = ( |
| 36 | + code_execution_utils.CodeExecutionUtils.extract_code_and_truncate_content( |
| 37 | + content, delimiters |
| 38 | + ) |
| 39 | + ) |
| 40 | + assert code == "x = 1" |
| 41 | + assert len(content.parts) == 2 |
| 42 | + assert content.parts[0].text == "Here is some code:\n" |
| 43 | + assert content.parts[1].executable_code.code == "x = 1" |
| 44 | + |
| 45 | + |
| 46 | +def test_extract_code_and_truncate_content_multiple_blocks(): |
| 47 | + """Tests that the first code block is extracted when multiple exist.""" |
| 48 | + content = types.Content( |
| 49 | + role="model", |
| 50 | + parts=[ |
| 51 | + types.Part( |
| 52 | + text=( |
| 53 | + "First:\n" |
| 54 | + "```python\n" |
| 55 | + "x = 1\n" |
| 56 | + "```\n" |
| 57 | + "Second:\n" |
| 58 | + "```python\n" |
| 59 | + "y = 2\n" |
| 60 | + "```" |
| 61 | + ) |
| 62 | + ) |
| 63 | + ], |
| 64 | + ) |
| 65 | + delimiters = [("```python\n", "\n```")] |
| 66 | + code = ( |
| 67 | + code_execution_utils.CodeExecutionUtils.extract_code_and_truncate_content( |
| 68 | + content, delimiters |
| 69 | + ) |
| 70 | + ) |
| 71 | + assert code == "x = 1" |
| 72 | + assert len(content.parts) == 2 |
| 73 | + assert content.parts[0].text == "First:\n" |
| 74 | + assert content.parts[1].executable_code.code == "x = 1" |
| 75 | + |
| 76 | + |
| 77 | +def test_extract_code_and_truncate_content_no_delimiter(): |
| 78 | + """Tests when no delimiters are found in the content.""" |
| 79 | + content = types.Content( |
| 80 | + role="model", |
| 81 | + parts=[types.Part(text="Just plain text without code.")], |
| 82 | + ) |
| 83 | + delimiters = [("```python\n", "\n```")] |
| 84 | + code = ( |
| 85 | + code_execution_utils.CodeExecutionUtils.extract_code_and_truncate_content( |
| 86 | + content, delimiters |
| 87 | + ) |
| 88 | + ) |
| 89 | + assert code is None |
| 90 | + # Content should be unmodified. |
| 91 | + assert len(content.parts) == 1 |
| 92 | + assert content.parts[0].text == "Just plain text without code." |
| 93 | + |
| 94 | + |
| 95 | +def test_extract_code_and_truncate_content_redos_vulnerability(): |
| 96 | + """Tests that a string that would cause ReDoS behaves reasonably.""" |
| 97 | + # Construct a long string that contains repeating patterns without matching delimiters. |
| 98 | + # The old regex pattern would backtrack exponentially. |
| 99 | + ticks = "`" * 3 |
| 100 | + long_invalid_payload = ticks + "python\n" + "x = 1\n" * 5000 + "not_matching" |
| 101 | + content = types.Content( |
| 102 | + role="model", |
| 103 | + parts=[types.Part(text=long_invalid_payload)], |
| 104 | + ) |
| 105 | + delimiters = [(ticks + "python\n", "\n" + ticks)] |
| 106 | + |
| 107 | + def handler(_signum, _frame): |
| 108 | + raise TimeoutError("Test timed out (possible ReDoS regression)") |
| 109 | + |
| 110 | + signal.signal(signal.SIGALRM, handler) |
| 111 | + signal.alarm(2) |
| 112 | + try: |
| 113 | + # If ReDoS vulnerability exists, this call will hang or take a very long time. |
| 114 | + code = code_execution_utils.CodeExecutionUtils.extract_code_and_truncate_content( |
| 115 | + content, delimiters |
| 116 | + ) |
| 117 | + finally: |
| 118 | + signal.alarm(0) |
| 119 | + assert code is None |
| 120 | + |
| 121 | + |
| 122 | +def test_extract_code_and_truncate_content_multiple_delimiter_pairs(): |
| 123 | + """Tests code extraction when multiple different delimiter pairs are provided.""" |
| 124 | + ticks = "`" * 3 |
| 125 | + # Case 1: First delimiter pair matches first |
| 126 | + content = types.Content( |
| 127 | + role="model", |
| 128 | + parts=[ |
| 129 | + types.Part( |
| 130 | + text="Here is tool code:\n" |
| 131 | + + ticks |
| 132 | + + "tool_code\nx = 1\n" |
| 133 | + + ticks |
| 134 | + + "\nAnd python code:\n" |
| 135 | + + ticks |
| 136 | + + "python\ny = 2\n" |
| 137 | + + ticks |
| 138 | + ) |
| 139 | + ], |
| 140 | + ) |
| 141 | + delimiters = [ |
| 142 | + (ticks + "tool_code\n", "\n" + ticks), |
| 143 | + (ticks + "python\n", "\n" + ticks), |
| 144 | + ] |
| 145 | + code = ( |
| 146 | + code_execution_utils.CodeExecutionUtils.extract_code_and_truncate_content( |
| 147 | + content, delimiters |
| 148 | + ) |
| 149 | + ) |
| 150 | + assert code == "x = 1" |
| 151 | + assert len(content.parts) == 2 |
| 152 | + assert content.parts[0].text == "Here is tool code:\n" |
| 153 | + assert content.parts[1].executable_code.code == "x = 1" |
| 154 | + |
| 155 | + # Case 2: Second delimiter pair matches first |
| 156 | + content = types.Content( |
| 157 | + role="model", |
| 158 | + parts=[ |
| 159 | + types.Part( |
| 160 | + text="Here is python code:\n" |
| 161 | + + ticks |
| 162 | + + "python\ny = 2\n" |
| 163 | + + ticks |
| 164 | + + "\nAnd tool code:\n" |
| 165 | + + ticks |
| 166 | + + "tool_code\nx = 1\n" |
| 167 | + + ticks |
| 168 | + ) |
| 169 | + ], |
| 170 | + ) |
| 171 | + code = ( |
| 172 | + code_execution_utils.CodeExecutionUtils.extract_code_and_truncate_content( |
| 173 | + content, delimiters |
| 174 | + ) |
| 175 | + ) |
| 176 | + assert code == "y = 2" |
| 177 | + assert len(content.parts) == 2 |
| 178 | + assert content.parts[0].text == "Here is python code:\n" |
| 179 | + assert content.parts[1].executable_code.code == "y = 2" |
0 commit comments