Class WasiContext
- All Implemented Interfaces:
AutoCloseable
This class provides comprehensive WASI (WebAssembly System Interface) context management for Panama FFI-based WebAssembly execution. It includes:
- Configurable permission system with fine-grained controls
- Security validation and path traversal protection
- Resource limiting and quota enforcement
- Thread-safe context management
- Comprehensive error mapping for system operation failures
Security is the highest priority - all file system operations are validated against sandbox permissions and checked for path traversal attacks before execution.
This implementation uses Panama Foreign Function Interface for native interop and integrates with the ArenaResourceManager for proper memory management.
- Since:
- 1.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic WasiContextBuilderbuilder()Creates a new WASI context builder.voidclose()Closes this WASI context and releases all associated resources.String[]Gets the command-line arguments.Gets all environment variables.getEnvironmentVariable(String name) Gets an environment variable value.Gets the native handle for this WASI context.Gets all pre-opened directories.Gets the working directory.booleanisClosed()Checks if this WASI context has been closed.toString()validatePath(String path) Validates that a file path is accessible within the sandbox.
-
Method Details
-
getNativeHandle
Gets the native handle for this WASI context.- Returns:
- the native handle
- Throws:
IllegalStateException- if the context is closed
-
getEnvironmentVariable
Gets an environment variable value.- Parameters:
name- the environment variable name- Returns:
- the environment variable value, or null if not set
- Throws:
IllegalArgumentException- if name is null or emptyIllegalStateException- if the context is closed
-
getEnvironment
Gets all environment variables.- Returns:
- a copy of all environment variables
- Throws:
IllegalStateException- if the context is closed
-
getArguments
Gets the command-line arguments.- Returns:
- a copy of the command-line arguments
- Throws:
IllegalStateException- if the context is closed
-
getPreopenedDirectories
Gets all pre-opened directories.- Returns:
- a copy of all pre-opened directories
- Throws:
IllegalStateException- if the context is closed
-
getWorkingDirectory
Gets the working directory.- Returns:
- the working directory
- Throws:
IllegalStateException- if the context is closed
-
validatePath
Validates that a file path is accessible within the sandbox.Security validation checks for path traversal attacks. Filesystem sandboxing is enforced by the Wasmtime native runtime through pre-opened directory configuration.
- Parameters:
path- the file path to validate- Returns:
- the resolved and validated path
- Throws:
IllegalArgumentException- if path is null or emptyIllegalStateException- if the context is closed
-
isClosed
public boolean isClosed()Checks if this WASI context has been closed.- Returns:
- true if closed, false otherwise
-
close
public void close()Closes this WASI context and releases all associated resources.This method is idempotent and thread-safe. After calling this method, all other methods will throw IllegalStateException.
- Specified by:
closein interfaceAutoCloseable
-
builder
Creates a new WASI context builder.- Returns:
- a new WASI context builder
-
toString
-