wasm-sandbox 0.4.1

A secure WebAssembly sandbox with dead-simple ease of use, progressive complexity APIs, and comprehensive safety controls
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//! Implementation of security capabilities for the sandbox

use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::net::{IpAddr, SocketAddr};

use crate::error::{Error, Result, SecurityContext};
use crate::security::{
    NetworkCapability, FilesystemCapability, 
    EnvironmentCapability, ProcessCapability, TimeCapability, RandomCapability
};

/// Capability verification helper
pub trait CapabilityVerifier {
    /// Verify that an operation is allowed by the capabilities
    fn verify(&self, operation: &str, params: &[&str]) -> Result<()>;
}

/// Network capability verifier
pub struct NetworkVerifier {
    capability: NetworkCapability,
}

impl NetworkVerifier {
    /// Create a new network verifier
    pub fn new(capability: NetworkCapability) -> Self {
        Self { capability }
    }
    
    /// Check if a host is allowed
    pub fn is_host_allowed(&self, host: &str, port: u16, secure: bool) -> bool {
        match &self.capability {
            NetworkCapability::None => false,
            NetworkCapability::Loopback => {
                // Check if host is localhost or 127.0.0.1
                host == "localhost" || host == "127.0.0.1" || host == "::1"
            },
            NetworkCapability::AllowedHosts(hosts) => {
                // Check if host is in the allowed hosts list
                hosts.iter().any(|h| {
                    // Check host
                    if h.host != host {
                        return false;
                    }
                    
                    // Check port if specified
                    if let Some(port_range) = &h.ports {
                        if !port_range.contains(port) {
                            return false;
                        }
                    }
                    
                    // Check secure flag
                    if !h.secure && secure {
                        return false;
                    }
                    
                    true
                })
            },
            NetworkCapability::AllowedPorts(ports) => {
                // Check if port is in any allowed port range
                ports.iter().any(|r| r.contains(port))
            },
            NetworkCapability::Full => true,
        }
    }
    
    /// Check if an IP address is allowed
    pub fn is_ip_allowed(&self, ip: IpAddr, port: u16) -> bool {
        match &self.capability {
            NetworkCapability::None => false,
            NetworkCapability::Loopback => {
                // Check if IP is localhost
                match ip {
                    IpAddr::V4(addr) => addr.is_loopback(),
                    IpAddr::V6(addr) => addr.is_loopback(),
                }
            },
            NetworkCapability::AllowedHosts(_hosts) => {
                // Not implemented: would need to resolve hosts to IPs
                false
            },
            NetworkCapability::AllowedPorts(ports) => {
                // Check if port is in any allowed port range
                ports.iter().any(|r| r.contains(port))
            },
            NetworkCapability::Full => true,
        }
    }
    
    /// Check if a socket address is allowed
    pub fn is_socket_allowed(&self, socket: SocketAddr) -> bool {
        self.is_ip_allowed(socket.ip(), socket.port())
    }
}

impl CapabilityVerifier for NetworkVerifier {
    fn verify(&self, operation: &str, params: &[&str]) -> Result<()> {
        match operation {
            "connect" => {
                if params.len() < 2 {
                    return Err(Error::Capability { message: "Missing host and port for connect".to_string() });
                }
                
                let host = params[0];
                let port = params[1].parse::<u16>().map_err(|_| {
                    Error::Capability { message: format!("Invalid port: {}", params[1]) }
                })?;
                
                let secure = params.get(2).map(|s| *s == "secure").unwrap_or(false);
                
                if !self.is_host_allowed(host, port, secure) {
                    return Err(Error::SecurityViolation {
                        violation: format!("Network access denied to {}:{}", host, port),
                        instance_id: None,
                        context: create_security_context("connect", "network.connect", &[]),
                    });
                }
            }
            "bind" => {
                if params.len() < 2 {
                    return Err(Error::Capability { message: "Missing host and port for bind".to_string() });
                }
                
                let host = params[0];
                let port = params[1].parse::<u16>().map_err(|_| {
                    Error::Capability { message: format!("Invalid port: {}", params[1]) }
                })?;
                
                if !self.is_host_allowed(host, port, false) {
                    return Err(Error::SecurityViolation {
                        violation: format!("Network binding denied to {}:{}", host, port),
                        instance_id: None,
                        context: create_security_context("bind", "network.bind", &[]),
                    });
                }
            }
            "listen" => {
                if params.len() < 1 {
                    return Err(Error::Capability { message: "Missing port for listen".to_string() });
                }
                
                let port = params[0].parse::<u16>().map_err(|_| {
                    Error::Capability { message: format!("Invalid port: {}", params[0]) }
                })?;
                
                // For listen, we check if the loopback address is allowed with this port
                if !self.is_host_allowed("127.0.0.1", port, false) {
                    return Err(Error::SecurityViolation {
                        violation: format!("Network listening denied on port {}", port),
                        instance_id: None,
                        context: create_security_context("listen", "network.listen", &[]),
                    });
                }
            }
            _ => {
                return Err(Error::Capability { message: format!("Unknown network operation: {}", operation) });
            }
        }
        
        Ok(())
    }
}

/// Filesystem capability verifier
pub struct FilesystemVerifier {
    capability: FilesystemCapability,
    normalized_readable: HashSet<PathBuf>,
    normalized_writable: HashSet<PathBuf>,
}

impl FilesystemVerifier {
    /// Create a new filesystem verifier
    pub fn new(capability: FilesystemCapability) -> Self {
        // Normalize paths for better comparison
        let normalized_readable = capability.readable_dirs
            .iter()
            .filter_map(|p| std::fs::canonicalize(p).ok())
            .collect();
            
        let normalized_writable = capability.writable_dirs
            .iter()
            .filter_map(|p| std::fs::canonicalize(p).ok())
            .collect();
        
        Self { 
            capability,
            normalized_readable,
            normalized_writable,
        }
    }
    
    /// Check if a path is readable
    pub fn is_readable(&self, path: &Path) -> bool {
        // Try to canonicalize the path
        let canon_path = match std::fs::canonicalize(path) {
            Ok(p) => p,
            Err(_) => return false, // Path doesn't exist or other error
        };
        
        // Check if the path is in any readable directory
        for dir in &self.normalized_readable {
            if is_path_within(dir, &canon_path) {
                return true;
            }
        }
        
        // Also check if it's writable (writable implies readable)
        self.is_writable(path)
    }
    
    /// Check if a path is writable
    pub fn is_writable(&self, path: &Path) -> bool {
        // Try to canonicalize the path
        let canon_path = match std::fs::canonicalize(path) {
            Ok(p) => p,
            Err(_) => {
                // If path doesn't exist, check its parent directory
                if let Some(parent) = path.parent() {
                    match std::fs::canonicalize(parent) {
                        Ok(p) => p,
                        Err(_) => return false, // Parent doesn't exist
                    }
                } else {
                    return false; // No parent (root)
                }
            }
        };
        
        // Check if the path is in any writable directory
        for dir in &self.normalized_writable {
            if is_path_within(dir, &canon_path) {
                return true;
            }
        }
        
        false
    }
    
    /// Check if file creation is allowed
    pub fn can_create(&self) -> bool {
        self.capability.allow_create
    }
    
    /// Check if file deletion is allowed
    pub fn can_delete(&self) -> bool {
        self.capability.allow_delete
    }
    
    /// Check if a file size is within limits
    pub fn is_size_allowed(&self, size: u64) -> bool {
        match self.capability.max_file_size {
            Some(limit) => size <= limit,
            None => true,
        }
    }
}

impl CapabilityVerifier for FilesystemVerifier {
    fn verify(&self, operation: &str, params: &[&str]) -> Result<()> {
        match operation {
            "open" | "read" => {
                if params.is_empty() {
                    return Err(Error::Capability { message: "Missing path for open/read".to_string() });
                }
                
                let path = Path::new(params[0]);
                if !self.is_readable(path) {
                    return Err(Error::SecurityViolation {
                        violation: format!("File read access denied: {}", path.display()),
                        instance_id: None,
                        context: create_security_context("read", "filesystem.read", &[]),
                    });
                }
            }
            "write" | "append" => {
                if params.is_empty() {
                    return Err(Error::Capability { message: "Missing path for write/append".to_string() });
                }
                
                let path = Path::new(params[0]);
                if !self.is_writable(path) {
                    return Err(Error::SecurityViolation {
                        violation: format!("File write access denied: {}", path.display()),
                        instance_id: None,
                        context: create_security_context("write", "filesystem.write", &[]),
                    });
                }
                
                // Check size limit if provided
                if params.len() > 1 {
                    let size = params[1].parse::<u64>().map_err(|_| {
                        Error::Capability { message: format!("Invalid size: {}", params[1]) }
                    })?;
                    
                    if !self.is_size_allowed(size) {
                        return Err(Error::ResourceLimit {
                            message: format!("File size limit exceeded: {}", size)
                        });
                    }
                }
            }
            "create" => {
                if params.is_empty() {
                    return Err(Error::Capability { message: "Missing path for create".to_string() });
                }
                
                if !self.can_create() {
                    return Err(Error::SecurityViolation {
                        violation: "File creation is not allowed".to_string(),
                        instance_id: None,
                        context: create_security_context("create", "filesystem.create", &[]),
                    });
                }
                
                let path = Path::new(params[0]);
                if !self.is_writable(path) {
                    return Err(Error::SecurityViolation {
                        violation: format!("File creation access denied: {}", path.display()),
                        instance_id: None,
                        context: create_security_context("create", "filesystem.create", &[]),
                    });
                }
            }
            "delete" | "remove" => {
                if params.is_empty() {
                    return Err(Error::Capability { message: "Missing path for delete/remove".to_string() });
                }
                
                if !self.can_delete() {
                    return Err(Error::SecurityViolation {
                        violation: "File deletion is not allowed".to_string(),
                        instance_id: None,
                        context: create_security_context("delete", "filesystem.delete", &[]),
                    });
                }
                
                let path = Path::new(params[0]);
                if !self.is_writable(path) {
                    return Err(Error::SecurityViolation {
                        violation: format!("File deletion access denied: {}", path.display()),
                        instance_id: None,
                        context: create_security_context("delete", "filesystem.delete", &[]),
                    });
                }
            }
            _ => {
                return Err(Error::Capability { message: format!("Unknown filesystem operation: {}", operation) });
            }
        }
        
        Ok(())
    }
}

/// Helper function to check if a path is within a directory
fn is_path_within(dir: &Path, path: &Path) -> bool {
    let dir_str = dir.to_string_lossy();
    let path_str = path.to_string_lossy();
    
    // Check if path starts with dir (and there's either an exact match or a path separator after)
    if path_str == dir_str {
        return true;
    }
    
    path_str.starts_with(&format!("{}{}", dir_str, std::path::MAIN_SEPARATOR))
}

/// Environment capability verifier
pub struct EnvironmentVerifier {
    capability: EnvironmentCapability,
}

impl EnvironmentVerifier {
    /// Create a new environment verifier
    pub fn new(capability: EnvironmentCapability) -> Self {
        Self { capability }
    }
    
    /// Check if a variable is allowed
    pub fn is_var_allowed(&self, var: &str) -> bool {
        match &self.capability {
            EnvironmentCapability::None => false,
            EnvironmentCapability::Allowlist(allowed) => allowed.iter().any(|v| v == var),
            EnvironmentCapability::Denylist(denied) => !denied.iter().any(|v| v == var),
            EnvironmentCapability::Full => true,
        }
    }
}

impl CapabilityVerifier for EnvironmentVerifier {
    fn verify(&self, operation: &str, params: &[&str]) -> Result<()> {
        match operation {
            "get" | "set" => {
                if params.is_empty() {
                    return Err(Error::Capability { message: "Missing variable name".to_string() });
                }
                
                let var = params[0];
                if !self.is_var_allowed(var) {
                    return Err(Error::SecurityViolation {
                        violation: format!("Environment variable access denied: {}", var),
                        instance_id: None,
                        context: create_security_context("get", "environment.get", &[]),
                    });
                }
                
                // For "set", additionally check if we're in Full mode
                if operation == "set" && !matches!(self.capability, EnvironmentCapability::Full) {
                    return Err(Error::SecurityViolation {
                        violation: format!("Setting environment variables is not allowed: {}", var),
                        instance_id: None,
                        context: create_security_context("set", "environment.set", &[]),
                    });
                }
            }
            _ => {
                return Err(Error::Capability { message: format!("Unknown environment operation: {}", operation) });
            }
        }
        
        Ok(())
    }
}

/// Process capability verifier
pub struct ProcessVerifier {
    capability: ProcessCapability,
}

impl ProcessVerifier {
    /// Create a new process verifier
    pub fn new(capability: ProcessCapability) -> Self {
        Self { capability }
    }
    
    /// Check if a command is allowed to be executed
    pub fn is_command_allowed(&self, command: &str) -> bool {
        match &self.capability {
            ProcessCapability::None => false,
            ProcessCapability::AllowedCommands(allowed) => {
                // Check if the command matches any allowed commands
                allowed.iter().any(|cmd| {
                    // Exact match
                    if cmd == command {
                        return true;
                    }
                    
                    // Wildcard match
                    if cmd.ends_with("*") {
                        let prefix = &cmd[..cmd.len() - 1];
                        return command.starts_with(prefix);
                    }
                    
                    false
                })
            }
            ProcessCapability::Full => true,
        }
    }
}

impl CapabilityVerifier for ProcessVerifier {
    fn verify(&self, operation: &str, params: &[&str]) -> Result<()> {
        match operation {
            "exec" | "spawn" => {
                if params.is_empty() {
                    return Err(Error::Capability { message: "Missing command".to_string() });
                }
                
                let command = params[0];
                if !self.is_command_allowed(command) {
                    return Err(Error::SecurityViolation {
                        violation: format!("Process execution denied: {}", command),
                        instance_id: None,
                        context: create_security_context("exec", "process.exec", &[]),
                    });
                }
            }
            _ => {
                return Err(Error::Capability { message: format!("Unknown process operation: {}", operation) });
            }
        }
        
        Ok(())
    }
}

/// Time capability verifier
pub struct TimeVerifier {
    capability: TimeCapability,
}

impl TimeVerifier {
    /// Create a new time verifier
    pub fn new(capability: TimeCapability) -> Self {
        Self { capability }
    }
    
    /// Check if setting time is allowed
    pub fn can_set_time(&self) -> bool {
        matches!(self.capability, TimeCapability::Full)
    }
}

impl CapabilityVerifier for TimeVerifier {
    fn verify(&self, operation: &str, _params: &[&str]) -> Result<()> {
        match operation {
            "get" => {
                // Reading time is always allowed
                Ok(())
            }
            "set" => {
                if !self.can_set_time() {
                    return Err(Error::SecurityViolation {
                        violation: "Setting time is not allowed".to_string(),
                        instance_id: None,
                        context: create_security_context("set", "time.set", &[]),
                    });
                }
                Ok(())
            }
            _ => {
                Err(Error::Capability { message: format!("Unknown time operation: {}", operation) })
            }
        }
    }
}

/// Random capability verifier
pub struct RandomVerifier {
    capability: RandomCapability,
}

impl RandomVerifier {
    /// Create a new random verifier
    pub fn new(capability: RandomCapability) -> Self {
        Self { capability }
    }
    
    /// Check if secure random generation is allowed
    pub fn can_secure_random(&self) -> bool {
        matches!(self.capability, RandomCapability::Full)
    }
    
    /// Check if any random generation is allowed
    pub fn can_pseudo_random(&self) -> bool {
        !matches!(self.capability, RandomCapability::None)
    }
}

impl CapabilityVerifier for RandomVerifier {
    fn verify(&self, operation: &str, _params: &[&str]) -> Result<()> {
        match operation {
            "pseudo" => {
                if !self.can_pseudo_random() {
                    return Err(Error::SecurityViolation {
                        violation: "Pseudo-random generation is not allowed".to_string(),
                        instance_id: None,
                        context: create_security_context("pseudo", "random.pseudo", &[]),
                    });
                }
                Ok(())
            }
            "secure" => {
                if !self.can_secure_random() {
                    return Err(Error::SecurityViolation {
                        violation: "Secure random generation is not allowed".to_string(),
                        instance_id: None,
                        context: create_security_context("secure", "random.secure", &[]),
                    });
                }
                Ok(())
            }
            _ => {
                Err(Error::Capability { message: format!("Unknown random operation: {}", operation) })
            }
        }
    }
}

/// Helper function to create SecurityContext for capability violations
fn create_security_context(operation: &str, required_capability: &str, available_capabilities: &[&str]) -> SecurityContext {
    SecurityContext {
        attempted_operation: operation.to_string(),
        required_capability: required_capability.to_string(),
        available_capabilities: available_capabilities.iter().map(|s| s.to_string()).collect(),
    }
}

/// Central capability manager that combines all verifiers
pub struct CapabilityManager {
    /// Network verifier
    pub network: NetworkVerifier,
    
    /// Filesystem verifier
    pub filesystem: FilesystemVerifier,
    
    /// Environment verifier
    pub environment: EnvironmentVerifier,
    
    /// Process verifier
    pub process: ProcessVerifier,
    
    /// Time verifier
    pub time: TimeVerifier,
    
    /// Random verifier
    pub random: RandomVerifier,
}

impl CapabilityManager {
    /// Create a new capability manager
    pub fn new(
        network: NetworkCapability,
        filesystem: FilesystemCapability,
        environment: EnvironmentCapability,
        process: ProcessCapability,
        time: TimeCapability,
        random: RandomCapability,
    ) -> Self {
        Self {
            network: NetworkVerifier::new(network),
            filesystem: FilesystemVerifier::new(filesystem),
            environment: EnvironmentVerifier::new(environment),
            process: ProcessVerifier::new(process),
            time: TimeVerifier::new(time),
            random: RandomVerifier::new(random),
        }
    }
    
    /// Check if an operation is allowed based on its capability domain
    pub fn verify(&self, domain: &str, operation: &str, params: &[&str]) -> Result<()> {
        match domain {
            "network" => self.network.verify(operation, params),
            "filesystem" | "fs" => self.filesystem.verify(operation, params),
            "environment" | "env" => self.environment.verify(operation, params),
            "process" | "proc" => self.process.verify(operation, params),
            "time" => self.time.verify(operation, params),
            "random" | "rand" => self.random.verify(operation, params),
            _ => Err(Error::Capability { message: format!("Unknown capability domain: {}", domain) }),
        }
    }
}