-
-
Notifications
You must be signed in to change notification settings - Fork 5
Permissions
bizzehdee edited this page Jun 23, 2026
·
2 revisions
EnginePermissions is a flags enum that restricts which system resources a script can access. Pass it when registering the standard library.
using DScript.Extras;
// Allow everything (default — equivalent to EnginePermissions.All)
new EngineFunctionLoader().RegisterFunctions(engine);
// Deny all privileged operations
new EngineFunctionLoader().RegisterFunctions(engine, EnginePermissions.None);
// Selective access
new EngineFunctionLoader().RegisterFunctions(engine,
EnginePermissions.FileSystem | EnginePermissions.Network);| Flag | Guards |
|---|---|
FileSystem |
All fs.* operations (readFileSync, writeFileSync, mkdirSync, etc.) |
Network |
fetch(), http.createServer, net.createServer, net.createConnection
|
ProcessSpawn |
child_process.execSync, spawnSync, exec, spawn
|
ProcessExit |
process.exit() |
EnvironmentVariables |
process.env, process.getenv()
|
None |
No privileged operations allowed |
All |
All operations allowed (default) |
Attempting a denied operation throws PermissionException, which derives from ScriptException and can be caught inside a script's try/catch:
try {
var data = fs.readFileSync("/etc/passwd");
} catch (e) {
console.log("denied:", e.message);
}From C# it is also catchable as a PermissionException:
try
{
engine.Run(program);
}
catch (PermissionException ex)
{
Console.Error.WriteLine($"Permission denied: {ex.Message}");
}Run arbitrary user code with no system access at all:
var engine = new ScriptEngine();
new EngineFunctionLoader().RegisterFunctions(engine, EnginePermissions.None);
engine.SetTimeout(TimeSpan.FromSeconds(5));
engine.SetInstructionLimit(10_000_000);
try
{
engine.Run(ScriptEngine.Compile(userCode));
}
catch (ScriptTimeoutException) { /* killed by limit */ }
catch (PermissionException) { /* tried to access system */ }
catch (JITException ex) { /* script runtime error */ }