zeptoclaw 0.9.2

Ultra-lightweight personal AI assistant
Documentation
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Browser tool — headless web browsing via agent-browser + Lightpanda.
//!
//! Wraps the `agent-browser` CLI to provide full browser automation:
//! navigation, content extraction, form filling, clicking, screenshots, etc.
//! Uses Lightpanda as the default engine (10x faster, 10x less memory than Chrome).
//! Falls back to Chrome automatically if Lightpanda fails on navigation.

use async_trait::async_trait;
use reqwest::Url;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::sync::Mutex;
use std::time::Duration;

use crate::config::BrowserConfig;
use crate::deps::{DepKind, Dependency, HasDependencies, HealthCheck};
use crate::error::{Result, ZeptoError};

use super::web::{is_blocked_host, resolve_and_check_host, validate_redirect_target_basic};
use super::{Tool, ToolCategory, ToolContext, ToolOutput};

const ENGINE_LIGHTPANDA: &str = "lightpanda";
const ENGINE_CHROME: &str = "chrome";

/// Browser automation tool wrapping the `agent-browser` CLI.
///
/// **Single-tenant**: engine state (`active_engine`) is shared across all
/// invocations of this tool instance. If multi-tenant isolation is needed,
/// engine state should move into `ToolContext` per conversation.
pub struct BrowserTool {
    default_engine: String,
    active_engine: Mutex<String>,
    executable: String,
    timeout_secs: u64,
}

impl BrowserTool {
    pub fn new(config: &BrowserConfig) -> Self {
        let engine = config.engine.clone();
        Self {
            active_engine: Mutex::new(engine.clone()),
            default_engine: engine,
            executable: config
                .executable_path
                .clone()
                .unwrap_or_else(|| "agent-browser".to_string()),
            timeout_secs: config.timeout_secs,
        }
    }

    /// Run an agent-browser command with a specific engine and return its stdout.
    async fn run_command_with_engine(
        &self,
        command: &str,
        args: &[&str],
        engine: &str,
    ) -> Result<String> {
        let mut cmd = tokio::process::Command::new(&self.executable);
        cmd.arg(command);
        cmd.args(args);
        cmd.env("AGENT_BROWSER_ENGINE", engine);
        cmd.env("LIGHTPANDA_DISABLE_TELEMETRY", "true");
        cmd.stdout(std::process::Stdio::piped());
        cmd.stderr(std::process::Stdio::piped());
        cmd.kill_on_drop(true);

        let child = cmd.spawn().map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                ZeptoError::Tool(format!(
                    "'{}' not found. Install it with:\n  \
                     npm install -g agent-browser   # or: brew install agent-browser\n  \
                     agent-browser install           # downloads Chrome\n\
                     For LightPanda (optional, faster): see https://agent-browser.dev/engines/lightpanda",
                    self.executable
                ))
            } else {
                ZeptoError::Tool(format!("Failed to run agent-browser: {}", e))
            }
        })?;

        let timeout = Duration::from_secs(self.timeout_secs);
        let output = tokio::time::timeout(timeout, child.wait_with_output())
            .await
            .map_err(|_| {
                ZeptoError::Tool(format!(
                    "Browser command '{}' timed out after {}s",
                    command, self.timeout_secs
                ))
            })?
            .map_err(|e| ZeptoError::Tool(format!("Failed to run agent-browser: {}", e)))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            let msg = if stderr.is_empty() {
                String::from_utf8_lossy(&output.stdout)
            } else {
                stderr
            };
            return Err(ZeptoError::Tool(format!(
                "agent-browser {} failed: {}",
                command,
                msg.trim()
            )));
        }

        Ok(String::from_utf8_lossy(&output.stdout).to_string())
    }

    fn get_active_engine(&self) -> String {
        self.active_engine
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .clone()
    }

    fn set_active_engine(&self, engine: &str) {
        *self.active_engine.lock().unwrap_or_else(|e| e.into_inner()) = engine.to_string();
    }

    /// Validate a URL against SSRF blocklist (scheme + host + DNS resolution).
    async fn check_url(url_str: &str) -> Result<()> {
        let parsed = Url::parse(url_str)
            .map_err(|e| ZeptoError::Tool(format!("Invalid URL '{}': {}", url_str, e)))?;
        validate_redirect_target_basic(&parsed)?;
        // DNS-aware check: resolve hostname and verify resolved IPs aren't private.
        // Catches attacks like metadata.attacker.com → 169.254.169.254.
        resolve_and_check_host(&parsed).await?;
        Ok(())
    }

    /// Post-navigation SSRF check: verify the final URL isn't a private/local address
    /// (catches redirect-based SSRF). Fails closed on unparseable URLs.
    async fn check_final_url(&self, engine: &str) -> Result<()> {
        let final_url = self
            .run_command_with_engine("get", &["url"], engine)
            .await?;
        let final_url = final_url.trim();

        if final_url.is_empty() {
            return Ok(());
        }

        let parsed = match Url::parse(final_url) {
            Ok(u) => u,
            Err(_) => {
                if let Err(e) = self.run_command_with_engine("close", &[], engine).await {
                    tracing::warn!("Failed to close browser after SSRF check: {}", e);
                }
                return Err(ZeptoError::SecurityViolation(format!(
                    "Navigation resulted in unparseable URL: {}",
                    final_url
                )));
            }
        };

        if is_blocked_host(&parsed) {
            if let Err(e) = self.run_command_with_engine("close", &[], engine).await {
                tracing::warn!("Failed to close browser after SSRF block: {}", e);
            }
            return Err(ZeptoError::SecurityViolation(format!(
                "Navigation redirected to blocked host: {}",
                final_url
            )));
        }

        Ok(())
    }

    /// Build a concise one-liner for the user, or None to show nothing.
    /// The model always gets the full output regardless.
    fn summarize_for_user(command: &str, args_str: &str) -> Option<String> {
        match command {
            "open" => {
                let url = args_str.split_whitespace().next().unwrap_or(args_str);
                Some(format!("Browsing {}", url))
            }
            "screenshot" => Some("Screenshot captured".to_string()),
            _ => None,
        }
    }
}

impl HasDependencies for BrowserTool {
    fn dependencies(&self) -> Vec<Dependency> {
        vec![
            Dependency {
                name: "agent-browser".to_string(),
                kind: DepKind::NpmPackage {
                    package: "agent-browser".to_string(),
                    version: "latest".to_string(),
                    entry_point: "agent-browser".to_string(),
                },
                health_check: HealthCheck::Command {
                    command: "agent-browser --version".to_string(),
                },
                env: HashMap::new(),
                args: vec![],
            },
            Dependency {
                name: "lightpanda".to_string(),
                kind: DepKind::Binary {
                    repo: "lightpanda-io/browser".to_string(),
                    asset_pattern: "lightpanda-{arch}-{os}".to_string(),
                    version: "nightly".to_string(),
                },
                health_check: HealthCheck::Command {
                    command: "lightpanda --version".to_string(),
                },
                env: HashMap::new(),
                args: vec![],
            },
        ]
    }
}

#[async_trait]
impl Tool for BrowserTool {
    fn name(&self) -> &str {
        "browser"
    }

    fn description(&self) -> &str {
        "Browse the web: fetch page content, read articles, interact with websites. \
         Use this tool whenever you need to visit a URL or retrieve web content. \
         Typical flow: open <url>, then snapshot to read the page. \
         Commands: open <url> (navigate to page), snapshot (read page content with element refs), \
         click <ref> (click element), fill <ref> <text> (type into input), \
         find role|text|label <query> (find elements), get text|html|url|title, \
         scroll up|down, back, forward, screenshot [path], wait <selector|ms>. \
         Element refs like @e1 are assigned by snapshot and reused for interaction. \
         Optional engine param: set to 'chrome' for full rendering fidelity when the user \
         requests Chrome. Defaults to lightpanda (faster). Falls back to Chrome automatically \
         if lightpanda fails."
    }

    fn compact_description(&self) -> &str {
        "Browse web"
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::Shell
    }

    fn parameters(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "command": {
                    "type": "string",
                    "description": "The agent-browser command to run (e.g. open, snapshot, click, fill, find, get, scroll, back, screenshot, wait, close)"
                },
                "args": {
                    "type": "string",
                    "description": "Arguments for the command (e.g. a URL for open, a ref like @e1 for click, 'text hello' for find)"
                },
                "engine": {
                    "type": "string",
                    "enum": ["lightpanda", "chrome"],
                    "description": "Browser engine override. Use 'chrome' for full rendering fidelity when requested by the user."
                }
            },
            "required": ["command"]
        })
    }

    async fn execute(&self, args: Value, _ctx: &ToolContext) -> Result<ToolOutput> {
        let command = args
            .get("command")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ZeptoError::Tool("Missing 'command' argument".into()))?;

        let args_str = args.get("args").and_then(|v| v.as_str()).unwrap_or("");
        let engine_override = args.get("engine").and_then(|v| v.as_str());

        // The JSON schema declares an enum for `engine`, but schemas are
        // advisory — the LLM can produce arbitrary values. Enforce an
        // allowlist so the engine string can never reach the
        // AGENT_BROWSER_ENGINE env var as something unexpected.
        if let Some(ov) = engine_override {
            if ov != ENGINE_LIGHTPANDA && ov != ENGINE_CHROME {
                return Err(ZeptoError::Tool(format!(
                    "Invalid engine '{}': must be '{}' or '{}'",
                    ov, ENGINE_LIGHTPANDA, ENGINE_CHROME
                )));
            }
        }

        let is_navigation = command == "open";

        // SSRF check: validate URLs in all commands that accept them.
        // "open" always has a URL; other commands may contain URLs in args.
        let url_to_check = if is_navigation {
            let url = args_str.split_whitespace().next().unwrap_or(args_str);
            if url.is_empty() {
                return Err(ZeptoError::Tool(format!(
                    "'{}' command requires a URL argument",
                    command
                )));
            }
            Some(url.to_string())
        } else {
            // Check for URLs in args of other commands that can accept them
            // (tab new <url>, connect <url>, network route <url>).
            // Also catch any arg that looks like a URL to be safe.
            args_str
                .split_whitespace()
                .find(|arg| arg.starts_with("http://") || arg.starts_with("https://"))
                .map(|s| s.to_string())
        };

        if let Some(ref url) = url_to_check {
            Self::check_url(url).await?;
        }

        if command == "close" {
            let engine = self.get_active_engine();
            let output = self.run_command_with_engine(command, &[], &engine).await?;
            self.set_active_engine(&self.default_engine);
            return Ok(ToolOutput::llm_only(output));
        }

        // Resolve engine: explicit override > active session engine
        let engine = if let Some(ov) = engine_override {
            self.set_active_engine(ov);
            ov.to_string()
        } else {
            self.get_active_engine()
        };

        // Build CLI args. Most commands take discrete tokens that can be split on
        // whitespace. However, `fill`/`type` take `<selector> <text>` where the text
        // portion may contain spaces, `keyboard` takes `<subcommand> <text>`, and
        // `eval` takes a single JS expression. Handle these specially.
        let cmd_args: Vec<&str> = if args_str.is_empty() {
            vec![]
        } else {
            match command {
                // <selector> <text> — split into exactly two args
                "fill" | "type" => match args_str.split_once(char::is_whitespace) {
                    Some((sel, text)) => vec![sel, text.trim_start()],
                    None => vec![args_str],
                },
                // <subcommand> <text> — e.g. "type hello world" or "inserttext hello"
                "keyboard" => match args_str.split_once(char::is_whitespace) {
                    Some((sub, text)) => vec![sub, text.trim_start()],
                    None => vec![args_str],
                },
                // Single expression/arg — don't split
                "eval" => vec![args_str],
                // All other commands: discrete tokens
                _ => args_str.split_whitespace().collect(),
            }
        };

        let (output, engine) = match self
            .run_command_with_engine(command, &cmd_args, &engine)
            .await
        {
            Ok(output) => (output, engine),
            Err(lp_err) if is_navigation && engine == ENGINE_LIGHTPANDA => {
                tracing::warn!(
                    "Lightpanda failed for '{}', falling back to Chrome: {}",
                    command,
                    lp_err
                );
                match self
                    .run_command_with_engine(command, &cmd_args, ENGINE_CHROME)
                    .await
                {
                    Ok(output) => {
                        self.set_active_engine(ENGINE_CHROME);
                        tracing::info!("Chrome fallback succeeded, session switched to Chrome");
                        (output, ENGINE_CHROME.to_string())
                    }
                    Err(chrome_err) => {
                        return Err(ZeptoError::Tool(format!(
                            "Both engines failed. Lightpanda: {}. Chrome: {}",
                            lp_err, chrome_err
                        )));
                    }
                }
            }
            Err(e) => return Err(e),
        };

        // Post-navigation SSRF check (catches redirects).
        //
        // Run the check whenever the command may have caused a navigation,
        // not only when the request body explicitly carried a URL. Commands
        // like `click`, `back`, `forward`, `submit`, and `follow` can land
        // the active page on an internal IP via a redirect chain (e.g.
        // `http://attacker.example/redirect?to=http://169.254.169.254/...`)
        // without ever passing through the pre-flight URL allowlist.
        let may_navigate = matches!(command, "click" | "back" | "forward" | "submit" | "follow");
        if url_to_check.is_some() || may_navigate {
            self.check_final_url(&engine).await?;
        }

        match Self::summarize_for_user(command, args_str) {
            Some(summary) => Ok(ToolOutput::split(output, summary)),
            None => Ok(ToolOutput::llm_only(output)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::BrowserConfig;

    fn make_tool(engine: &str) -> BrowserTool {
        BrowserTool::new(&BrowserConfig {
            enabled: true,
            engine: engine.to_string(),
            executable_path: None,
            timeout_secs: 30,
        })
    }

    #[tokio::test]
    async fn test_check_url_blocks_localhost() {
        assert!(BrowserTool::check_url("http://localhost").await.is_err());
        assert!(BrowserTool::check_url("http://localhost:8080")
            .await
            .is_err());
        assert!(BrowserTool::check_url("http://127.0.0.1").await.is_err());
    }

    #[tokio::test]
    async fn test_check_url_blocks_private_networks() {
        assert!(BrowserTool::check_url("http://192.168.1.1").await.is_err());
        assert!(BrowserTool::check_url("http://10.0.0.1").await.is_err());
        assert!(BrowserTool::check_url("http://172.16.0.1").await.is_err());
    }

    #[tokio::test]
    async fn test_check_url_allows_public() {
        assert!(BrowserTool::check_url("https://example.com").await.is_ok());
        assert!(BrowserTool::check_url("https://google.com").await.is_ok());
    }

    #[tokio::test]
    async fn test_check_url_rejects_non_http() {
        assert!(BrowserTool::check_url("ftp://example.com").await.is_err());
        assert!(BrowserTool::check_url("file:///etc/passwd").await.is_err());
    }

    #[tokio::test]
    async fn test_check_url_rejects_invalid() {
        assert!(BrowserTool::check_url("not a url").await.is_err());
    }

    #[test]
    fn test_default_engine_is_preserved() {
        let tool = make_tool(ENGINE_LIGHTPANDA);
        assert_eq!(tool.default_engine, ENGINE_LIGHTPANDA);
    }

    #[test]
    fn test_engine_override_sets_active_engine() {
        let tool = make_tool(ENGINE_LIGHTPANDA);
        assert_eq!(tool.get_active_engine(), ENGINE_LIGHTPANDA);

        tool.set_active_engine(ENGINE_CHROME);
        assert_eq!(tool.get_active_engine(), ENGINE_CHROME);
    }

    #[test]
    fn test_close_resets_active_engine() {
        let tool = make_tool(ENGINE_LIGHTPANDA);

        tool.set_active_engine(ENGINE_CHROME);
        assert_eq!(tool.get_active_engine(), ENGINE_CHROME);

        // Simulate what close does
        tool.set_active_engine(&tool.default_engine);
        assert_eq!(tool.get_active_engine(), ENGINE_LIGHTPANDA);
    }

    #[test]
    fn test_summarize_open() {
        let summary = BrowserTool::summarize_for_user("open", "https://example.com");
        assert_eq!(summary, Some("Browsing https://example.com".to_string()));
    }

    #[test]
    fn test_summarize_screenshot() {
        let summary = BrowserTool::summarize_for_user("screenshot", "");
        assert_eq!(summary, Some("Screenshot captured".to_string()));
    }

    #[test]
    fn test_summarize_other_commands_are_silent() {
        assert!(BrowserTool::summarize_for_user("click", "@e1").is_none());
        assert!(BrowserTool::summarize_for_user("snapshot", "").is_none());
        assert!(BrowserTool::summarize_for_user("fill", "@e5 hello").is_none());
        assert!(BrowserTool::summarize_for_user("scroll", "down").is_none());
        assert!(BrowserTool::summarize_for_user("close", "").is_none());
    }

    #[test]
    fn test_parameters_include_engine() {
        let tool = make_tool(ENGINE_LIGHTPANDA);
        let params = tool.parameters();
        let engine_prop = &params["properties"]["engine"];
        assert_eq!(engine_prop["type"], "string");
        let enum_values = engine_prop["enum"].as_array().unwrap();
        assert_eq!(enum_values.len(), 2);
        assert!(enum_values.iter().any(|v| v == ENGINE_LIGHTPANDA));
        assert!(enum_values.iter().any(|v| v == ENGINE_CHROME));
    }

    #[tokio::test]
    async fn test_execute_rejects_invalid_engine_override() {
        let tool = make_tool(ENGINE_LIGHTPANDA);
        let ctx = crate::tools::types::ToolContext::new();
        // The JSON schema enum is advisory — execute() must enforce the
        // allowlist itself so the LLM cannot inject arbitrary engine strings
        // into the AGENT_BROWSER_ENGINE env var.
        let err = tool
            .execute(
                serde_json::json!({
                    "command": "open",
                    "args": "https://example.com",
                    "engine": "../../bin/sh"
                }),
                &ctx,
            )
            .await
            .expect_err("invalid engine must be rejected");
        let msg = err.to_string();
        assert!(
            msg.contains("Invalid engine") && msg.contains("../../bin/sh"),
            "Expected invalid engine error, got: {msg}"
        );
    }

    #[tokio::test]
    async fn test_execute_rejects_arbitrary_engine_value() {
        let tool = make_tool(ENGINE_LIGHTPANDA);
        let ctx = crate::tools::types::ToolContext::new();
        let err = tool
            .execute(
                serde_json::json!({
                    "command": "open",
                    "args": "https://example.com",
                    "engine": "firefox"
                }),
                &ctx,
            )
            .await
            .expect_err("non-allowlisted engine must be rejected");
        assert!(err.to_string().contains("Invalid engine"));
    }

    #[test]
    fn test_navigation_commands_trigger_post_nav_check() {
        // Document the allowlist of commands that require post-navigation
        // SSRF re-checks. If a new navigation-causing command is added to
        // the browser, it must also be added here so redirects can't reach
        // internal IPs (e.g. 169.254.169.254 cloud metadata endpoints).
        for cmd in &["click", "back", "forward", "submit", "follow"] {
            let may_navigate = matches!(*cmd, "click" | "back" | "forward" | "submit" | "follow");
            assert!(
                may_navigate,
                "{cmd} should be in the post-navigation SSRF check allowlist"
            );
        }
    }
}