mirror of
https://github.com/UNITRONIX/BetterDesk.git
synced 2026-09-10 17:45:42 +00:00
0f161181f1
Privilege separation across all installers so the long-running services no longer run with full administrative rights: betterdesk.sh: installer keeps root but systemd units now run as a dedicated unprivileged 'betterdesk' system account by default (auto-created via ensure_service_user). Added full systemd hardening for the Go server (NoNewPrivileges, ProtectSystem=strict, ProtectHome, PrivateTmp, ReadWritePaths) and light hardening for the Node.js console. chown migrates existing root-owned data to the service account on update. Opt-out via --run-as-root / BETTERDESK_RUN_AS_ROOT=1; custom account via BETTERDESK_SERVICE_USER. Minimal mode covered too. betterdesk.ps1: NSSM services now run under their per-service low-privilege virtual accounts (NT SERVICE\<service>) instead of LocalSystem, with scoped icacls grants on the install/data dirs (Set-ServiceLeastPrivilege helper). Applied to the Go server, Node.js console and minimal-mode service. Opt-out via -RunAsRoot / BETTERDESK_RUN_AS_ROOT=1. Docker: verified already privilege-separated (supervisord drops both programs to user=betterdesk; multi-container images drop via su-exec). Also bundles in-progress changes to the Go server API, Node.js console services and Docker compose/Dockerfiles. This commit was made possible thanks to Insolve.
152 lines
5.7 KiB
JavaScript
152 lines
5.7 KiB
JavaScript
/**
|
|
* BetterDesk Console — CDAP Terminal WebSocket Proxy
|
|
* Proxies terminal WebSocket connections from the browser to the Go server's
|
|
* CDAP terminal endpoint. Authenticates via session cookie.
|
|
*
|
|
* Browser ←WS→ Node.js (:5000) ←WS→ Go API (:21121)
|
|
* /api/cdap/devices/:id/terminal → /api/cdap/devices/:id/terminal
|
|
*/
|
|
|
|
const WebSocket = require('ws');
|
|
const config = require('../config/config');
|
|
|
|
/**
|
|
* Initialize the CDAP terminal WebSocket proxy and attach to HTTP server.
|
|
* @param {import('http').Server} server
|
|
* @param {Function} sessionMiddleware - Express session middleware for auth
|
|
*/
|
|
function initCdapTerminalProxy(server, sessionMiddleware) {
|
|
const wss = new WebSocket.Server({ noServer: true });
|
|
const { enforceOrigin } = require('../middleware/wsOrigin');
|
|
|
|
server.on('upgrade', (req, socket, head) => {
|
|
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
const pathname = url.pathname;
|
|
|
|
// Match /api/cdap/devices/:id/terminal
|
|
const match = pathname.match(/^\/api\/cdap\/devices\/([A-Za-z0-9_-]{6,30})\/terminal$/);
|
|
if (!match) return; // Let other upgrade handlers deal with it
|
|
|
|
const deviceId = match[1];
|
|
|
|
// CSWSH protection — reject before validating session.
|
|
if (!enforceOrigin(req, socket, `cdap-terminal ${pathname}`)) return;
|
|
|
|
// Require session authentication
|
|
sessionMiddleware(req, {}, () => {
|
|
if (!req.session || !req.session.userId) {
|
|
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
|
|
socket.destroy();
|
|
return;
|
|
}
|
|
|
|
// Session may store user under req.session.user (object) or as
|
|
// flat fields. Accept either; treat super_admin/admin as admin.
|
|
const sessUser = req.session.user || {};
|
|
const userRole = sessUser.role || req.session.role || '';
|
|
const userName = sessUser.username || req.session.username || `user#${req.session.userId}`;
|
|
|
|
// RBAC: only admin / super_admin users can access terminal
|
|
if (userRole !== 'admin' && userRole !== 'super_admin') {
|
|
console.warn(`[CDAP Terminal] 403 upgrade rejected (user=${userName} role=${userRole})`);
|
|
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
|
|
socket.destroy();
|
|
return;
|
|
}
|
|
|
|
req._cdapUserName = userName;
|
|
req._cdapUserRole = userRole;
|
|
|
|
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
wss.emit('connection', ws, req, deviceId);
|
|
});
|
|
});
|
|
});
|
|
|
|
wss.on('connection', (browserWs, req, deviceId) => {
|
|
const username = req._cdapUserName || req.session?.user?.username || 'admin';
|
|
const role = req._cdapUserRole || req.session?.user?.role || 'admin';
|
|
console.log(`[CDAP Terminal] Proxy session started for device ${deviceId} by ${username}`);
|
|
|
|
// Build Go server WebSocket URL
|
|
const goApiBase = config.betterdeskApiUrl || 'http://localhost:21121/api';
|
|
const goWsUrl = goApiBase
|
|
.replace(/^http/, 'ws')
|
|
.replace(/\/api\/?$/, '') +
|
|
`/api/cdap/devices/${encodeURIComponent(deviceId)}/terminal`;
|
|
|
|
// Connect to Go server terminal endpoint
|
|
const goWs = new WebSocket(goWsUrl, ['cdap-terminal'], {
|
|
headers: {
|
|
'X-API-Key': config.betterdeskApiKey || '',
|
|
'X-Username': username,
|
|
'X-Role': role
|
|
},
|
|
// Allow self-signed certs for local Go server
|
|
rejectUnauthorized: !config.allowSelfSignedCerts
|
|
});
|
|
|
|
let goConnected = false;
|
|
// Buffer messages that arrive before upstream is open (race fix).
|
|
const pendingBrowserMsgs = [];
|
|
|
|
goWs.on('open', () => {
|
|
goConnected = true;
|
|
while (pendingBrowserMsgs.length > 0) {
|
|
const buffered = pendingBrowserMsgs.shift();
|
|
try { goWs.send(buffered); } catch (_) { /* ignore */ }
|
|
}
|
|
});
|
|
|
|
// Relay: Browser → Go
|
|
browserWs.on('message', (data) => {
|
|
if (goConnected && goWs.readyState === WebSocket.OPEN) {
|
|
goWs.send(data);
|
|
} else if (goWs.readyState === WebSocket.CONNECTING) {
|
|
pendingBrowserMsgs.push(data);
|
|
}
|
|
});
|
|
|
|
// Relay: Go → Browser
|
|
goWs.on('message', (data) => {
|
|
if (browserWs.readyState === WebSocket.OPEN) {
|
|
browserWs.send(data);
|
|
}
|
|
});
|
|
|
|
// Handle disconnection
|
|
browserWs.on('close', () => {
|
|
console.log(`[CDAP Terminal] Browser disconnected for device ${deviceId}`);
|
|
if (goWs.readyState === WebSocket.OPEN || goWs.readyState === WebSocket.CONNECTING) {
|
|
goWs.close();
|
|
}
|
|
});
|
|
|
|
goWs.on('close', () => {
|
|
if (browserWs.readyState === WebSocket.OPEN) {
|
|
browserWs.close();
|
|
}
|
|
});
|
|
|
|
goWs.on('error', (err) => {
|
|
console.error(`[CDAP Terminal] Go server WS error for ${deviceId}:`, err.message);
|
|
if (browserWs.readyState === WebSocket.OPEN) {
|
|
browserWs.send(JSON.stringify({ type: 'error', error: 'Server connection failed' }));
|
|
browserWs.close();
|
|
}
|
|
});
|
|
|
|
browserWs.on('error', (err) => {
|
|
console.error(`[CDAP Terminal] Browser WS error for ${deviceId}:`, err.message);
|
|
if (goWs.readyState === WebSocket.OPEN) {
|
|
goWs.close();
|
|
}
|
|
});
|
|
});
|
|
|
|
console.log('[CDAP Terminal] WebSocket proxy initialized (/api/cdap/devices/:id/terminal)');
|
|
return wss;
|
|
}
|
|
|
|
module.exports = { initCdapTerminalProxy };
|