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
//! HTTP server wrapper implementation

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

use serde::{Serialize, Deserialize};

use crate::error::Result;
use crate::wrappers::{WrapperGenerator, WrapperSpec};

/// HTTP request method
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum HttpMethod {
    /// GET request
    GET,
    
    /// POST request
    POST,
    
    /// PUT request
    PUT,
    
    /// DELETE request
    DELETE,
    
    /// PATCH request
    PATCH,
    
    /// HEAD request
    HEAD,
    
    /// OPTIONS request
    OPTIONS,
}

impl std::fmt::Display for HttpMethod {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::GET => write!(f, "GET"),
            Self::POST => write!(f, "POST"),
            Self::PUT => write!(f, "PUT"),
            Self::DELETE => write!(f, "DELETE"),
            Self::PATCH => write!(f, "PATCH"),
            Self::HEAD => write!(f, "HEAD"),
            Self::OPTIONS => write!(f, "OPTIONS"),
        }
    }
}

/// HTTP request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HttpRequest {
    /// Request method
    pub method: HttpMethod,
    
    /// Request path
    pub path: String,
    
    /// Query parameters
    pub query: Option<String>,
    
    /// Request headers
    pub headers: Vec<(String, String)>,
    
    /// Request body
    pub body: Option<Vec<u8>>,
    
    /// Remote address
    pub remote_addr: Option<SocketAddr>,
}

/// HTTP response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HttpResponse {
    /// Status code
    pub status: u16,
    
    /// Response headers
    pub headers: Vec<(String, String)>,
    
    /// Response body
    pub body: Vec<u8>,
}

impl HttpResponse {
    /// Create a new HTTP response
    pub fn new(status: u16, body: Vec<u8>) -> Self {
        Self {
            status,
            headers: Vec::new(),
            body,
        }
    }
    
    /// Create a new HTTP response with headers
    pub fn with_headers(status: u16, headers: Vec<(String, String)>, body: Vec<u8>) -> Self {
        Self {
            status,
            headers,
            body,
        }
    }
    
    /// Create an OK response with text
    pub fn text(body: &str) -> Self {
        Self {
            status: 200,
            headers: vec![
                ("Content-Type".to_string(), "text/plain; charset=utf-8".to_string()),
                ("Content-Length".to_string(), body.len().to_string()),
            ],
            body: body.as_bytes().to_vec(),
        }
    }
    
    /// Create an OK response with JSON
    pub fn json<T: Serialize>(body: &T) -> Self {
        let json = serde_json::to_vec(body).unwrap_or_default();
        Self {
            status: 200,
            headers: vec![
                ("Content-Type".to_string(), "application/json".to_string()),
                ("Content-Length".to_string(), json.len().to_string()),
            ],
            body: json,
        }
    }
    
    /// Create a not found response
    pub fn not_found() -> Self {
        Self::text("Not Found")
            .with_status(404)
    }
    
    /// Create an internal server error response
    pub fn server_error(message: &str) -> Self {
        Self::text(message)
            .with_status(500)
    }
    
    /// Set the status code
    pub fn with_status(mut self, status: u16) -> Self {
        self.status = status;
        self
    }
    
    /// Add a header
    pub fn with_header(mut self, name: &str, value: &str) -> Self {
        self.headers.push((name.to_string(), value.to_string()));
        self
    }
}

/// HTTP server configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HttpServerConfig {
    /// Bind address
    pub address: IpAddr,
    
    /// Port
    pub port: u16,
    
    /// Thread count
    pub threads: Option<usize>,
    
    /// Connection timeout in seconds
    pub timeout_seconds: Option<u64>,
    
    /// Maximum request size in bytes
    pub max_request_size: Option<usize>,
    
    /// CORS configuration
    pub cors: Option<CorsConfig>,
    
    /// TLS configuration
    pub tls: Option<TlsConfig>,
}

/// CORS configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CorsConfig {
    /// Allowed origins
    pub allowed_origins: Vec<String>,
    
    /// Allowed methods
    pub allowed_methods: Vec<String>,
    
    /// Allowed headers
    pub allowed_headers: Vec<String>,
    
    /// Allow credentials
    pub allow_credentials: bool,
    
    /// Max age in seconds
    pub max_age: Option<u32>,
}

/// TLS configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TlsConfig {
    /// Certificate path
    pub cert_path: PathBuf,
    
    /// Key path
    pub key_path: PathBuf,
}

/// HTTP server wrapper generator
pub struct HttpServerGenerator;

impl WrapperGenerator for HttpServerGenerator {
    fn generate_wrapper(&self, spec: &WrapperSpec) -> Result<String> {
        // Parse the spec template variables for HTTP server options
        let config = HttpServerConfig {
            address: spec.template_variables.get("address")
                .and_then(|s| s.parse().ok())
                .unwrap_or_else(|| "127.0.0.1".parse().unwrap()),
            port: spec.template_variables.get("port")
                .and_then(|s| s.parse().ok())
                .unwrap_or(8080),
            threads: spec.template_variables.get("threads")
                .and_then(|s| s.parse().ok()),
            timeout_seconds: spec.template_variables.get("timeout_seconds")
                .and_then(|s| s.parse().ok()),
            max_request_size: spec.template_variables.get("max_request_size")
                .and_then(|s| s.parse().ok()),
            cors: None,
            tls: None,
        };
        
        // Generate a basic HTTP server wrapper
        let code = self.generate_code(&config, spec)?;
        
        Ok(code)
    }
    
    fn compile_wrapper(&self, code: &str, output_path: &std::path::Path) -> Result<()> {
        // For now, just write the code to the output path
        std::fs::write(output_path, code)
            .map_err(|e| crate::error::Error::WrapperGeneration {
                reason: format!("Failed to write wrapper code: {}", e),
                wrapper_type: Some("http_server".to_string()),
            })?;
        Ok(())
    }
}

impl HttpServerGenerator {
    /// Generate HTTP server wrapper code
    fn generate_code(&self, config: &HttpServerConfig, spec: &WrapperSpec) -> Result<String> {
        let address = format!("{}:{}", config.address, config.port);
        let _threads = config.threads.unwrap_or(4);
        let _timeout = config.timeout_seconds.unwrap_or(30);
        
        // Generate the server code
        let mut code = format!(
            r#"//! HTTP server wrapper for WASM sandbox

use std::{{
    net::{{IpAddr, SocketAddr}},
    sync::{{Arc, Mutex}},
    time::Duration,
}};

use tokio::{{
    net::TcpListener,
    runtime::Runtime,
    io::{{AsyncReadExt, AsyncWriteExt}},
}};
use hyper::{{
    Body, Request, Response, Server, StatusCode,
    service::{{make_service_fn, service_fn}},
    header::{{HeaderValue, CONTENT_TYPE}},
}};
use serde_json::{{Value, json}};
use tracing::{{info, error, debug, warn}};

use wasm_sandbox::{{
    WasmSandbox, InstanceId, SandboxConfig, InstanceConfig,
    security::{{ResourceLimits, Capabilities}},
}};

/// Main function
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {{
    // Initialize logging
    tracing_subscriber::fmt::init();
    info!("Starting HTTP server on {address}");
    
    // Create sandbox
    let mut sandbox = WasmSandbox::new()?;
    
    // Load the WASM module
    let wasm_bytes = include_bytes!("{wasm_path}");
    let module_id = sandbox.load_module(wasm_bytes)?;
    
    info!("Loaded WASM module");
    
    // Create instance config
    let instance_config = {instance_config};
    
    // Create the instance
    let instance_id = sandbox.create_instance(module_id, Some(instance_config))?;
    info!("Created WASM instance: {{instance_id}}");
    
    // Store the instance ID
    let instance_id = Arc::new(instance_id);
    
    // Create HTTP server
    let make_svc = make_service_fn(move |conn| {{
        let remote_addr = conn.remote_addr();
        let instance_id = instance_id.clone();
        let sandbox = sandbox.clone();
        
        async move {{
            Ok::<_, hyper::Error>(service_fn(move |req| {{
                let instance_id = instance_id.clone();
                let sandbox = sandbox.clone();
                handle_request(req, sandbox, instance_id, remote_addr)
            }}))
        }}
    }});
    
    let addr = "{address}".parse()?;
    let server = Server::bind(&addr)
        .serve(make_svc)
        .with_graceful_shutdown(shutdown_signal());
        
    info!("Server running on http://{address}");
    
    if let Err(e) = server.await {{
        error!("Server error: {{}}", e);
    }}
    
    Ok(())
}}

/// Handle HTTP request
async fn handle_request(
    req: Request<Body>, 
    sandbox: Arc<Mutex<WasmSandbox>>,
    instance_id: Arc<InstanceId>,
    remote_addr: SocketAddr,
) -> Result<Response<Body>, hyper::Error> {{
    // Convert hyper request to our format
    let (parts, body) = req.into_parts();
    let body_bytes = hyper::body::to_bytes(body).await?;
    
    // Create request object
    let request = json!({{
        "method": parts.method.as_str(),
        "path": parts.uri.path(),
        "query": parts.uri.query(),
        "headers": parts.headers.iter().map(|(k, v)| {{
            (k.as_str(), v.to_str().unwrap_or_default())
        }}).collect::<Vec<_>>(),
        "body": body_bytes.to_vec(),
        "remote_addr": remote_addr.to_string(),
    }});
    
    // Call handler function in sandbox
    let response = match sandbox.lock().unwrap().call_function::<_, Value>(
        *instance_id,
        "handle_http_request",
        &request,
    ).await {{
        Ok(res) => res,
        Err(e) => {{
            error!("Error calling WASM function: {{}}", e);
            return Ok(Response::builder()
                .status(StatusCode::INTERNAL_SERVER_ERROR)
                .body(Body::from("Internal Server Error"))?);
        }}
    }};
    
    // Convert JSON response to hyper response
    let status = response["status"].as_u64().unwrap_or(500) as u16;
    let headers = response["headers"].as_array().unwrap_or(&Vec::new());
    let body = response["body"].as_array().unwrap_or(&Vec::new())
        .iter()
        .filter_map(|v| v.as_u64().map(|n| n as u8))
        .collect::<Vec<_>>();
        
    // Build response
    let mut builder = Response::builder().status(status);
    
    // Add headers
    for header in headers {{
        if let (Some(name), Some(value)) = (
            header[0].as_str(),
            header[1].as_str(),
        ) {{
            builder = builder.header(name, value);
        }}
    }}
    
    // Add default content type if not present
    if !headers.iter().any(|h| h[0].as_str() == Some("content-type")) {{
        builder = builder.header(CONTENT_TYPE, "text/plain");
    }}
    
    Ok(builder.body(Body::from(body))?)
}}

/// Shutdown signal handler
async fn shutdown_signal() {{
    tokio::signal::ctrl_c()
        .await
        .expect("Failed to install CTRL+C signal handler");
        
    info!("Shutdown signal received, stopping server...");
}}
"#,
            address = address,
            wasm_path = spec.app_path.display(),
            instance_config = "InstanceConfig::default()",  // Simplified for now
        );
        
        // Add TLS configuration if enabled
        if let Some(_tls_config) = &config.cors {
            // Add TLS imports and config
            code = code.replace(
                "use hyper::",
                r#"use rustls::{{Certificate, PrivateKey, ServerConfig}};
use tokio_rustls::TlsAcceptor;
use std::fs::File;
use std::io::BufReader;
use rustls_pemfile::{certs, rsa_private_keys};
use hyper::"#
            );
            
            // This is a simplified placeholder - real TLS configuration would be more complex
            code = code.replace(
                "let addr = \"{address}\".parse()?;",
                r#"let addr = "{address}".parse()?;
    
    // Configure TLS
    let tls_config = {
        // Load certificate and private key
        let cert_file = File::open("certificate.pem")?;
        let mut reader = BufReader::new(cert_file);
        let certs = certs(&mut reader)?;
        let certs = certs.into_iter().map(Certificate).collect();
        
        let key_file = File::open("private_key.pem")?;
        let mut reader = BufReader::new(key_file);
        let keys = rsa_private_keys(&mut reader)?;
        let key = PrivateKey(keys[0].clone());
        
        let mut config = ServerConfig::builder()
            .with_safe_defaults()
            .with_no_client_auth()
            .with_single_cert(certs, key)?;
            
        Arc::new(config)
    };"#
            );
        }
        
        // Add CORS configuration if enabled
        if let Some(cors_config) = &config.cors {
            let _allowed_origins = cors_config.allowed_origins
                .iter()
                .map(|o| format!("\"{}\"", o))
                .collect::<Vec<_>>()
                .join(", ");
                
            let _allowed_methods = cors_config.allowed_methods
                .iter()
                .map(|m| format!("\"{}\"", m))
                .collect::<Vec<_>>()
                .join(", ");
                
            let _allowed_headers = cors_config.allowed_headers
                .iter()
                .map(|h| format!("\"{}\"", h))
                .collect::<Vec<_>>()
                .join(", ");
                
            // Add CORS middleware
            code = code.replace(
                "/// Handle HTTP request",
                r#"/// Apply CORS headers
fn apply_cors(builder: &mut http::response::Builder) {
    builder
        .header("Access-Control-Allow-Origin", "*")
        .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
        .header("Access-Control-Allow-Headers", "Content-Type, Authorization")
        .header("Access-Control-Allow-Credentials", "true")
        .header("Access-Control-Max-Age", "3600");
}

/// Handle HTTP request"#
            );
            
            // Use CORS in the handler
            code = code.replace(
                "// Add default content type if not present",
                r#"// Add CORS headers
    apply_cors(&mut builder);
    
    // Add default content type if not present"#
            );
        }
        
        Ok(code)
    }
}