lc-tools 0.25.0

Built-in tools for langchainrust — Calculator, DateTime, URLFetch, etc.
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
// lc-tools/src/sandbox/local.rs
//! Local process sandbox backend.

use std::sync::LazyLock;
use std::time::Instant;

use async_trait::async_trait;
use regex::Regex;
use tokio::process::Command;

use super::{CodeSandbox, Language, RunResult, SandboxError};

/// Dangerous Python modules matched by a noise-filter substring blacklist.
///
/// Note: this is a *noise filter*, **not a security boundary** — a substring scan is
/// trivially bypassed (`__import__("o" + "s")`, `import os;` variants, exec, ...).
/// Untrusted code must not be run through [`LocalSandbox`] at all unless the process
/// itself sits inside a real sandbox (container / VM / WASM).
///
/// The JS branch carries an equivalent noise filter ([`BLOCKED_JS_MODULES`] +
/// [`BLOCKED_JS_CALL_REGEX`], H1) so it is not left completely open where Python has a guard.
const BLOCKED_PYTHON_IMPORTS: &[&str] = &[
    "os",
    "subprocess",
    "sys",
    "shutil",
    "signal",
    "ctypes",
    "multiprocessing",
    "socket",
    "http.server",
    "xmlrpc",
    "pickle",
    "shelve",
    "importlib",
    "code",
    "codeop",
    "compileall",
    "pty",
    "commands",
    "pdb",
    "webbrowser",
];

/// Check if Python code contains dangerous imports.
fn contains_dangerous_python_import(code: &str) -> Option<String> {
    for line in code.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with('#') {
            continue;
        }
        let code_part = trimmed.split('#').next().unwrap_or(trimmed);
        if code_part.contains("import") {
            for blocked in BLOCKED_PYTHON_IMPORTS {
                if code_part.contains(&format!("import {}", blocked))
                    || code_part.contains(&format!("from {} ", blocked))
                    || code_part.contains(&format!("from {}.", blocked))
                    || code_part.contains(&format!("from {}import", blocked))
                {
                    return Some(blocked.to_string());
                }
            }
        }
    }
    None
}

/// Dangerous Node.js modules for the JS branch, mirroring [`BLOCKED_PYTHON_IMPORTS`].
///
/// H1 (0.25.0): the JS branch previously executed `node -e <code>` with *no* guard while Python
/// had at least the noise-filter blacklist — strictly worse, and it made the "sandbox" name even
/// more misleading for a local raw process. This is the same **noise filter, not a security
/// boundary**: a substring scan is trivially bypassed (`require('child'+'process')`,
/// `globalThis['fs']`, unicode obfuscation...). Untrusted code must not run through
/// [`LocalSandbox`] unless the process itself sits inside a real sandbox (container / VM / WASM).
const BLOCKED_JS_MODULES: &[&str] = &[
    "fs",
    "child_process",
    "net",
    "dgram",
    "tls",
    "http",
    "https",
    "http2",
    "worker_threads",
    "vm",
    "cluster",
    "repl",
    "readline",
    "os",
];

/// Common dangerous Node.js **calls** not gated by an import: `process.env`/`process.exit`, the
/// `eval`/`Function` constructors, `globalThis` dynamic reach, and `spawn`/`exec` (which a
/// `node -e` one-liner can also reach via `child_process`, already blocked above). These are the
/// JS analogues of [`BLOCKED_PYTHON_IMPORTS`]/builtin calls — word-boundary guarded so ordinary
/// identifiers like `evaluate(`/`spawner(` do not false-positive.
static BLOCKED_JS_CALL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"\b(?:process\.|eval\s*\(|Function\s*\(|globalThis|spawn\s*\(|exec\s*\()")
        .expect("static JS regex literal must compile")
});

/// Check if JavaScript code reaches dangerous Node.js APIs (noise-filter, see above).
fn contains_dangerous_javascript(code: &str) -> Option<String> {
    for line in code.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("//") {
            continue;
        }
        let code_part = trimmed.split("//").next().unwrap_or(trimmed);
        // require('<module>') / require("<module>") for the blocked modules.
        for m in BLOCKED_JS_MODULES {
            if code_part.contains(&format!("require('{}')", m))
                || code_part.contains(&format!("require(\"{}\")", m))
            {
                return Some(format!("require('{}')", m));
            }
        }
        // statement-level dangerous access: process.* / eval( / Function( / globalThis / spawn / exec.
        if let Some(m) = BLOCKED_JS_CALL_REGEX.find(code_part) {
            return Some(m.as_str().to_string());
        }
    }
    None
}

/// Local process sandbox using `tokio::process::Command`.
pub struct LocalSandbox {
    python_path: String,
    node_path: String,
}

impl LocalSandbox {
    /// Create a new local sandbox with auto-detected interpreter paths.
    pub fn new() -> Self {
        Self {
            python_path: Self::find_python(),
            node_path: "node".to_string(),
        }
    }

    /// Use a custom Python interpreter path.
    pub fn with_python_path(mut self, path: impl Into<String>) -> Self {
        self.python_path = path.into();
        self
    }

    /// Use a custom Node.js runtime path.
    pub fn with_node_path(mut self, path: impl Into<String>) -> Self {
        self.node_path = path.into();
        self
    }

    /// Auto-detect the Python interpreter on the system.
    fn find_python() -> String {
        for candidate in &["python3", "python"] {
            if std::process::Command::new(candidate)
                .arg("--version")
                .output()
                .is_ok()
            {
                return candidate.to_string();
            }
        }
        "python3".to_string()
    }

    /// Build the command for the given language and code.
    fn build_command(&self, code: &str, language: Language) -> Result<Command, SandboxError> {
        match language {
            Language::Python => {
                if let Some(blocked) = contains_dangerous_python_import(code) {
                    return Err(SandboxError::Runtime(format!(
                        "Code contains dangerous import: '{}'. \
                         Blocked by the noise-filter blacklist (note: this is not a \
                         security boundary; untrusted code must run in a real sandbox).",
                        blocked
                    )));
                }
                let mut cmd = Command::new(&self.python_path);
                cmd.arg("-c").arg(code);
                Ok(cmd)
            }
            Language::JavaScript => {
                if let Some(dangerous) = contains_dangerous_javascript(code) {
                    return Err(SandboxError::Runtime(format!(
                        "Code contains dangerous Node.js API: '{}'. \
                         Blocked by the noise-filter blacklist (note: this is not a \
                         security boundary; untrusted code must run in a real sandbox).",
                        dangerous
                    )));
                }
                let mut cmd = Command::new(&self.node_path);
                cmd.arg("-e").arg(code);
                Ok(cmd)
            }
            Language::Rust => Err(SandboxError::UnsupportedLanguage(
                "Rust compilation is not supported by LocalSandbox".to_string(),
            )),
        }
    }
}

impl Default for LocalSandbox {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl CodeSandbox for LocalSandbox {
    async fn run(
        &self,
        code: &str,
        language: Language,
        timeout_ms: u64,
    ) -> Result<RunResult, SandboxError> {
        let mut cmd = self.build_command(code, language)?;

        let start = Instant::now();

        let result =
            tokio::time::timeout(std::time::Duration::from_millis(timeout_ms), cmd.output())
                .await
                .map_err(|_| SandboxError::Timeout(timeout_ms))?
                .map_err(|e| {
                    SandboxError::Runtime(format!("failed to execute subprocess: {}", e))
                })?;

        let execution_time_ms = start.elapsed().as_millis() as u64;

        let stdout = String::from_utf8_lossy(&result.stdout).to_string();
        let stderr = String::from_utf8_lossy(&result.stderr).to_string();
        let exit_code = result.status.code().unwrap_or(-1);

        Ok(RunResult {
            stdout,
            stderr,
            exit_code,
            execution_time_ms,
        })
    }
}

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

    #[test]
    fn test_local_sandbox_default() {
        let sandbox = LocalSandbox::default();
        assert!(!sandbox.python_path.is_empty());
        assert_eq!(sandbox.node_path, "node");
    }

    #[test]
    fn test_local_sandbox_custom_paths() {
        let sandbox = LocalSandbox::new()
            .with_python_path("/usr/bin/python3.11")
            .with_node_path("/usr/local/bin/node");
        assert_eq!(sandbox.python_path, "/usr/bin/python3.11");
        assert_eq!(sandbox.node_path, "/usr/local/bin/node");
    }

    #[test]
    fn test_rust_unsupported() {
        let sandbox = LocalSandbox::new();
        let result = sandbox.build_command("fn main(){}", Language::Rust);
        assert!(result.is_err());
        match result.unwrap_err() {
            SandboxError::UnsupportedLanguage(msg) => {
                assert!(msg.contains("Rust"));
            }
            other => panic!("expected UnsupportedLanguage, got: {:?}", other),
        }
    }

    #[test]
    fn test_dangerous_python_import_detection() {
        assert!(contains_dangerous_python_import("import os").is_some());
        assert!(contains_dangerous_python_import("from sys import path").is_some());
        assert!(contains_dangerous_python_import("import subprocess").is_some());
        assert!(contains_dangerous_python_import("import math").is_none());
        assert!(contains_dangerous_python_import("import json").is_none());
        assert!(contains_dangerous_python_import("from datetime import datetime").is_none());
        assert!(contains_dangerous_python_import("# import os").is_none());
    }

    #[test]
    fn test_dangerous_javascript_detection() {
        // module requires (H1)
        assert!(contains_dangerous_javascript("require('fs').readFileSync('/etc/passwd')").is_some());
        assert!(contains_dangerous_javascript("require(\"child_process\")").is_some());
        assert!(contains_dangerous_javascript("const net = require('net')").is_some());
        // statement-level dangerous calls
        assert!(contains_dangerous_javascript("process.env").is_some());
        assert!(contains_dangerous_javascript("eval('console.log(1)')").is_some());
        assert!(contains_dangerous_javascript("new Function('x', 'return x')").is_some());
        assert!(contains_dangerous_javascript("globalThis.foo = 1").is_some());
        // benign code and comment lines pass
        assert!(contains_dangerous_javascript("console.log(1 + 2)").is_none());
        assert!(contains_dangerous_javascript("JSON.parse(x).map(v => v * 2)").is_none());
        assert!(contains_dangerous_javascript("// require('fs')").is_none());
        assert!(contains_dangerous_javascript("evaluate(fn).execute()").is_none());
    }

    #[tokio::test]
    async fn test_javascript_dangerous_api_blocked() {
        let sandbox = LocalSandbox::new();
        let result = sandbox
            .run(
                "require('fs').readFileSync('/etc/passwd')",
                Language::JavaScript,
                10_000,
            )
            .await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("dangerous Node.js API"),
            "Expected dangerous Node.js API error, got: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_python_dangerous_import_blocked() {
        let sandbox = LocalSandbox::new();
        let result = sandbox
            .run("import os; print(os.getcwd())", Language::Python, 10_000)
            .await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("dangerous import"),
            "Expected dangerous import error, got: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_python_execution_if_available() {
        let sandbox = LocalSandbox::new();
        let result = sandbox.run("print(1 + 2)", Language::Python, 10_000).await;

        match result {
            Ok(run_result) => {
                if run_result.exit_code == 0 {
                    assert!(
                        run_result.stdout.trim() == "3",
                        "expected '3', got '{}'",
                        run_result.stdout.trim()
                    );
                }
            }
            Err(SandboxError::Runtime(msg)) => {
                eprintln!("Python not available (expected in some CI): {}", msg);
            }
            Err(other) => panic!("unexpected error: {:?}", other),
        }
    }

    #[tokio::test]
    async fn test_javascript_execution_if_available() {
        let sandbox = LocalSandbox::new();
        let result = sandbox
            .run("console.log(1 + 2)", Language::JavaScript, 10_000)
            .await;

        match result {
            Ok(run_result) => {
                if run_result.exit_code == 0 {
                    assert!(
                        run_result.stdout.trim() == "3",
                        "expected '3', got '{}'",
                        run_result.stdout.trim()
                    );
                }
            }
            Err(SandboxError::Runtime(msg)) => {
                eprintln!("Node.js not available (expected in some CI): {}", msg);
            }
            // Warm Windows runners occasionally let `node -e` blow the 10s
            // budget (Defender scan / host load); this is an "if available"
            // smoke test, so treat an environmental timeout like no runtime
            // rather than a code regression (observed 2026-09-14, passed on
            // the same code the two previous runs).
            Err(SandboxError::Timeout(ms)) => {
                eprintln!("Node.js present but too slow in this CI environment ({ms}ms)");
            }
            Err(other) => panic!("unexpected error: {:?}", other),
        }
    }

    #[tokio::test]
    async fn test_rust_execution_unsupported() {
        let sandbox = LocalSandbox::new();
        let result = sandbox.run("fn main(){}", Language::Rust, 10_000).await;
        assert!(result.is_err());
        match result.unwrap_err() {
            SandboxError::UnsupportedLanguage(_) => {}
            other => panic!("expected UnsupportedLanguage, got: {:?}", other),
        }
    }

    #[tokio::test]
    async fn test_execution_timeout() {
        let sandbox = LocalSandbox::new();
        let result = sandbox
            .run("import time; time.sleep(10)", Language::Python, 100)
            .await;

        match result {
            Err(SandboxError::Timeout(ms)) => {
                assert_eq!(ms, 100);
            }
            Ok(_) => {
                // Python not available, code didn't run — acceptable
            }
            Err(other) => panic!("expected Timeout, got: {:?}", other),
        }
    }

    #[tokio::test]
    async fn test_execution_time_is_recorded() {
        let sandbox = LocalSandbox::new();
        let result = sandbox.run("print('hi')", Language::Python, 10_000).await;

        if let Ok(run_result) = result {
            assert!(run_result.execution_time_ms < 10_000);
        }
    }

    #[tokio::test]
    async fn test_stderr_captured() {
        let sandbox = LocalSandbox::new();
        let result = sandbox
            .run(
                "import sys; print('error', file=sys.stderr)",
                Language::Python,
                10_000,
            )
            .await;

        if let Ok(run_result) = result {
            if run_result.exit_code == 0 {
                assert!(
                    run_result.stderr.contains("error"),
                    "stderr should contain 'error', got: '{}'",
                    run_result.stderr
                );
            }
        }
    }
}