Summary
Magistrala's Rules Engine allows authenticated users to create rules containing Go or Lua scripts that execute server-side when IoT messages arrive. Because the scripting environments expose file, database, environment, networking, and HTTP capabilities without sufficient restrictions, a low-privileged authenticated user may access internal resources or manipulate data.
Affected context
The issue occurs in Magistrala deployments where authenticated users can create or modify Rules Engine rules containing executable Go or Lua scripts. Risk is highest when the rule-processing service runs with access to sensitive files, environment variables, databases, or internal microservices, and when the embedded interpreters expose standard-library or auxiliary APIs without effective capability restrictions.
Exploit preconditions
- Attack surface: Rules Engine script execution in the server-side message-processing path
- Required access: An authenticated account permitted to create or modify rules; the described scenario includes low-privileged users
- Trigger point: Processing an IoT message that causes a stored Go or Lua rule to execute
- Why exploitation is possible: Go scripts run through Yaegi with broad standard-library symbols, while Lua scripts receive dangerous libraries without equivalent input validation or capability isolation
- Relevant capabilities: File operations, environment access, database operations, path handling, and outbound HTTP requests may be available to scripts
Why this happens
The underlying bug is insufficient isolation of user-controlled scripts from application and operating-system capabilities. The Go engine exposes broad Go standard-library functionality and relies on narrow regular-expression checks that do not remove dangerous APIs. The Lua engine lacks comparable validation while preloading libraries for file I/O, database access, path manipulation, and HTTP requests. This behavior maps to CWE-94: Improper Control of Generation of Code, because user-supplied rule content is treated as executable server-side logic without a sufficiently restricted execution model.
Potential impact
- An attacker may read, write, or delete files available to the service account
- Environment variables containing credentials or deployment metadata could be exposed
- Database records may be queried or modified
- The service could make unauthorized requests to internal microservices
- Internal network information may be disclosed through server-side request forgery
- Application data integrity or downstream IoT processing could be affected
- Broader compromise could occur if the service account has excessive permissions
General mitigation guidance
Mitigation generally focuses on removing unnecessary execution capabilities and enforcing a strict trust boundary around user-authored rules.
Common defensive patterns include:
- Prefer declarative, allowlisted rule expressions over general-purpose scripting
- Run scripts in a strongly isolated sandbox with no direct access to the host filesystem, environment, databases, or internal network
- Expose only narrowly scoped capabilities rather than complete standard libraries or general-purpose HTTP and file APIs
- Apply positive validation for permitted syntax, functions, modules, and resource limits instead of relying on blocklists
- Separate rule execution identities from sensitive application and database credentials
- Restrict outbound network access and require explicit destination allowlists
- Enforce least privilege for service accounts and isolate workloads from other tenant data
- Treat stored rules as untrusted input and require appropriate authorization, review, and audit controls
How teams detect issues like this
Organizations can identify this class of weakness through:
- Static analysis rules for interpreter initialization, broad standard-library exposure, and dynamic script evaluation
- Code review checks for user-controlled content reaching Go or Lua execution APIs
- Inventory of capabilities exposed to embedded interpreters, including file, database, environment, and network functions
- Policy checks that reject unrestricted HTTP clients, filesystem modules, or database handles in script runtimes
- Runtime monitoring for unusual file access, environment reads, database queries, and outbound connections from the Rules Engine
- Audit logging of rule creation, modification, execution failures, and capability usage
- Automated security review tools that flag code-execution sinks and missing sandbox boundaries

