2026-04-14 12:24:30 -04:00
|
|
|
'use strict';
|
2026-11-22 22:05:42 -05:00
|
|
|
const { evaluateCondition, checkCondition } = require('./condition');
|
2026-04-14 12:24:30 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate a webhook URL.
|
|
|
|
|
* Returns { ok: true, url } or { ok: false, reason }.
|
|
|
|
|
*/
|
|
|
|
|
function validateWebhookUrl(raw) {
|
|
|
|
|
if (!raw) return { ok: true, url: null };
|
|
|
|
|
let url;
|
|
|
|
|
try { url = new URL(raw); } catch { return { ok: false, reason: 'Invalid URL format' }; }
|
|
|
|
|
if (!['http:', 'https:'].includes(url.protocol)) {
|
|
|
|
|
return { ok: false, reason: 'Webhook URL must use http or https' };
|
|
|
|
|
}
|
|
|
|
|
const host = url.hostname.toLowerCase();
|
|
|
|
|
if (
|
|
|
|
|
host === 'localhost' ||
|
|
|
|
|
host === '::1' ||
|
|
|
|
|
/^127\./.test(host) ||
|
|
|
|
|
/^10\./.test(host) ||
|
|
|
|
|
/^192\.168\./.test(host) ||
|
|
|
|
|
/^172\.(1[6-9]|2\d|3[01])\./.test(host) ||
|
|
|
|
|
/^169\.254\./.test(host) ||
|
|
|
|
|
/^fe80:/i.test(host)
|
|
|
|
|
) {
|
|
|
|
|
return { ok: false, reason: 'Webhook URL must not point to a private/internal address' };
|
|
|
|
|
}
|
|
|
|
|
return { ok: true, url };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replace {{param}} placeholders in a command string.
|
|
|
|
|
* Throws if a substituted value contains unsafe characters.
|
|
|
|
|
*/
|
|
|
|
|
function applyParams(command, params) {
|
|
|
|
|
return command.replace(/\{\{(\w+)\}\}/g, (match, key) => {
|
|
|
|
|
if (!(key in params)) return match;
|
|
|
|
|
const val = String(params[key]).trim();
|
2026-04-14 12:41:09 -04:00
|
|
|
if (!/^[a-zA-Z0-9._:@/-]+$/.test(val)) {
|
2026-04-14 12:24:30 -04:00
|
|
|
throw new Error(`Unsafe value for workflow parameter "${key}"`);
|
|
|
|
|
}
|
|
|
|
|
return val;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-22 22:05:42 -05:00
|
|
|
* Evaluate a workflow condition with the safe interpreter in lib/condition.js (no vm,
|
|
|
|
|
* no JavaScript engine). Returns a boolean; a condition that can't be parsed or
|
|
|
|
|
* evaluated counts as false.
|
2026-04-14 12:24:30 -04:00
|
|
|
*/
|
|
|
|
|
function evalCondition(condition, state, params) {
|
|
|
|
|
try {
|
2026-11-22 22:05:42 -05:00
|
|
|
return evaluateCondition(condition, state, params);
|
2026-04-14 12:24:30 -04:00
|
|
|
} catch (e) {
|
2026-11-22 01:50:37 -05:00
|
|
|
console.warn(`[Workflow] evalCondition error (treated as false): ${e.message} (condition: ${condition})`);
|
2026-04-14 12:24:30 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 22:05:42 -05:00
|
|
|
/**
|
|
|
|
|
* Every condition in a workflow definition that doesn't parse, as
|
|
|
|
|
* [{ step, condition, error }]. Used to reject bad definitions when they are saved.
|
|
|
|
|
*/
|
|
|
|
|
function conditionErrors(definition) {
|
|
|
|
|
const problems = [];
|
|
|
|
|
let def = definition;
|
|
|
|
|
if (typeof def === 'string') {
|
|
|
|
|
try { def = JSON.parse(def); } catch { return problems; } // JSON errors are reported elsewhere
|
|
|
|
|
}
|
|
|
|
|
const steps = (def && Array.isArray(def.steps)) ? def.steps : [];
|
|
|
|
|
steps.forEach((step, i) => {
|
|
|
|
|
const label = (step && (step.id || step.name)) || `step ${i + 1}`;
|
|
|
|
|
const exprs = [];
|
|
|
|
|
if (step && step.condition) exprs.push(step.condition);
|
|
|
|
|
for (const c of (step && Array.isArray(step.conditions) ? step.conditions : [])) {
|
|
|
|
|
if (c && !c.default && c.if) exprs.push(c.if);
|
|
|
|
|
}
|
|
|
|
|
for (const expr of exprs) {
|
|
|
|
|
try { checkCondition(expr); } catch (e) { problems.push({ step: label, condition: expr, error: e.message }); }
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return problems;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 23:54:32 -05:00
|
|
|
/**
|
|
|
|
|
* Every jump in a workflow definition to a step id that doesn't exist (#20), as
|
|
|
|
|
* [{ step, goto, error }]. 'end' and '__end__' finish the workflow and are always valid.
|
|
|
|
|
*/
|
|
|
|
|
function gotoErrors(definition) {
|
|
|
|
|
let def = definition;
|
|
|
|
|
if (typeof def === 'string') {
|
|
|
|
|
try { def = JSON.parse(def); } catch { return []; }
|
|
|
|
|
}
|
|
|
|
|
const steps = (def && Array.isArray(def.steps)) ? def.steps : [];
|
|
|
|
|
const ids = new Set(steps.map(st => st && st.id).filter(Boolean));
|
|
|
|
|
const ok = target => target === 'end' || target === '__end__' || ids.has(target);
|
|
|
|
|
const problems = [];
|
|
|
|
|
steps.forEach((step, i) => {
|
|
|
|
|
if (!step) return;
|
|
|
|
|
const label = step.id || step.name || `step ${i + 1}`;
|
|
|
|
|
const targets = [];
|
|
|
|
|
if (step.goto) targets.push(step.goto);
|
|
|
|
|
for (const c of (Array.isArray(step.conditions) ? step.conditions : [])) if (c && c.goto) targets.push(c.goto);
|
|
|
|
|
if (step.routes && typeof step.routes === 'object') targets.push(...Object.values(step.routes));
|
|
|
|
|
for (const t of targets) {
|
|
|
|
|
if (typeof t !== 'string' || !ok(t)) problems.push({ step: label, goto: t, error: `no step with id ${JSON.stringify(t)}` });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return problems;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 12:24:30 -04:00
|
|
|
/**
|
|
|
|
|
* Calculate the next run date for a scheduled command.
|
|
|
|
|
*/
|
|
|
|
|
function calculateNextRun(scheduleType, scheduleValue) {
|
|
|
|
|
const cronParser = require('cron-parser');
|
|
|
|
|
const now = new Date();
|
|
|
|
|
|
|
|
|
|
if (scheduleType === 'interval') {
|
|
|
|
|
const minutes = parseInt(scheduleValue);
|
|
|
|
|
if (isNaN(minutes) || minutes <= 0) throw new Error(`Invalid interval value: ${scheduleValue}`);
|
|
|
|
|
return new Date(now.getTime() + minutes * 60000);
|
|
|
|
|
} else if (scheduleType === 'daily') {
|
|
|
|
|
const [hours, minutes] = scheduleValue.split(':').map(Number);
|
|
|
|
|
if (isNaN(hours) || isNaN(minutes)) throw new Error(`Invalid daily time format: ${scheduleValue}`);
|
|
|
|
|
const next = new Date(now);
|
|
|
|
|
next.setHours(hours, minutes, 0, 0);
|
|
|
|
|
if (next <= now) next.setDate(next.getDate() + 1);
|
|
|
|
|
return next;
|
|
|
|
|
} else if (scheduleType === 'hourly') {
|
|
|
|
|
const hours = parseInt(scheduleValue);
|
|
|
|
|
if (isNaN(hours) || hours <= 0) throw new Error(`Invalid hourly value: ${scheduleValue}`);
|
|
|
|
|
return new Date(now.getTime() + hours * 3600000);
|
|
|
|
|
} else if (scheduleType === 'cron') {
|
|
|
|
|
const interval = cronParser.CronExpressionParser.parse(scheduleValue, { currentDate: now });
|
|
|
|
|
return interval.next().toDate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Error(`Unknown schedule type: ${scheduleType}`);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 23:54:32 -05:00
|
|
|
module.exports = { validateWebhookUrl, applyParams, evalCondition, conditionErrors, gotoErrors, calculateNextRun };
|