แซนด์บ็อกซ์และสิทธิ์การใช้งาน
สร้างกลยุทธ์เพื่อจำกัดความสามารถของโมดูล WASM เพิ่มเติมและจัดการสิทธิ์ในสภาพแวดล้อมโฮสต์
แซนด์บ็อกซ์และสิทธิ์การใช้งาน เป็นบทเรียน WebAssembly (WASM) for High Performance Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebAssembly (WASM) for High Performance Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
WASM's Security Sandbox
WebAssembly (WASM) is designed with security at its core. It operates within a tightly controlled environment known as a sandbox.
This sandboxing mechanism isolates WASM code from the host system, preventing it from directly accessing sensitive resources or executing arbitrary operations outside its allocated space.
Inherent Restrictions
By default, a WASM module has no direct access to common system capabilities such as:
- The host's file system
- Network interfaces
- Environment variables
- Arbitrary memory outside its linear memory
This strict isolation is a fundamental security feature, making WASM a safe choice for executing untrusted code.
Granting Powers via Imports
If a WASM module needs to perform tasks like logging, accessing a file, or making a network request, it cannot do so directly. Instead, it must import functions from its host environment.
The host (e.g., a web browser's JavaScript engine or a WASI runtime) acts as a gatekeeper, explicitly providing these capabilities. This gives the host full control over what the WASM module can and cannot do.
WASM Requests Host Functions
Here's how a Rust-compiled WASM module might declare its need for a host function. It expects the host to provide a function called host_log within the env module.
/* src/lib.rs */
// Link to an imported function from the 'env' module
#[link(wasm_import_module = "env")]
extern "C" {
fn host_log(ptr: *const u8, len: usize);
}
#[no_mangle]
pub extern "C" fn log_message_from_wasm() {
let message = "Hello from WASM!";
let ptr = message.as_ptr();
let len = message.len();
unsafe {
// Call the imported host function
host_log(ptr, len);
}
}Host Grants or Denies Permission
The JavaScript host decides whether to provide the host_log function. If it's provided, WASM can call it. If not, the WASM module will fail to instantiate, enforcing the permission.
Try running this example:
// host.js
async function loadWasmWithPermission() {
const wasmBytes = await fetch("log_module.wasm").then(res => res.arrayBuffer());
// Define the imports object for WASM
const imports = {
env: {
host_log: (ptr, len) => {
// This function is provided by JS to WASM
const memory = instance.exports.memory;
const data = new Uint8Array(memory.buffer, ptr, len);
const text = new TextDecoder("utf-8").decode(data);
console.log(`WASM says: ${text}`);
}
}
};
try {
const { instance } = await WebAssembly.instantiate(wasmBytes, imports);
instance.exports.log_message_from_from_wasm(); // Call the WASM function
} catch (e) {
console.error("WASM instantiation failed:", e);
}
}
loadWasmWithPermission();Controlling WASM Memory
Beyond functions, the host environment also controls the WASM module's linear memory. When instantiating a module, the host can specify the initial and maximum memory size (in 64KB pages).
This prevents a malicious or buggy WASM module from consuming excessive memory and impacting the host application.
// host.js
async function loadWasmWithMemory() {
// Create a WebAssembly.Memory object
const memory = new WebAssembly.Memory({ initial: 1, maximum: 2 });
// 1 page (64KB) initial, max 2 pages (128KB)
const wasmBytes = await fetch("memory_user.wasm").then(res => res.arrayBuffer());
const imports = {
env: {
memory: memory // Pass the controlled memory object to WASM
}
};
const { instance } = await WebAssembly.instantiate(wasmBytes, imports);
console.log("WASM module loaded with controlled memory.");
// WASM can now access and grow this memory up to 2 pages
}
loadWasmWithMemory();Principle of Least Privilege
A critical security practice when working with WASM is the Principle of Least Privilege.
This means you should grant a WASM module only the permissions and resources it absolutely needs to perform its intended function, and no more. Minimizing access reduces the attack surface and potential impact of vulnerabilities.
Beyond Browser: WASI & Host Capabilities
For environments outside the web browser (like servers or IoT devices), the WebAssembly System Interface (WASI) standardizes system-level access.
Even with WASI, the underlying WASI runtime (e.g., Wasmtime, Wasmer) is the host. It still enforces permissions, deciding which file paths, network sockets, or environment variables a WASI module can access.
Secure Interaction Tips
To build secure WASM applications:
- Minimize Imports: Only provide host functions that are strictly necessary.
- Validate Inputs: Always validate data passed from WASM to JavaScript (and vice-versa) to prevent injection or buffer overflow issues.
- Memory Boundaries: Set appropriate initial and maximum memory limits.
- Trusted Modules: Only load WebAssembly modules from trusted and verified sources.
Permission Check
Consider a WebAssembly module that needs to read from a specific file. Which entity is primarily responsible for granting or denying this capability?
Recap: Secure WASM
We've learned that WebAssembly modules operate within a secure sandbox, relying entirely on the host environment to provide any external capabilities. Managing imports and controlling memory are crucial for maintaining security and adhering to the principle of least privilege.
Understanding these mechanisms helps you build robust, high-performance, and secure WebAssembly applications.
คำถามที่พบบ่อย
บทเรียน “แซนด์บ็อกซ์และสิทธิ์การใช้งาน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “แซนด์บ็อกซ์และสิทธิ์การใช้งาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebAssembly (WASM) for High Performance Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “แซนด์บ็อกซ์และสิทธิ์การใช้งาน”
สร้างกลยุทธ์เพื่อจำกัดความสามารถของโมดูล WASM เพิ่มเติมและจัดการสิทธิ์ในสภาพแวดล้อมโฮสต์ คุณปฏิบัติ WebAssembly (WASM) for High Performance Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebAssembly (WASM) for High Performance Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebAssembly (WASM) for High Performance Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “แซนด์บ็อกซ์และสิทธิ์การใช้งาน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebAssembly (WASM) for High Performance Apps นี้ได้ไหม
ได้ บทเรียน WebAssembly (WASM) for High Performance Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แบบจำลองความปลอดภัยของ WASM
- แซนด์บ็อกซ์และสิทธิ์การใช้งาน
- กลยุทธ์การนำไปใช้งานจริง
- ความปลอดภัยของห่วงโซ่อุปทานและการตรวจสอบโมดูล