Safer workflow conditions; Pulse is admin-only
CI / UI screenshots (before/after) (push) Skipped
CI / Lint (eslint) (push) Successful in 25s
CI / Test (jest) (push) Successful in 25s
CI / Security (npm audit) (push) Successful in 21s
CI / Scan (secrets, workflows, shell) (push) Successful in 2m0s
CI / Integration (api) (push) Successful in 2m23s
CI / Lint (eslint) (pull_request) Successful in 24s
CI / Test (jest) (pull_request) Successful in 32s
CI / Security (npm audit) (pull_request) Successful in 39s
CI / UI (playwright) (push) Successful in 2m6s
CI / Deploy (push) Skipped
CI / Deploy worker (push) Skipped
CI / Notify (push) Skipped
CI / Integration (api) (pull_request) Successful in 2m13s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 2m46s
CI / UI screenshots (before/after) (pull_request) Successful in 27s
CI / UI (playwright) (pull_request) Successful in 3m5s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped

- Workflow conditions (step.condition and route `if`) are now parsed and
  interpreted by lib/condition.js instead of being run with Node's vm
  module. The language covers what workflows use: state/params lookups,
  parseInt/parseFloat/Number/String/Boolean, a few string methods, the
  usual operators with JavaScript semantics. Conditions can only read the
  workflow's own data. Every condition in the production workflows gives
  the same result as before (differential test, 150 random states each).
- Creating and updating a workflow rejects conditions that don't parse
  (400 with the step and the error).
- Creating a workflow requires admin, like editing already did.
- Only the admin group may use Pulse (pages, API and live WebSocket);
  employees get 403. Gandalf and workers use API keys and are unaffected.
- The live-update UI test finds its execution's row by id instead of
  counting rows (the list is capped at 50).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-22 22:05:42 -05:00
1 parent f5a4333a8e
commit 040ad028c0
10 files changed
+453 -20

No files matched your search

+135
View File
@@ -0,0 +1,135 @@
'use strict';
const vm = require('vm');
const { evaluateCondition, checkCondition } = require('../lib/condition');
const { evalCondition, conditionErrors } = require('../lib/utils');
// Every condition used by the production workflows (2026-11-22).
const PRODUCTION = [
"params.switch_ip && params.switch_ip.trim() !== ''",
"(state.carrier||'x') === 'error'",
"(state.carrier||'1') === '0' && (state.has_dom||'no') === 'yes'",
"(state.carrier||'1') === '0'",
"parseInt(state.carrier_changes||'0') > 100 && (state.has_dom||'no') === 'yes'",
"parseInt(state.carrier_changes||'0') > 100",
"parseInt(state.carrier_changes||'0') > 20 && (state.has_dom||'no') === 'yes'",
"parseInt(state.carrier_changes||'0') > 20",
"(state.duplex||'full') === 'half'",
"parseInt(state.crc_errors||'0') > 10 || parseInt(state.frame_errors||'0') > 10",
"(state.has_dom||'no') === 'no'",
"state.sfp_rx_dbm === 'no_dom' || parseFloat(state.sfp_rx_dbm||'0') < -26",
"parseFloat(state.sfp_rx_dbm||'0') < -18",
"parseFloat(state.sfp_tx_dbm||'0') < -10",
"(state.carrier||'0') === '1' && (state.operstate||'down') === 'up'",
"state.sfp_rx_dbm && state.sfp_rx_dbm !== 'no_dom' && parseFloat(state.sfp_rx_dbm||'0') < -25",
"state.sfp_rx_dbm && state.sfp_rx_dbm !== 'no_dom' && parseFloat(state.sfp_rx_dbm||'0') < -18",
"(state.carrier||'0') === '0'",
"(state.duplex||'half') === 'full' && parseInt(state.crc_errors||'0') < 10",
"(state.duplex||'half') === 'full'",
"(state.has_dom||'no') === 'yes' && state.sfp_rx_dbm !== 'no_dom' && parseFloat(state.sfp_rx_dbm||'0') < -18",
"(state.has_dom||'no') === 'yes'",
"state.ring_rx_cur && state.ring_rx_max && state.ring_rx_cur !== 'unknown' && state.ring_rx_max !== 'unknown' && parseInt(state.ring_rx_cur||'0') < parseInt(state.ring_rx_max||'0') && parseInt(state.rx_dropped||'0') + parseInt(state.rx_missed||'0') > 0",
"parseInt(state.rx_dropped||'0') + parseInt(state.rx_missed||'0') > 100",
"(state.flowctl_rx||'on') === 'on' || (state.flowctl_tx||'on') === 'on'",
"state.speed && state.speed !== '-1' && parseInt(state.speed||'0') > 0 && (state.duplex||'full') === 'full'",
];
const VALUES = [undefined, '', '0', '1', '-1', '5', '11', '25', '150', '1000', '-12', '-20', '-27', 'error', 'yes', 'no',
'no_dom', 'full', 'half', 'on', 'off', 'up', 'down', 'unknown', ' 10.0.0.1 '];
// Small deterministic PRNG so failures are reproducible.
function rng(seed) { return () => (seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648; }
describe('production conditions behave exactly as plain JavaScript did', () => {
for (const cond of PRODUCTION) {
test(cond, () => {
expect(() => checkCondition(cond)).not.toThrow();
const keys = [...new Set([...cond.matchAll(/(?:state|params)\.([a-z_]+)/g)].map(m => m[1]))];
const rand = rng(cond.length);
for (let i = 0; i < 150; i++) {
const state = {}; const params = {};
for (const k of keys) {
const v = VALUES[Math.floor(rand() * VALUES.length)];
if (v !== undefined) { state[k] = v; params[k] = v; }
}
// Reference: the old behaviour (vm), on trusted test input only.
const expected = !!vm.runInNewContext(cond, { state, params, parseInt, parseFloat });
expect(evaluateCondition(cond, state, params)).toBe(expected);
}
});
}
});
describe('the language', () => {
const ev = (c, state = {}, params = {}) => evaluateCondition(c, state, params);
test('operators, precedence and literals', () => {
expect(ev('1 + 2 * 3 === 7')).toBe(true);
expect(ev('(1 + 2) * 3 === 9')).toBe(true);
expect(ev('!false && (null || "x") === "x"')).toBe(true);
expect(ev('-5 < -4 && 10 % 3 === 1 && 7 / 2 === 3.5')).toBe(true);
expect(ev("'a' + 1 === 'a1'")).toBe(true);
expect(ev('1 == "1" && 1 != 2')).toBe(true);
expect(ev("state.x === undefined", {})).toBe(true);
expect(ev("'it\\'s' === \"it's\"")).toBe(true);
});
test('functions and string methods', () => {
expect(ev("parseInt('42px') === 42 && parseFloat('-3.5') === -3.5")).toBe(true);
expect(ev("Number('7') === 7 && String(7) === '7' && Boolean('')=== false")).toBe(true);
expect(ev("state.h.toLowerCase().startsWith('web') && state.h.includes('01')", { h: 'WEB-01' })).toBe(true);
expect(ev("state.h.length === 6", { h: 'WEB-01' })).toBe(true);
expect(ev('promptResponse === "Yes"', { promptResponse: 'Yes' })).toBe(true);
});
});
describe('a condition can only read the workflow data', () => {
const rejected = [
'this',
'state.constructor',
'state.__proto__',
'params.prototype',
"state['x']",
'globalThis',
'process',
'require',
'Function',
'eval("1")',
'state.x = 1',
'(() => 1)()',
'`template`',
'1; 2',
"state.x.toString()",
"'a'.constructor",
"parseInt.call(null, '1')",
];
test.each(rejected)('%s is refused', cond => {
expect(() => checkCondition(cond)).toThrow();
expect(evalCondition(cond, {}, {})).toBe(false); // and the engine treats it as false
});
test('inherited properties are invisible', () => {
expect(evaluateCondition('state.toString === undefined && state.hasOwnProperty === undefined', {}, {})).toBe(true);
});
test('string methods only work on strings', () => {
expect(evalCondition('state.n.trim() === "1"', { n: 1 }, {})).toBe(false);
});
test('huge or deeply nested input is refused', () => {
expect(() => checkCondition('1+'.repeat(2000) + '1')).toThrow();
expect(() => checkCondition('('.repeat(200) + '1' + ')'.repeat(200))).toThrow();
});
});
describe('conditionErrors', () => {
test('lists every condition that does not parse, by step', () => {
const problems = conditionErrors({ steps: [
{ id: 'ok', type: 'execute', condition: "params.env === 'prod'" },
{ id: 'bad', type: 'execute', condition: 'state.constructor' },
{ id: 'route', type: 'route', conditions: [{ if: 'state.x ===' }, { default: true, goto: 'end' }] },
] });
expect(problems.map(p => p.step)).toEqual(['bad', 'route']);
});
test('accepts a JSON string definition and ignores malformed JSON', () => {
expect(conditionErrors(JSON.stringify({ steps: [{ condition: 'process' }] }))).toHaveLength(1);
expect(conditionErrors('{not json')).toEqual([]);
});
});
+7
View File
@@ -15,6 +15,13 @@ describe('HTTP', () => {
expect((await api('GET', '/api/user', { headers: { ...SSO, 'Remote-Groups': 'guests' } })).status).toBe(403);
});
test('Pulse is admin-only: employees are refused on the API and the pages', async () => {
const employee = { ...SSO, 'Remote-Groups': 'employee' };
expect((await api('GET', '/api/user', { headers: employee })).status).toBe(403);
expect((await api('GET', '/api/workers', { headers: employee })).status).toBe(403);
expect((await api('GET', '/', { headers: employee })).status).toBe(403);
});
test('worker heartbeats need the worker key', async () => {
const r = await api('POST', '/api/workers/heartbeat', {
headers: { 'X-API-Key': 'wrong' }, body: { worker_id: 'x', name: 'x' },
+14
View File
@@ -155,3 +155,17 @@ test.failing('a goto to a missing step fails the execution (#20)', async () => {
const ex = await waitForExecution(id);
expect(ex.status).toBe('failed');
});
test('a workflow whose condition does not parse is refused on save', async () => {
const create = await sso('POST', '/api/workflows', { name: `int ${unique()}`, definition: { steps: [
{ id: 'a', type: 'execute', command: 'true', targets: ['node-01'], condition: 'state.constructor' },
] } });
expect(create.status).toBe(400);
expect(create.json.problems[0]).toMatchObject({ step: 'a', condition: 'state.constructor' });
const id = await createWorkflow([{ id: 'ok', type: 'execute', command: 'true', targets: ['node-01'] }]);
const update = await sso('PUT', `/api/workflows/${id}`, { name: 'x', definition: { steps: [
{ id: 'r', type: 'route', conditions: [{ if: 'process', goto: 'end' }] },
] } });
expect(update.status).toBe(400);
});
+3 -3
View File
@@ -52,14 +52,14 @@ test('multi-worker mode sends the command to every selected worker', async ({ pa
test('Executions updates live when a command starts elsewhere', async ({ page, request }) => {
await page.goto('/executions');
await page.waitForLoadState('networkidle');
const rows = page.locator('#ex-list-manual .ex-row');
const before = await rows.count();
// Start a command through the API, not this page: only the WebSocket can tell it.
const workers = await (await request.get('/api/workers')).json();
const node3 = workers.find(w => w.name === 'node-03');
const res = await request.post(`/api/workers/${node3.id}/command`, { data: { command: `echo ${token()}` } });
expect(res.ok()).toBeTruthy();
const { execution_id: id } = await res.json();
await expect(rows).toHaveCount(before + 1);
// Its own row appears without a reload (by id, so it works however full the list is).
await expect(page.locator(`#ex-list-manual .ex-row[data-execution-id="${id}"]`)).toBeVisible();
});
+3 -1
View File
@@ -47,7 +47,9 @@ describe('classifyUpgrade', () => {
const opts = (enforce) => ({ trusted, workerKey: KEY, enforce });
test('trusted proxy with an allowed group is a browser', () => {
expect(classifyUpgrade(req(PROXY, sso('employee')), opts(true))).toBe('browser');
expect(classifyUpgrade(req(PROXY, sso('admin')), opts(true))).toBe('browser');
// Pulse is admin-only: employees are not browsers either.
expect(classifyUpgrade(req(PROXY, sso('employee')), opts(true))).toBe('reject');
});
test('trusted proxy with a wrong group is not a browser', () => {