phase 2 rework

This commit is contained in:
admin_rb committed 2026-10-23 18:19:44 +02:00
1 parent 20054198f3
commit b397ea9256
44 files changed
+5528 -1377

No files matched your search

+15 -62
View File
@@ -7,7 +7,6 @@ const { Duplex } = require('node:stream');
const http = require('node:http');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const express = require('express');
const WebSocket = require('ws');
const { config } = require('../lib/config');
@@ -368,69 +367,23 @@ test('server construction does not listen and heartbeat/server close clean up lo
assert.equal(cleared, 1);
});
test('PM2 browser controls use safe DOM text, visible feedback, ready-state checks, and stop on deselect', async () => {
class Element {
constructor(tag = 'div') {
this.tagName = tag;
this.className = '';
this.children = [];
this.dataset = {};
this.style = {};
this.textContent = '';
this.listeners = {};
this.scrollTop = this.scrollHeight = this.clientHeight = 0;
this.classList = {
add: (...names) => { this.className = [...new Set([...this.className.split(' '), ...names])].join(' '); },
remove: (...names) => { this.className = this.className.split(' ').filter(name => !names.includes(name)).join(' '); },
};
}
set innerHTML(value) { assert.fail('Untrusted refreshed process fields must not use innerHTML.'); }
addEventListener(name, callback) { (this.listeners[name] ||= []).push(callback); }
append(...values) { this.children.push(...values); }
appendChild(value) { this.children.push(value); return value; }
replaceChildren(...values) { this.children = values; }
async click() { for (const callback of this.listeners.click || []) await callback({ preventDefault() {}, currentTarget: this }); }
}
const elements = new Map();
const get = id => { if (!elements.has(id)) elements.set(id, new Element()); return elements.get(id); };
const document = {
getElementById: get,
createElement: tag => new Element(tag),
querySelectorAll: () => get('process-list').children.filter(node => node.className?.split(' ').includes('pm2-row')),
};
let socket;
class BrowserSocket {
static OPEN = 1;
constructor() { socket = this; this.readyState = 0; this.sent = []; this.listeners = {}; }
addEventListener(name, callback) { this.listeners[name] = callback; }
send(text) { assert.equal(this.readyState, 1); this.sent.push(JSON.parse(text)); }
}
const maliciousName = '<img src=x onerror=alert(1)>';
const process = { name: maliciousName, pm_id: '0', pid: '12', status: '<script>fixture</script>', execPath: '<b>fixture</b>', uptime: '1m' };
const context = vm.createContext({
document,
WebSocket: BrowserSocket,
window: { addEventListener() {} },
location: { protocol: 'http:', host: 'fixture.invalid' },
fetch: async () => ({ ok: true, json: async () => ({ ok: true, processes: [process] }) }),
test('worker view escapes process fields and exposes accessible scoped controls', async () => {
const filename = path.join(config.projectRoot, 'views/pm2.ejs');
const template = fs.readFileSync(filename, 'utf8');
const attack = '<img src=x onerror="alert(1)">';
const rendered = await require('ejs').renderFile(filename, {
processes: [{ name: attack, pm_id: '0', pid: '12', status: attack, execPath: attack,
cpu: '1.2%', memoryMB: '40 MB', uptime: '1m' }],
});
const template = fs.readFileSync(path.join(config.projectRoot, 'views/pm2.ejs'), 'utf8');
assert.equal(template.includes('id="btn-save"'), false);
assert.equal(template.includes('/home/pm2/'), false);
assert.ok(template.indexOf('id="control-output"') < template.indexOf('id="control-centre"'));
const script = template.match(/<script>([\s\S]*?)<\/script>/)[1];
vm.runInContext(script + '\nglobalThis.fixture = { refreshList, sendLogRequest, appendLog };', context);
assert.equal(context.fixture.sendLogRequest({ type: 'logs', name: 'worker-one' }, false), false);
assert.equal(socket.sent.length, 0);
await context.fixture.refreshList();
const row = document.querySelectorAll()[0];
assert.equal(row.children[0].children[0].textContent, maliciousName);
assert.equal(row.dataset.name, maliciousName);
socket.readyState = 1;
await row.click();
assert.equal(socket.sent.at(-1).type, 'logs');
await row.click();
assert.equal(socket.sent.at(-1).type, 'stop-logs');
context.fixture.appendLog('x'.repeat(200_000));
assert.ok(get('logs').textContent.length < 66 * 1024);
assert.match(template, /<script src="\/workers\.js" defer><\/script>/);
assert.doesNotMatch(template, /<script>/);
assert.ok(!rendered.includes(attack));
assert.match(rendered, /&lt;img src=x onerror=&#34;alert\(1\)&#34;&gt;/);
assert.match(rendered, /type="button" class="worker-row" aria-pressed="false"/);
assert.match(rendered, /data-pmid="0"/);
assert.match(rendered, /for="worker-name"/);
assert.match(rendered, /role="status" aria-live="polite"/);
});