feat(workflows,transfers,broadcast): harden templates/rsync/SFTP-transfer, add http/notify steps, retries, secret inputs & broadcast history
This commit is contained in:
parent
d5822c9eca
commit
2b878c1d99
8 changed files with 1217 additions and 168 deletions
|
|
@ -10,13 +10,17 @@ export interface StepOutputSummary {
|
|||
export interface TemplateContext {
|
||||
workflowId?: string;
|
||||
runId?: string;
|
||||
triggerSource?: string;
|
||||
serverId?: string;
|
||||
serverMeta?: { id: string; name: string; host: string; port: number };
|
||||
inputs: Record<string, string>;
|
||||
stepOutputs?: Map<string, StepOutputSummary>;
|
||||
captures?: Map<string, string>;
|
||||
eventMeta?: Record<string, string>;
|
||||
}
|
||||
|
||||
const FORBIDDEN_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
||||
|
||||
export function toEnvIdent(raw: string): string {
|
||||
return raw.replace(/[^A-Za-z0-9]/g, '_').toUpperCase();
|
||||
}
|
||||
|
|
@ -27,8 +31,15 @@ export function validateAndResolveInputs(
|
|||
): Record<string, string> {
|
||||
const out: Record<string, string> = {};
|
||||
|
||||
for (const k of Object.keys(supplied)) {
|
||||
if (FORBIDDEN_KEYS.has(k)) {
|
||||
throw new Error(`Unsafe input parameter name "${k}"`);
|
||||
}
|
||||
}
|
||||
|
||||
for (const def of defs) {
|
||||
const raw = supplied[def.name];
|
||||
if (FORBIDDEN_KEYS.has(def.name)) continue;
|
||||
const raw = Object.hasOwn(supplied, def.name) ? supplied[def.name] : undefined;
|
||||
const hasSupplied = raw !== undefined && raw !== null && raw !== '';
|
||||
const candidate = hasSupplied ? raw : def.default;
|
||||
|
||||
|
|
@ -61,6 +72,9 @@ export function validateAndResolveInputs(
|
|||
}
|
||||
|
||||
const strVal = String(candidate);
|
||||
if (/[\x00]/.test(strVal)) {
|
||||
throw new Error(`Input "${def.name}" contains NUL bytes`);
|
||||
}
|
||||
if (def.type === 'select' && def.options && def.options.length > 0) {
|
||||
if (!def.options.includes(strVal)) {
|
||||
throw new Error(
|
||||
|
|
@ -72,14 +86,48 @@ export function validateAndResolveInputs(
|
|||
}
|
||||
|
||||
for (const [k, v] of Object.entries(supplied)) {
|
||||
if (!(k in out) && v !== undefined && v !== null) {
|
||||
out[k] = String(v);
|
||||
if (FORBIDDEN_KEYS.has(k)) continue;
|
||||
if (!Object.hasOwn(out, k) && v !== undefined && v !== null) {
|
||||
const s = String(v);
|
||||
if (/[\x00]/.test(s)) {
|
||||
throw new Error(`Input "${k}" contains NUL bytes`);
|
||||
}
|
||||
out[k] = s;
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
export function maskSecretInputsForStorage(
|
||||
defs: InputDef[],
|
||||
resolved: Record<string, string>,
|
||||
): { masked: Record<string, string>; secretValues: string[] } {
|
||||
const secretKeys = new Set(defs.filter((d) => d.type === 'secret').map((d) => d.name));
|
||||
const masked: Record<string, string> = {};
|
||||
const secretValues: string[] = [];
|
||||
for (const [k, v] of Object.entries(resolved)) {
|
||||
if (secretKeys.has(k) && v.length > 0) {
|
||||
masked[k] = '***REDACTED***';
|
||||
secretValues.push(v);
|
||||
} else {
|
||||
masked[k] = v;
|
||||
}
|
||||
}
|
||||
return { masked, secretValues };
|
||||
}
|
||||
|
||||
export function redactWorkflowSecrets(text: string, secretValues: string[]): string {
|
||||
if (!text || secretValues.length === 0) return text;
|
||||
let out = text;
|
||||
for (const sec of secretValues) {
|
||||
if (sec.length >= 2 && out.includes(sec)) {
|
||||
out = out.split(sec).join('***REDACTED***');
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function buildWorkflowEnv(ctx: TemplateContext): Record<string, string> {
|
||||
const env: Record<string, string> = {};
|
||||
if (ctx.workflowId) env.GCOORD_WORKFLOW_ID = ctx.workflowId;
|
||||
|
|
@ -87,6 +135,7 @@ export function buildWorkflowEnv(ctx: TemplateContext): Record<string, string> {
|
|||
if (ctx.serverId) env.GCOORD_SERVER_ID = ctx.serverId;
|
||||
|
||||
for (const [k, v] of Object.entries(ctx.inputs)) {
|
||||
if (FORBIDDEN_KEYS.has(k)) continue;
|
||||
env[`GCOORD_INPUT_${toEnvIdent(k)}`] = v;
|
||||
}
|
||||
|
||||
|
|
@ -106,6 +155,7 @@ export function buildWorkflowEnv(ctx: TemplateContext): Record<string, string> {
|
|||
|
||||
if (ctx.eventMeta) {
|
||||
for (const [k, v] of Object.entries(ctx.eventMeta)) {
|
||||
if (FORBIDDEN_KEYS.has(k)) continue;
|
||||
env[`GCOORD_EVENT_${toEnvIdent(k)}`] = v;
|
||||
}
|
||||
}
|
||||
|
|
@ -113,45 +163,188 @@ export function buildWorkflowEnv(ctx: TemplateContext): Record<string, string> {
|
|||
return env;
|
||||
}
|
||||
|
||||
function resolveTemplateToken(rawExpr: string, ctx: TemplateContext): string {
|
||||
/**
|
||||
* Resolve ONLY recognized GCoordinator template namespaces so Go/Docker format
|
||||
* templates like `{{.Names}}` or `{{json .}}` are left completely untouched.
|
||||
*/
|
||||
export function resolveTemplateToken(
|
||||
rawExpr: string,
|
||||
ctx: TemplateContext,
|
||||
): { matched: boolean; value: string } {
|
||||
const expr = rawExpr.trim();
|
||||
if (expr.startsWith('inputs.')) {
|
||||
const key = expr.slice('inputs.'.length);
|
||||
return ctx.inputs[key] ?? '';
|
||||
if (!expr || expr.startsWith('.')) return { matched: false, value: '' };
|
||||
const parts = expr.split('.');
|
||||
if (parts.some((p) => FORBIDDEN_KEYS.has(p))) {
|
||||
return { matched: false, value: '' };
|
||||
}
|
||||
if (expr.startsWith('steps.')) {
|
||||
const rest = expr.slice('steps.'.length);
|
||||
|
||||
if (expr.startsWith('inputs.') || expr.startsWith('input.')) {
|
||||
const key = expr.slice(expr.indexOf('.') + 1);
|
||||
if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(key)) return { matched: false, value: '' };
|
||||
return {
|
||||
matched: true,
|
||||
value: Object.hasOwn(ctx.inputs, key) ? ctx.inputs[key]! : '',
|
||||
};
|
||||
}
|
||||
|
||||
if (expr.startsWith('steps.') || expr.startsWith('step.')) {
|
||||
const rest = expr.slice(expr.indexOf('.') + 1);
|
||||
const dot = rest.lastIndexOf('.');
|
||||
if (dot > 0) {
|
||||
const stepId = rest.slice(0, dot);
|
||||
const field = rest.slice(dot + 1);
|
||||
const out = ctx.stepOutputs?.get(stepId);
|
||||
if (!out) return '';
|
||||
if (field === 'stdout') return out.stdout.trim();
|
||||
if (field === 'exitCode') return out.exitCode === null ? '' : String(out.exitCode);
|
||||
if (field === 'stdout') return { matched: true, value: out ? out.stdout.trim() : '' };
|
||||
if (field === 'exitCode') {
|
||||
return {
|
||||
matched: true,
|
||||
value: out && out.exitCode !== null ? String(out.exitCode) : '',
|
||||
};
|
||||
}
|
||||
}
|
||||
return { matched: false, value: '' };
|
||||
}
|
||||
if (expr.startsWith('captures.')) {
|
||||
const key = expr.slice('captures.'.length);
|
||||
return (ctx.captures?.get(key) ?? '').trim();
|
||||
|
||||
if (expr.startsWith('captures.') || expr.startsWith('capture.')) {
|
||||
const key = expr.slice(expr.indexOf('.') + 1);
|
||||
return { matched: true, value: (ctx.captures?.get(key) ?? '').trim() };
|
||||
}
|
||||
|
||||
if (expr.startsWith('event.')) {
|
||||
const key = expr.slice('event.'.length);
|
||||
return ctx.eventMeta?.[key] ?? '';
|
||||
return {
|
||||
matched: true,
|
||||
value: ctx.eventMeta && Object.hasOwn(ctx.eventMeta, key) ? ctx.eventMeta[key]! : '',
|
||||
};
|
||||
}
|
||||
return (ctx.captures?.get(expr) ?? ctx.inputs[expr] ?? '').trim();
|
||||
|
||||
if (expr.startsWith('server.')) {
|
||||
const field = expr.slice('server.'.length);
|
||||
if (field === 'id') return { matched: true, value: ctx.serverMeta?.id ?? ctx.serverId ?? '' };
|
||||
if (field === 'name') return { matched: true, value: ctx.serverMeta?.name ?? '' };
|
||||
if (field === 'host') return { matched: true, value: ctx.serverMeta?.host ?? '' };
|
||||
if (field === 'port') {
|
||||
return {
|
||||
matched: true,
|
||||
value: ctx.serverMeta ? String(ctx.serverMeta.port) : '',
|
||||
};
|
||||
}
|
||||
return { matched: false, value: '' };
|
||||
}
|
||||
|
||||
if (expr === 'run.id') return { matched: true, value: ctx.runId ?? '' };
|
||||
if (expr === 'run.workflowId') return { matched: true, value: ctx.workflowId ?? '' };
|
||||
if (expr === 'trigger.source') return { matched: true, value: ctx.triggerSource ?? '' };
|
||||
if (expr.startsWith('env.')) {
|
||||
const key = expr.slice('env.'.length);
|
||||
const env = buildWorkflowEnv(ctx);
|
||||
return { matched: true, value: Object.hasOwn(env, key) ? env[key]! : '' };
|
||||
}
|
||||
|
||||
if (/^[A-Za-z0-9][A-Za-z0-9_-]*$/.test(expr)) {
|
||||
if (ctx.captures?.has(expr)) {
|
||||
return { matched: true, value: (ctx.captures.get(expr) ?? '').trim() };
|
||||
}
|
||||
if (Object.hasOwn(ctx.inputs, expr)) {
|
||||
return { matched: true, value: ctx.inputs[expr]! };
|
||||
}
|
||||
}
|
||||
|
||||
return { matched: false, value: '' };
|
||||
}
|
||||
|
||||
export function interpolateValuePlain(template: string, ctx: TemplateContext): string {
|
||||
return template.replace(/\{\{([^{}]+)\}\}/g, (_match, expr: string) =>
|
||||
resolveTemplateToken(expr, ctx),
|
||||
);
|
||||
return template.replace(/\{\{([^{}]+)\}\}/g, (full, expr: string) => {
|
||||
const res = resolveTemplateToken(expr, ctx);
|
||||
return res.matched ? res.value : full;
|
||||
});
|
||||
}
|
||||
|
||||
function escapeDoubleQuotedShell(val: string): string {
|
||||
return val.replace(/([\\"$`])/g, '\\$1');
|
||||
}
|
||||
|
||||
function escapeInsideSingleQuotedShell(val: string): string {
|
||||
return val.replace(/'/g, "'\\''");
|
||||
}
|
||||
|
||||
/**
|
||||
* Quote-aware shell template substitution:
|
||||
* - Unquoted `{{token}}`: wrapped in `posixSingleQuote(...)`
|
||||
* - Inside `'...'`: safely single-quote escaped without outer quotes
|
||||
* - Inside `"..."`: escaped for `$`, `"`, `\`, `` ` ``
|
||||
* - Unrecognized `{{.Names}}` Go templates remain byte-for-byte untouched
|
||||
*/
|
||||
export function interpolateShellCommandSafe(template: string, ctx: TemplateContext): string {
|
||||
return template.replace(/\{\{([^{}]+)\}\}/g, (_match, expr: string) =>
|
||||
posixSingleQuote(resolveTemplateToken(expr, ctx)),
|
||||
);
|
||||
let out = '';
|
||||
let quote: 'none' | 'single' | 'double' = 'none';
|
||||
let i = 0;
|
||||
|
||||
while (i < template.length) {
|
||||
const ch = template[i]!;
|
||||
if (quote === 'none') {
|
||||
if (ch === '\\' && i + 1 < template.length) {
|
||||
out += template.slice(i, i + 2);
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (ch === "'") {
|
||||
quote = 'single';
|
||||
out += ch;
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if (ch === '"') {
|
||||
quote = 'double';
|
||||
out += ch;
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
} else if (quote === 'single') {
|
||||
if (ch === "'") {
|
||||
quote = 'none';
|
||||
out += ch;
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
} else if (quote === 'double') {
|
||||
if (ch === '\\' && i + 1 < template.length) {
|
||||
out += template.slice(i, i + 2);
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (ch === '"') {
|
||||
quote = 'none';
|
||||
out += ch;
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (ch === '{' && template[i + 1] === '{') {
|
||||
const closeIdx = template.indexOf('}}', i + 2);
|
||||
if (closeIdx > i + 1) {
|
||||
const expr = template.slice(i + 2, closeIdx);
|
||||
const resolved = resolveTemplateToken(expr, ctx);
|
||||
if (!resolved.matched) {
|
||||
out += template.slice(i, closeIdx + 2);
|
||||
} else if (quote === 'single') {
|
||||
out += escapeInsideSingleQuotedShell(resolved.value);
|
||||
} else if (quote === 'double') {
|
||||
out += escapeDoubleQuotedShell(resolved.value);
|
||||
} else {
|
||||
out += posixSingleQuote(resolved.value);
|
||||
}
|
||||
i = closeIdx + 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
out += ch;
|
||||
i += 1;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
export function resolveTargetServers(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue