UNPKG

webssh2-server

Version:

A Websocket to SSH2 gateway using xterm.js, socket.io, ssh2

54 lines (53 loc) 1.91 kB
// server // app/validators/exec-validate.ts // Lightweight runtime validation for exec payloads (no external deps) import { isValidEnvKey, isValidEnvValue } from '../utils.js'; export function validateExecPayload(payload) { if (payload === null || payload === undefined || typeof payload !== 'object') { throw new Error('payload must be an object'); } const p = payload; if (typeof p['command'] !== 'string' || String(p['command']).trim() === '') { throw new Error('command must be a non-empty string'); } const out = { command: String(p['command']).trim() }; if (p['pty'] != null) { out.pty = Boolean(p['pty']); } if (p['term'] != null) { const term = p['term']; if (typeof term === 'string' || typeof term === 'number') { out.term = String(term); } } if (p['cols'] != null) { out.cols = Number(p['cols']); } if (p['rows'] != null) { out.rows = Number(p['rows']); } if (p['env'] != null) { if (Array.isArray(p['env'])) { const arr = p['env']; const entries = arr.slice(0, 50).map((v, i) => [String(i), String(v)]); out.env = Object.fromEntries(entries); } else if (typeof p['env'] === 'object') { const src = p['env']; const entries = Object.entries(src) .filter(([k, v]) => typeof k === 'string' && isValidEnvKey(k) && v != null && (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') && isValidEnvValue(String(v))) .slice(0, 50) .map(([k, v]) => [k, String(v)]); out.env = Object.fromEntries(entries); } else { out.env = {}; } } if (p['timeoutMs'] != null) { out.timeoutMs = Number(p['timeoutMs']); } return out; }