This is a fascinating project! Here's how you could architect this system and the security considerations:
Architecture Overview
Frontend (Browser) → Node.js Backend → SSH to VM → Sandboxed Linux Container
Implementation Approach
Frontend WebSocket Connection:
// WebSocket connection to your Node backend
const ws = new WebSocket('wss://yourserver.com/terminal');
ws.onmessage = (event) => {
// Display command output in terminal UI
terminal.write(event.data);
};
Node.js Backend with SSH:
const { Client } = require('ssh2');
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', (ws) => {
const conn = new Client();
conn.on('ready', () => {
conn.shell((err, stream) => {
stream.on('data', (data) => {
ws.send(data.toString());
});
ws.on('message', (message) => {
stream.write(message);
});
});
}).connect({
host: 'your-vm-ip',
username: 'restricted-user',
privateKey: require('fs').readFileSync('/path/to/key')
});
});
VM Setup:
Use Docker containers that reset daily via cron job. Create a restricted user with limited shell access.
Security Assessment
High Risk Issues
Command Injection: Users can execute arbitrary commands on your VM. Even with restrictions, this is inherently dangerous.
Resource Exhaustion: Users could spawn processes, fill disk space, or consume CPU/memory, affecting other users or your host system.
Privilege Escalation: Despite restrictions, users might find ways to escape the sandbox or gain elevated permissions.
Network Access: The VM could be used to attack other systems, making you liable for malicious activity originating from your infrastructure.
Mitigation Strategies
Container Isolation: Use Docker with strict resource limits, no network access, and read-only filesystem where possible.
User Restrictions: Create a heavily restricted user account with minimal permissions, custom shell with command filtering.
Network Segmentation: Isolate the VM network completely from your other infrastructure.
Monitoring: Log all commands and implement rate limiting on both WebSocket connections and command execution.
Daily Reset: Automate complete container destruction and recreation.
Input Validation: Filter dangerous commands at the Node.js level before sending to SSH.
Recommended Security Stack
# Docker container with restrictions
docker run --rm -it \
--memory=128m \
--cpus=0.5 \
--network=none \
--read-only \
--tmpfs /tmp \
--user 1000:1000 \
your-restricted-image
Additional Considerations:
- Implement session timeouts
- Rate limit connections per IP
- Use HTTPS/WSS only
- Consider legal liability for user actions
- Monitor for abuse patterns
- Have clear terms of service
This project walks the line between "really cool" and "security nightmare." The key is layered security - assume each layer will be bypassed and plan accordingly. Consider starting with a heavily restricted proof-of-concept before going full production.