sendword 0.8.7

Simple HTTP webhook to command runner sidecar. Frontend for managing hooks, JSON state for config portability, SQLite for execution history and logs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use regex::Regex;
use std::borrow::Cow;
use std::sync::LazyLock;

use crate::payload::resolve_field;

/// Regex matching `{{field_name}}` placeholders in command templates.
/// Allows dotted paths like `{{repo.name}}` and trims internal whitespace
/// so `{{ repo.name }}` also works.
static PLACEHOLDER_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\{\{\s*([\w.]+)\s*\}\}").expect("placeholder regex is valid"));

/// Shell-escape a string value for safe embedding in `sh -c` commands.
///
/// Wraps the value in single quotes. Any internal single quotes are escaped
/// using the POSIX idiom `'\''` (end quote, backslash-escaped literal quote,
/// start quote).
pub fn shell_escape(value: &str) -> String {
    if !value.contains('\'') {
        format!("'{value}'")
    } else {
        let escaped = value.replace('\'', "'\\''");
        format!("'{escaped}'")
    }
}

/// Convert a resolved `serde_json::Value` to its string representation
/// for interpolation.
///
/// - Strings: raw value (no JSON quotes)
/// - Numbers/booleans: their display form
/// - Arrays/objects: compact JSON
/// - Null: empty string
fn value_to_string(value: &serde_json::Value) -> Cow<'_, str> {
    match value {
        serde_json::Value::String(s) => Cow::Borrowed(s.as_str()),
        serde_json::Value::Number(n) => Cow::Owned(n.to_string()),
        serde_json::Value::Bool(b) => Cow::Borrowed(if *b { "true" } else { "false" }),
        serde_json::Value::Null => Cow::Borrowed(""),
        // Arrays and objects: compact JSON representation
        other => Cow::Owned(other.to_string()),
    }
}

/// Interpolate `{{field_name}}` placeholders in a command template.
///
/// For each placeholder found:
/// 1. Resolve the field path against the payload JSON (supports dot-notation).
/// 2. Convert the resolved value to a string.
/// 3. Shell-escape the string.
/// 4. Replace the placeholder with the escaped value.
///
/// Unresolved placeholders are left as-is (not replaced).
///
/// Returns `Cow::Borrowed` if no placeholders were found (zero allocation).
pub fn interpolate_command<'a>(template: &'a str, payload: &serde_json::Value) -> Cow<'a, str> {
    if !template.contains("{{") {
        return Cow::Borrowed(template);
    }

    let result = PLACEHOLDER_RE.replace_all(template, |caps: &regex::Captures| {
        let field_path = &caps[1];
        match resolve_field(payload, field_path) {
            Some(value) => shell_escape(&value_to_string(value)),
            None => caps[0].to_string(),
        }
    });

    match result {
        Cow::Borrowed(_) => Cow::Borrowed(template),
        Cow::Owned(s) => Cow::Owned(s),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    // --- shell_escape tests ---

    #[test]
    fn shell_escape_simple_string() {
        assert_eq!(shell_escape("hello"), "'hello'");
    }

    #[test]
    fn shell_escape_string_with_spaces() {
        assert_eq!(shell_escape("hello world"), "'hello world'");
    }

    #[test]
    fn shell_escape_string_with_special_chars() {
        assert_eq!(
            shell_escape("rm -rf /; echo pwned"),
            "'rm -rf /; echo pwned'"
        );
    }

    #[test]
    fn shell_escape_string_with_single_quotes() {
        assert_eq!(shell_escape("it's"), "'it'\\''s'");
    }

    #[test]
    fn shell_escape_string_with_backticks() {
        assert_eq!(shell_escape("`whoami`"), "'`whoami`'");
    }

    #[test]
    fn shell_escape_string_with_dollar_expansion() {
        assert_eq!(shell_escape("$(cat /etc/passwd)"), "'$(cat /etc/passwd)'");
    }

    #[test]
    fn shell_escape_empty_string() {
        assert_eq!(shell_escape(""), "''");
    }

    #[test]
    fn shell_escape_string_with_newline() {
        assert_eq!(shell_escape("line1\nline2"), "'line1\nline2'");
    }

    // --- value_to_string tests ---

    #[test]
    fn value_to_string_string() {
        let v = json!("hello");
        assert_eq!(value_to_string(&v).as_ref(), "hello");
    }

    #[test]
    fn value_to_string_number_integer() {
        let v = json!(42);
        assert_eq!(value_to_string(&v).as_ref(), "42");
    }

    #[test]
    fn value_to_string_number_float() {
        let v = json!(3.14);
        assert_eq!(value_to_string(&v).as_ref(), "3.14");
    }

    #[test]
    fn value_to_string_boolean_true() {
        let v = json!(true);
        assert_eq!(value_to_string(&v).as_ref(), "true");
    }

    #[test]
    fn value_to_string_boolean_false() {
        let v = json!(false);
        assert_eq!(value_to_string(&v).as_ref(), "false");
    }

    #[test]
    fn value_to_string_null() {
        let v = json!(null);
        assert_eq!(value_to_string(&v).as_ref(), "");
    }

    #[test]
    fn value_to_string_array() {
        let v = json!([1, 2, 3]);
        assert_eq!(value_to_string(&v).as_ref(), "[1,2,3]");
    }

    #[test]
    fn value_to_string_object() {
        let v = json!({"key": "val"});
        assert_eq!(value_to_string(&v).as_ref(), r#"{"key":"val"}"#);
    }

    // --- interpolate_command tests ---

    #[test]
    fn no_placeholders_returns_borrowed() {
        let cmd = "echo hello";
        let payload = json!({});
        let result = interpolate_command(cmd, &payload);
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result.as_ref(), "echo hello");
    }

    #[test]
    fn simple_string_field_interpolated() {
        let cmd = "deploy {{action}}";
        let payload = json!({"action": "rollout"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "deploy 'rollout'");
    }

    #[test]
    fn dot_notation_nested_field() {
        let cmd = "echo {{repo.name}}";
        let payload = json!({"repo": {"name": "myapp"}});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo 'myapp'");
    }

    #[test]
    fn number_field_interpolated() {
        let cmd = "scale --replicas={{count}}";
        let payload = json!({"count": 3});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "scale --replicas='3'");
    }

    #[test]
    fn boolean_field_interpolated() {
        let cmd = "deploy --dry-run={{dry_run}}";
        let payload = json!({"dry_run": true});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "deploy --dry-run='true'");
    }

    #[test]
    fn unresolved_placeholder_left_as_is() {
        let cmd = "echo {{missing_field}}";
        let payload = json!({});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo {{missing_field}}");
    }

    #[test]
    fn multiple_placeholders() {
        let cmd = "deploy {{app}} to {{env}}";
        let payload = json!({"app": "frontend", "env": "production"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "deploy 'frontend' to 'production'");
    }

    #[test]
    fn injection_via_semicolon_is_escaped() {
        let cmd = "echo {{name}}";
        let payload = json!({"name": "foo; rm -rf /"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo 'foo; rm -rf /'");
    }

    #[test]
    fn injection_via_backtick_is_escaped() {
        let cmd = "echo {{name}}";
        let payload = json!({"name": "`cat /etc/passwd`"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo '`cat /etc/passwd`'");
    }

    #[test]
    fn injection_via_dollar_expansion_is_escaped() {
        let cmd = "echo {{name}}";
        let payload = json!({"name": "$(whoami)"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo '$(whoami)'");
    }

    #[test]
    fn injection_via_single_quote_breakout_is_escaped() {
        let cmd = "echo {{name}}";
        let payload = json!({"name": "'; rm -rf / '"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo ''\\''; rm -rf / '\\'''");
    }

    #[test]
    fn whitespace_around_field_name_is_trimmed() {
        let cmd = "echo {{ action }}";
        let payload = json!({"action": "deploy"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo 'deploy'");
    }

    #[test]
    fn array_field_produces_json() {
        let cmd = "process {{items}}";
        let payload = json!({"items": [1, 2, 3]});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "process '[1,2,3]'");
    }

    #[test]
    fn object_field_produces_json() {
        let cmd = "process {{config}}";
        let payload = json!({"config": {"key": "val"}});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), r#"process '{"key":"val"}'"#);
    }

    #[test]
    fn mixed_resolved_and_unresolved() {
        let cmd = "echo {{found}} and {{missing}}";
        let payload = json!({"found": "yes"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo 'yes' and {{missing}}");
    }

    #[test]
    fn deeply_nested_dot_notation() {
        let cmd = "echo {{a.b.c}}";
        let payload = json!({"a": {"b": {"c": "deep"}}});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo 'deep'");
    }

    #[test]
    fn null_field_interpolates_to_empty_quoted_string() {
        let cmd = "echo {{value}}";
        let payload = json!({"value": null});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo ''");
    }

    // --- Additional coverage ---

    #[test]
    fn single_braces_are_not_placeholders() {
        let cmd = "echo {not_a_placeholder}";
        let payload = json!({"not_a_placeholder": "value"});
        let result = interpolate_command(cmd, &payload);
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result.as_ref(), "echo {not_a_placeholder}");
    }

    #[test]
    fn negative_number_field_interpolated() {
        let cmd = "offset={{delta}}";
        let payload = json!({"delta": -5});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "offset='-5'");
    }

    #[test]
    fn injection_via_double_quotes_is_escaped() {
        let cmd = "echo {{name}}";
        let payload = json!({"name": "a\"b"});
        let result = interpolate_command(cmd, &payload);
        // Double quotes inside single-quoted string are harmless
        assert_eq!(result.as_ref(), r#"echo 'a"b'"#);
    }

    #[test]
    fn injection_via_newline_is_escaped() {
        let cmd = "echo {{msg}}";
        let payload = json!({"msg": "line1\nrm -rf /"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo 'line1\nrm -rf /'");
    }

    #[test]
    fn injection_via_pipe_is_escaped() {
        let cmd = "echo {{name}}";
        let payload = json!({"name": "foo | rm -rf /"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo 'foo | rm -rf /'");
    }

    #[test]
    fn partial_dot_path_unresolved_left_as_is() {
        let cmd = "echo {{a.b.missing}}";
        let payload = json!({"a": {"b": {"c": "found"}}});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo {{a.b.missing}}");
    }

    #[test]
    fn dot_path_through_non_object_unresolved() {
        let cmd = "echo {{a.b}}";
        let payload = json!({"a": 42});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo {{a.b}}");
    }

    #[test]
    fn template_is_entirely_a_placeholder() {
        let cmd = "{{cmd}}";
        let payload = json!({"cmd": "ls -la"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "'ls -la'");
    }

    #[test]
    fn adjacent_placeholders_both_interpolated() {
        let cmd = "{{a}}{{b}}";
        let payload = json!({"a": "hello", "b": "world"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "'hello''world'");
    }

    #[test]
    fn underscore_and_digit_field_names() {
        let cmd = "echo {{_var1}} {{item_2}}";
        let payload = json!({"_var1": "alpha", "item_2": "beta"});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "echo 'alpha' 'beta'");
    }

    #[test]
    fn empty_payload_object_leaves_placeholders() {
        let cmd = "deploy {{app}} to {{env}}";
        let payload = json!({});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "deploy {{app}} to {{env}}");
    }

    #[test]
    fn float_zero_and_integer_zero() {
        let cmd = "{{a}} {{b}}";
        let payload = json!({"a": 0, "b": 0.0});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), "'0' '0.0'");
    }

    #[test]
    fn nested_object_field_produces_json() {
        let cmd = "echo {{config.sub}}";
        let payload = json!({"config": {"sub": {"key": "val"}}});
        let result = interpolate_command(cmd, &payload);
        assert_eq!(result.as_ref(), r#"echo '{"key":"val"}'"#);
    }

    #[test]
    fn shell_escape_string_with_backslash() {
        assert_eq!(shell_escape(r"a\b"), r"'a\b'");
    }

    #[test]
    fn shell_escape_multiple_single_quotes() {
        assert_eq!(shell_escape("a'b'c"), "'a'\\''b'\\''c'");
    }
}