fastskill-core 0.9.112

FastSkill core library - AI Skills management toolkit
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
//! Script execution environment with sandboxing support

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::process::Stdio;
use std::time::Duration;
use tokio::process::Command as TokioCommand;
use tokio::time::timeout;

#[derive(Debug, thiserror::Error)]
pub enum ExecutionError {
    #[error("Execution failed: {0}")]
    Failed(String),

    #[error("Script not found: {0}")]
    ScriptNotFound(String),

    #[error("Invalid script content: {0}")]
    InvalidScript(String),

    #[error("Execution timeout after {0:?}")]
    Timeout(Duration),

    #[error("Resource limit exceeded: {0}")]
    ResourceExceeded(String),

    #[error("Security violation: {0}")]
    SecurityViolation(String),

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
}

/// Execution environment configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionConfig {
    /// Default timeout for script execution
    pub default_timeout: Duration,

    /// Maximum memory per execution (MB)
    pub max_memory_mb: usize,

    /// Network access policy
    pub network_policy: NetworkPolicy,

    /// File system access level
    pub filesystem_access: FileSystemAccess,

    /// Allowed commands for shell execution
    pub allowed_commands: Vec<String>,

    /// Allowed script roots - scripts must be under one of these paths if configured
    pub allowed_script_roots: Vec<PathBuf>,

    /// Environment variables to set
    pub environment_variables: HashMap<String, String>,
}

impl Default for ExecutionConfig {
    fn default() -> Self {
        Self {
            default_timeout: Duration::from_secs(30),
            max_memory_mb: 100,
            network_policy: NetworkPolicy::Restricted {
                allowed_domains: vec![],
            },
            filesystem_access: FileSystemAccess::WorkingDirectory,
            allowed_commands: vec![
                "python".to_string(),
                "python3".to_string(),
                "node".to_string(),
            ],
            allowed_script_roots: Vec::new(),
            environment_variables: HashMap::new(),
        }
    }
}

/// Network access policy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum NetworkPolicy {
    /// No network access allowed
    None,
    /// Only localhost allowed
    Localhost,
    /// Restricted to specific domains
    Restricted { allowed_domains: Vec<String> },
    /// Full network access
    Full,
}

/// File system access levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FileSystemAccess {
    /// No file system access
    None,
    /// Only working directory
    WorkingDirectory,
    /// Read-only access to specific paths
    ReadOnly { paths: Vec<PathBuf> },
    /// Full access (dangerous)
    Full,
}

/// Script definition for execution
#[derive(Debug, Clone)]
pub struct ScriptDefinition {
    /// Path to the script file
    pub path: PathBuf,

    /// Script content (if not reading from file)
    pub content: Option<String>,

    /// Script language/interpreter
    pub language: ScriptLanguage,

    /// Execution parameters
    pub parameters: HashMap<String, String>,

    /// Working directory for execution
    pub working_directory: Option<PathBuf>,
}

/// Supported script languages
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ScriptLanguage {
    Python,
    NodeJS,
    Shell,
    Rust,
}

impl ScriptLanguage {
    #[allow(dead_code)]
    fn get_command(&self) -> &'static str {
        match self {
            ScriptLanguage::Python => "python3",
            ScriptLanguage::NodeJS => "node",
            ScriptLanguage::Shell => "sh",
            ScriptLanguage::Rust => "cargo",
        }
    }
}

/// Execution context
#[derive(Debug, Clone)]
pub struct ExecutionContext {
    /// Skill ID that initiated the execution
    pub skill_id: String,

    /// User ID (if available)
    pub user_id: Option<String>,

    /// Session ID
    pub session_id: String,

    /// Input parameters for the execution
    pub parameters: HashMap<String, String>,

    /// Working directory override
    pub working_directory: Option<PathBuf>,

    /// Environment variables override
    pub environment_variables: HashMap<String, String>,
}

/// Execution result
#[derive(Debug, Clone)]
pub struct ExecutionResult {
    /// Whether execution was successful
    pub success: bool,

    /// Standard output from execution
    pub stdout: String,

    /// Standard error from execution
    pub stderr: String,

    /// Exit code (if applicable)
    pub exit_code: Option<i32>,

    /// Execution time taken
    pub execution_time: Duration,

    /// Resources used (approximate)
    pub resources_used: ResourceUsage,
}

/// Resource usage tracking
#[derive(Debug, Clone, Default)]
pub struct ResourceUsage {
    /// Memory used (MB)
    pub memory_mb: f64,

    /// CPU time used (seconds)
    pub cpu_seconds: f64,
}

/// Security sandbox for script execution
pub struct ExecutionSandbox {
    config: ExecutionConfig,
}

impl ExecutionSandbox {
    /// Create a new execution sandbox with the given configuration
    pub fn new(config: ExecutionConfig) -> Result<Self, ExecutionError> {
        Ok(Self { config })
    }

    /// Execute a script with the provided context
    pub async fn execute_script(
        &self,
        script: ScriptDefinition,
        context: ExecutionContext,
    ) -> Result<ExecutionResult, ExecutionError> {
        // Validate script for security
        self.validate_script(&script)?;

        let start_time = std::time::Instant::now();

        // For now, fallback to user's environment execution
        // In the future, this would use proper sandboxing
        let result = self.execute_in_user_environment(script, context).await?;

        let execution_time = start_time.elapsed();

        Ok(ExecutionResult {
            success: result.exit_code.unwrap_or(0) == 0,
            stdout: result.stdout,
            stderr: result.stderr,
            exit_code: result.exit_code,
            execution_time,
            resources_used: ResourceUsage::default(), // Would track actual usage in sandboxed version
        })
    }

    /// Validate script for security issues
    fn validate_script(&self, script: &ScriptDefinition) -> Result<(), ExecutionError> {
        // Basic validation - check for dangerous patterns
        if let Some(content) = &script.content {
            // Check for dangerous imports or system calls
            let dangerous_patterns = [
                "import os",
                "import subprocess",
                "import sys",
                "exec(",
                "eval(",
                "system(",
                "popen(",
                "rm -rf",
                "sudo",
                "chmod 777",
                "chown",
            ];

            for pattern in &dangerous_patterns {
                if content.contains(pattern) {
                    return Err(ExecutionError::SecurityViolation(format!(
                        "Dangerous pattern detected: {}",
                        pattern
                    )));
                }
            }
        }

        // Validate file paths if accessing files
        if script.path.exists() {
            // Check path traversal - ensure script is under allowed roots if configured
            if !self.config.allowed_script_roots.is_empty() {
                let canonical_path = script.path.canonicalize().map_err(|e| {
                    ExecutionError::SecurityViolation(format!(
                        "Failed to canonicalize script path: {}",
                        e
                    ))
                })?;

                let is_allowed = self.config.allowed_script_roots.iter().any(|root| {
                    if let Ok(canonical_root) = root.canonicalize() {
                        canonical_path.starts_with(&canonical_root)
                    } else {
                        false
                    }
                });

                if !is_allowed {
                    return Err(ExecutionError::SecurityViolation(format!(
                        "Script path '{}' is outside allowed roots: {:?}",
                        script.path.display(),
                        self.config.allowed_script_roots
                    )));
                }
            }
        }

        Ok(())
    }

    /// Execute script in user's environment (fallback implementation)
    async fn execute_in_user_environment(
        &self,
        script: ScriptDefinition,
        context: ExecutionContext,
    ) -> Result<UserExecutionResult, ExecutionError> {
        let script_path = &script.path;

        if !script_path.exists() {
            return Err(ExecutionError::ScriptNotFound(
                script_path.to_string_lossy().to_string(),
            ));
        }

        // Determine command based on script language
        let command = match script.language {
            ScriptLanguage::Python => "python3",
            ScriptLanguage::NodeJS => "node",
            ScriptLanguage::Shell => "sh",
            ScriptLanguage::Rust => "cargo",
        };

        // Build the command
        let mut cmd = TokioCommand::new(command);

        // Add script path as argument
        cmd.arg(script_path);

        // Add parameters as environment variables
        for (key, value) in &script.parameters {
            cmd.env(format!("PARAM_{}", key), value);
        }

        // Add context environment variables
        for (key, value) in &context.environment_variables {
            cmd.env(key, value);
        }

        // Set working directory
        if let Some(working_dir) = &script.working_directory {
            cmd.current_dir(working_dir);
        }

        // Set timeout
        let timeout_duration = self.config.default_timeout;

        // Execute with timeout
        let result = timeout(timeout_duration, async {
            let output = cmd
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .output()
                .await?;

            Ok::<_, std::io::Error>(output)
        })
        .await;

        match result {
            Ok(Ok(output)) => Ok(UserExecutionResult {
                stdout: String::from_utf8_lossy(&output.stdout).to_string(),
                stderr: String::from_utf8_lossy(&output.stderr).to_string(),
                exit_code: output.status.code(),
            }),
            Ok(Err(e)) => Err(ExecutionError::Io(e)),
            Err(_) => Err(ExecutionError::Timeout(timeout_duration)),
        }
    }

    /// Execute a command directly
    pub async fn execute_command(
        &self,
        command: String,
        args: Vec<String>,
        context: ExecutionContext,
    ) -> Result<ExecutionResult, ExecutionError> {
        // Check if command is allowed
        if !self.config.allowed_commands.is_empty() {
            let command_name = command.split('/').next_back().unwrap_or(&command);
            if !self
                .config
                .allowed_commands
                .contains(&command_name.to_string())
            {
                return Err(ExecutionError::SecurityViolation(format!(
                    "Command '{}' is not in allowed commands list",
                    command_name
                )));
            }
        }

        let start_time = std::time::Instant::now();

        // Execute command in user's environment
        let result = self
            .execute_command_in_user_environment(command, args, context)
            .await?;

        let execution_time = start_time.elapsed();

        Ok(ExecutionResult {
            success: result.exit_code.unwrap_or(0) == 0,
            stdout: result.stdout,
            stderr: result.stderr,
            exit_code: result.exit_code,
            execution_time,
            resources_used: ResourceUsage::default(),
        })
    }

    /// Execute command in user's environment (fallback implementation)
    async fn execute_command_in_user_environment(
        &self,
        command: String,
        args: Vec<String>,
        context: ExecutionContext,
    ) -> Result<UserExecutionResult, ExecutionError> {
        let mut cmd = TokioCommand::new(command);
        cmd.args(args);

        // Set working directory
        if let Some(working_dir) = &context.working_directory {
            cmd.current_dir(working_dir);
        }

        // Set timeout
        let timeout_duration = self.config.default_timeout;

        let result = timeout(timeout_duration, async {
            let output = cmd
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .output()
                .await?;

            Ok::<_, std::io::Error>(output)
        })
        .await;

        match result {
            Ok(Ok(output)) => Ok(UserExecutionResult {
                stdout: String::from_utf8_lossy(&output.stdout).to_string(),
                stderr: String::from_utf8_lossy(&output.stderr).to_string(),
                exit_code: output.status.code(),
            }),
            Ok(Err(e)) => Err(ExecutionError::Io(e)),
            Err(_) => Err(ExecutionError::Timeout(timeout_duration)),
        }
    }

    /// Get current configuration
    pub fn config(&self) -> &ExecutionConfig {
        &self.config
    }
}

/// Result from user environment execution
#[derive(Debug)]
struct UserExecutionResult {
    stdout: String,
    stderr: String,
    exit_code: Option<i32>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_validate_script_rejects_path_traversal() {
        let temp_dir = TempDir::new().unwrap();
        let allowed_root = temp_dir.path().join("allowed");
        std::fs::create_dir_all(&allowed_root).unwrap();

        let config = ExecutionConfig {
            allowed_script_roots: vec![allowed_root.clone()],
            ..Default::default()
        };

        let sandbox = ExecutionSandbox::new(config).unwrap();

        // Create a script outside the allowed root
        let outside_path = temp_dir.path().join("outside").join("script.py");
        std::fs::create_dir_all(outside_path.parent().unwrap()).unwrap();
        std::fs::write(&outside_path, "print('test')").unwrap();

        let script = ScriptDefinition {
            path: outside_path,
            content: None,
            language: ScriptLanguage::Python,
            parameters: std::collections::HashMap::new(),
            working_directory: None,
        };

        let result = sandbox.validate_script(&script);
        assert!(matches!(result, Err(ExecutionError::SecurityViolation(_))));
    }

    #[test]
    fn test_validate_script_accepts_path_inside_allowed_root() {
        let temp_dir = TempDir::new().unwrap();
        let allowed_root = temp_dir.path().join("allowed");
        std::fs::create_dir_all(&allowed_root).unwrap();

        let config = ExecutionConfig {
            allowed_script_roots: vec![allowed_root.clone()],
            ..Default::default()
        };

        let sandbox = ExecutionSandbox::new(config).unwrap();

        // Create a script inside the allowed root
        let inside_path = allowed_root.join("script.py");
        std::fs::write(&inside_path, "print('test')").unwrap();

        let script = ScriptDefinition {
            path: inside_path,
            content: None,
            language: ScriptLanguage::Python,
            parameters: std::collections::HashMap::new(),
            working_directory: None,
        };

        let result = sandbox.validate_script(&script);
        assert!(result.is_ok());
    }
}