SceneGuard is a library designed to solve the challenge of securely running third-party code in Babylon.js web applications. It addresses the following problems:
- Security risks associated with executing untrusted scripts
- Performance issues caused by third-party code interfering with the main rendering thread
- Lack of controlled access to Babylon.js scene elements for external scripts
SceneGuard employs a sandboxed execution model with the following key components:
- SceneGuard: Main entry point that initializes the secure environment
- SceneBridge: Manages communication between the main thread and worker
- GuardedWorker: Isolated Web Worker environment for running third-party code
- GuardedAPI: Provides a controlled interface for third-party scripts to interact with Babylon.js elements
- EntityHandle: Represents a reference to Babylon.js entities without exposing direct access
The architecture uses message passing between the main thread and worker to ensure isolation and controlled access to scene elements.
import { Engine, Scene } from '@babylonjs/core';
import { SceneGuard } from 'sceneguard';
// Create Babylon.js scene
const canvas = document.getElementById('renderCanvas') as HTMLCanvasElement;
const engine = new Engine(canvas);
const scene = new Scene(engine);
// Initialize SceneGuard
const sceneGuard = new SceneGuard(scene, engine);
// Load a third-party script
sceneGuard.loadScript('https://example.com/third-party-script.js');
// In third-party-script.js
// Create a sphere
const sphereHandle = GuardedAPI.createMesh({
type: 'sphere',
diameter: 2,
segments: 32,
position: new Vector3(0, 1, 0),
});
// Add a rotation behavior
GuardedAPI.addBehavior(sphereHandle, {
type: 'rotate',
options: {
axis: new Vector3(0, 1, 0),
speed: 0.5,
},
});
// Observe the sphere's rotation
const observerId = GuardedAPI.observe(sphereHandle, 'rotation', data => {
console.log(`Rotation: x=${data.x}, y=${data.y}, z=${data.z}`);
});
// Clean up when done
GuardedAPI.unobserve(observerId);
GuardedAPI.disposeEntity(sphereHandle);SceneGuard supports the following mesh types:
box: Create a box with size or width/height/depthsphere: Create a sphere with diameter and segmentscylinder: Create a cylinder with height, diameter, and tessellationground: Create a ground plane with width, height, and subdivisionsplane: Create a plane with size or width/heighttorus: Create a torus with diameter, thickness, and tessellationtorusKnot: Create a torus knot with radius, tube, and segmentstube: Create a tube along a path with radius and tessellation
SceneGuard provides the following behaviors:
rotate: Rotate an entity around a specified axisscale: Scale an entity to a target sizemove: Move an entity to a target position
You can observe the following properties of entities:
position: Track position changesrotation: Track rotation changesscaling: Track scaling changesbeforeRender: Track scene render events
We welcome contributions to enhance SceneGuard! Here's how you can contribute:
- Fork the repository on GitHub
- Clone your forked repository to your local machine
- Create a new branch for your feature or bug fix
- Make your changes and commit them with clear, descriptive messages
- Push your changes to your fork on GitHub
- Create a pull request from your fork to the main SceneGuard repository
Please ensure your code follows the existing style and includes appropriate tests. For major changes, please open an issue first to discuss the proposed changes.
SceneGuard is released under the MIT License. See the LICENSE file for details.