|
| 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 | +"""Path-traversal containment tests for Agent Builder file tools.""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import os |
| 20 | +from pathlib import Path |
| 21 | +from unittest import mock |
| 22 | + |
| 23 | +from google.adk.cli.built_in_agents.tools.delete_files import delete_files |
| 24 | +from google.adk.cli.built_in_agents.tools.read_files import read_files |
| 25 | +from google.adk.cli.built_in_agents.tools.write_files import write_files |
| 26 | +from google.adk.cli.built_in_agents.utils.resolve_root_directory import resolve_file_path |
| 27 | +import pytest |
| 28 | + |
| 29 | + |
| 30 | +def _tool_context(root: Path) -> mock.MagicMock: |
| 31 | + tool_context = mock.MagicMock() |
| 32 | + tool_context._invocation_context.session.state = {"root_directory": str(root)} |
| 33 | + return tool_context |
| 34 | + |
| 35 | + |
| 36 | +def test_resolve_file_path_allows_path_within_root(tmp_path): |
| 37 | + resolved = resolve_file_path( |
| 38 | + "sub/dir/file.txt", {"root_directory": str(tmp_path)} |
| 39 | + ) |
| 40 | + assert resolved == (tmp_path / "sub" / "dir" / "file.txt").resolve() |
| 41 | + |
| 42 | + |
| 43 | +def test_resolve_file_path_allows_dot(tmp_path): |
| 44 | + resolved = resolve_file_path(".", {"root_directory": str(tmp_path)}) |
| 45 | + assert resolved == tmp_path.resolve() |
| 46 | + |
| 47 | + |
| 48 | +def test_resolve_file_path_allows_interior_dotdot_within_root(tmp_path): |
| 49 | + resolved = resolve_file_path( |
| 50 | + "sub/../file.txt", {"root_directory": str(tmp_path)} |
| 51 | + ) |
| 52 | + assert resolved == (tmp_path / "file.txt").resolve() |
| 53 | + |
| 54 | + |
| 55 | +def test_resolve_file_path_allows_absolute_within_root(tmp_path): |
| 56 | + target = tmp_path / "nested" / "ok.txt" |
| 57 | + resolved = resolve_file_path(str(target), {"root_directory": str(tmp_path)}) |
| 58 | + assert resolved == target.resolve() |
| 59 | + |
| 60 | + |
| 61 | +def test_resolve_file_path_rejects_relative_traversal(tmp_path): |
| 62 | + with pytest.raises(ValueError): |
| 63 | + resolve_file_path("../../escape.txt", {"root_directory": str(tmp_path)}) |
| 64 | + |
| 65 | + |
| 66 | +def test_resolve_file_path_rejects_absolute_outside_root(tmp_path): |
| 67 | + with pytest.raises(ValueError): |
| 68 | + resolve_file_path("/etc/passwd", {"root_directory": str(tmp_path)}) |
| 69 | + |
| 70 | + |
| 71 | +async def test_write_files_blocks_relative_traversal( |
| 72 | + tmp_path, tmp_path_factory |
| 73 | +): |
| 74 | + outside = tmp_path_factory.mktemp("outside") |
| 75 | + payload = os.path.relpath(outside / "pwned.txt", tmp_path) |
| 76 | + |
| 77 | + result = await write_files( |
| 78 | + files={payload: "PWNED"}, tool_context=_tool_context(tmp_path) |
| 79 | + ) |
| 80 | + |
| 81 | + assert not result["success"] |
| 82 | + assert not (outside / "pwned.txt").exists() |
| 83 | + |
| 84 | + |
| 85 | +async def test_write_files_blocks_absolute_outside_root( |
| 86 | + tmp_path, tmp_path_factory |
| 87 | +): |
| 88 | + outside = tmp_path_factory.mktemp("outside") |
| 89 | + target = outside / "abs.txt" |
| 90 | + |
| 91 | + result = await write_files( |
| 92 | + files={str(target): "PWNED"}, tool_context=_tool_context(tmp_path) |
| 93 | + ) |
| 94 | + |
| 95 | + assert not result["success"] |
| 96 | + assert not target.exists() |
| 97 | + |
| 98 | + |
| 99 | +async def test_write_files_allows_path_within_root(tmp_path): |
| 100 | + result = await write_files( |
| 101 | + files={"sub/ok.txt": "hello"}, tool_context=_tool_context(tmp_path) |
| 102 | + ) |
| 103 | + |
| 104 | + assert result["success"] |
| 105 | + assert (tmp_path / "sub" / "ok.txt").read_text() == "hello" |
| 106 | + |
| 107 | + |
| 108 | +async def test_read_files_blocks_relative_traversal(tmp_path, tmp_path_factory): |
| 109 | + outside = tmp_path_factory.mktemp("outside") |
| 110 | + secret = outside / "secret.txt" |
| 111 | + secret.write_text("TOKEN=abc") |
| 112 | + payload = os.path.relpath(secret, tmp_path) |
| 113 | + |
| 114 | + result = await read_files( |
| 115 | + file_paths=[payload], tool_context=_tool_context(tmp_path) |
| 116 | + ) |
| 117 | + |
| 118 | + assert not result["success"] |
| 119 | + assert all( |
| 120 | + "TOKEN=abc" not in info.get("content", "") |
| 121 | + for info in result["files"].values() |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +async def test_delete_files_blocks_relative_traversal( |
| 126 | + tmp_path, tmp_path_factory |
| 127 | +): |
| 128 | + outside = tmp_path_factory.mktemp("outside") |
| 129 | + victim = outside / "victim.txt" |
| 130 | + victim.write_text("bye") |
| 131 | + payload = os.path.relpath(victim, tmp_path) |
| 132 | + |
| 133 | + result = await delete_files( |
| 134 | + file_paths=[payload], |
| 135 | + tool_context=_tool_context(tmp_path), |
| 136 | + confirm_deletion=True, |
| 137 | + ) |
| 138 | + |
| 139 | + assert not result["success"] |
| 140 | + assert victim.exists() |
0 commit comments