Commit 792775f
feat: add request timeout to load_web_page
Merge #4887
## Link to Issue or Description of Change
Closes #4886
## Update — 2026-04-30
After merging the latest `upstream/main`, the SSRF rewrite already covers URL-scheme validation and routes every fetch failure through a unified `Failed to fetch url` message. The unique contribution of this PR is now the request **timeout**; the body and tests below have been updated to reflect the post-merge scope. No code added by this PR duplicates what upstream already provides.
## Problem
`load_web_page()` calls `requests.get()` without a `timeout`. If the target server is unresponsive, the agent hangs indefinitely.
## Solution
Add `timeout=_DEFAULT_TIMEOUT_SECONDS` (30 seconds) to both HTTP entry points in the module:
- `requests.get` in `_fetch_response` (proxy path).
- `session.get` in `_fetch_direct_response` (pinned-IP path).
Extend the `except` in `load_web_page` to also catch `requests.RequestException`, so timeout and connection errors return the standard `Failed to fetch url: {url}` message instead of propagating.
**Design note:** the timeout is a module-level constant rather than a function parameter to keep it out of the LLM function-calling schema. It can be overridden via `load_web_page._DEFAULT_TIMEOUT_SECONDS = 30` if needed.
## Testing Plan
### Unit Tests
- [x] Added/updated unit tests.
- [x] All unit tests pass locally (`pytest tests/unittests/tools/test_load_web_page.py` → 10 passed).
New/updated tests in `tests/unittests/tools/test_load_web_page.py`:
- `test_load_web_page_uses_proxy_for_unresolved_public_hostnames` — updated to verify `timeout=10` is forwarded on the proxy path.
- `test_load_web_page_passes_timeout_to_pinned_session` — verifies the timeout reaches the pinned-IP session.
- `test_load_web_page_passes_timeout_to_proxied_get` — verifies the timeout is forwarded when a proxy is configured.
- `test_load_web_page_returns_failure_on_timeout` — verifies `requests.exceptions.Timeout` is converted into `Failed to fetch url`.
### Manual E2E
N/A — internal hardening; function signature unchanged.
## Checklist
- [x] I have read the CONTRIBUTING.md document.
- [x] I have performed a self-review of my own code.
- [x] I have added tests that prove my fix is effective.
- [x] New and existing unit tests pass locally with my changes.
## Additional Context
This complements the existing SSRF protection (`allow_redirects=False`, hostname/IP validation, pinned-IP adapter) already present in the module after upstream/main was merged.
Co-authored-by: Bo Yang <ybo@google.com>
COPYBARA_INTEGRATE_REVIEW=#4887 from cchinchilla-dev:feat/load-web-page-timeout-and-url-validation 4bd4799
PiperOrigin-RevId: 9303359771 parent 3e9f3da commit 792775f
2 files changed
Lines changed: 131 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
| |||
230 | 232 | | |
231 | 233 | | |
232 | 234 | | |
| 235 | + | |
233 | 236 | | |
234 | 237 | | |
235 | 238 | | |
| |||
253 | 256 | | |
254 | 257 | | |
255 | 258 | | |
256 | | - | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
257 | 262 | | |
258 | 263 | | |
259 | 264 | | |
| |||
285 | 290 | | |
286 | 291 | | |
287 | 292 | | |
288 | | - | |
| 293 | + | |
289 | 294 | | |
290 | 295 | | |
291 | 296 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
150 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
151 | 153 | | |
152 | 154 | | |
153 | 155 | | |
| |||
272 | 274 | | |
273 | 275 | | |
274 | 276 | | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
0 commit comments