forjar 1.4.1

Rust-native Infrastructure as Code — bare-metal first, BLAKE3 state, provenance tracing
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
use super::*;

#[test]
fn test_fj132_exec_script_large_output() {
    // Verify transport handles large output without truncation
    let machine = Machine {
        hostname: "local".to_string(),
        addr: "127.0.0.1".to_string(),
        user: "root".to_string(),
        arch: "x86_64".to_string(),
        ssh_key: None,
        roles: vec![],
        transport: None,
        container: None,
        pepita: None,
        cost: 0,
        allowed_operators: vec![],
    };
    let out = exec_script(&machine, "seq 1 10000").unwrap();
    assert!(out.success());
    assert!(out.stdout.contains("10000"));
}

#[test]
fn test_fj132_exec_script_env_isolation() {
    // Scripts should not leak env vars between calls
    let machine = Machine {
        hostname: "local".to_string(),
        addr: "127.0.0.1".to_string(),
        user: "root".to_string(),
        arch: "x86_64".to_string(),
        ssh_key: None,
        roles: vec![],
        transport: None,
        container: None,
        pepita: None,
        cost: 0,
        allowed_operators: vec![],
    };
    exec_script(&machine, "export FORJAR_TEST_LEAK=yes").unwrap();
    let out = exec_script(&machine, "echo ${FORJAR_TEST_LEAK:-unset}").unwrap();
    assert!(out.success());
    assert_eq!(out.stdout.trim(), "unset");
}

#[test]
fn test_fj132_exec_script_exit_code_preserved() {
    // Verify various exit codes are preserved
    let machine = Machine {
        hostname: "local".to_string(),
        addr: "127.0.0.1".to_string(),
        user: "root".to_string(),
        arch: "x86_64".to_string(),
        ssh_key: None,
        roles: vec![],
        transport: None,
        container: None,
        pepita: None,
        cost: 0,
        allowed_operators: vec![],
    };
    for code in [0, 1, 2, 42, 126, 127] {
        let out = exec_script(&machine, &format!("exit {code}")).unwrap();
        assert_eq!(out.exit_code, code, "exit code {code} should be preserved");
    }
}

#[test]
fn test_fj132_timeout_zero_seconds_fails() {
    // A timeout of 0 seconds should cause immediate timeout
    let machine = Machine {
        hostname: "local".to_string(),
        addr: "127.0.0.1".to_string(),
        user: "root".to_string(),
        arch: "x86_64".to_string(),
        ssh_key: None,
        roles: vec![],
        transport: None,
        container: None,
        pepita: None,
        cost: 0,
        allowed_operators: vec![],
    };
    // sleep 5 with 0s timeout should error — but 0-second timeout
    // may or may not catch "echo ok" depending on scheduling
    let result = exec_script_timeout(&machine, "sleep 5", Some(0));
    // This should almost always timeout, but we accept either outcome
    // since 0-second timeout behavior is platform-dependent
    if let Err(e) = result {
        assert!(e.contains("timeout"));
    }
}

#[test]
fn test_fj132_is_local_addr_empty_string() {
    assert!(!is_local_addr(""));
}

// ── FJ-036: Transport script execution coverage ─────────────────

#[test]
fn test_fj036_local_script_echo() {
    let machine = Machine {
        hostname: "local".to_string(),
        addr: "127.0.0.1".to_string(),
        user: "root".to_string(),
        arch: "x86_64".to_string(),
        ssh_key: None,
        roles: vec![],
        transport: None,
        container: None,
        pepita: None,
        cost: 0,
        allowed_operators: vec![],
    };
    let out = exec_script(&machine, "echo 'hello from forjar'").unwrap();
    assert!(out.success());
    assert_eq!(out.exit_code, 0);
    assert_eq!(out.stdout.trim(), "hello from forjar");
    // stderr may contain noise from parallel test processes under coverage instrumentation
    let _ = &out.stderr;
}

#[test]
fn test_fj036_local_script_exit_code() {
    let machine = Machine {
        hostname: "local".to_string(),
        addr: "127.0.0.1".to_string(),
        user: "root".to_string(),
        arch: "x86_64".to_string(),
        ssh_key: None,
        roles: vec![],
        transport: None,
        container: None,
        pepita: None,
        cost: 0,
        allowed_operators: vec![],
    };
    let out = exec_script(&machine, "exit 1").unwrap();
    assert!(!out.success());
    assert_eq!(out.exit_code, 1);
}

#[test]
fn test_fj036_is_local_addr_comprehensive() {
    // All standard local address variants
    assert!(is_local_addr("127.0.0.1"), "IPv4 loopback must be local");
    assert!(is_local_addr("localhost"), "localhost must be local");
    assert!(is_local_addr("::1"), "IPv6 loopback must be local");

    // Non-local addresses must return false
    assert!(!is_local_addr("0.0.0.0"), "0.0.0.0 is not treated as local");
    assert!(
        !is_local_addr("192.168.1.1"),
        "private IP must not be local"
    );
    assert!(!is_local_addr("10.0.0.1"), "10.x must not be local");
    assert!(!is_local_addr("8.8.8.8"), "public IP must not be local");
    assert!(!is_local_addr("google.com"), "domain must not be local");
    assert!(!is_local_addr(""), "empty string must not be local");
    assert!(
        !is_local_addr("127.0.0.2"),
        "127.0.0.2 is not explicitly local"
    );
}

// ── FJ-261: SSH retry with exponential backoff ──

#[test]
fn test_fj261_is_transient_ssh_error_connection_refused() {
    assert!(is_transient_ssh_error(
        "ssh: connect to host 10.0.0.1 port 22: Connection refused"
    ));
}

#[test]
fn test_fj261_is_transient_ssh_error_connection_reset() {
    assert!(is_transient_ssh_error("Connection reset by peer"));
}

#[test]
fn test_fj261_is_transient_ssh_error_timeout() {
    assert!(is_transient_ssh_error(
        "transport timeout: script on 'box' exceeded 30s limit"
    ));
}

#[test]
fn test_fj261_is_transient_ssh_error_broken_pipe() {
    assert!(is_transient_ssh_error("Write failed: Broken pipe"));
}

#[test]
fn test_fj261_is_transient_ssh_error_no_route() {
    assert!(is_transient_ssh_error(
        "ssh: connect to host 10.0.0.1: No route to host"
    ));
}

#[test]
fn test_fj261_is_transient_ssh_error_spawn_failure() {
    assert!(is_transient_ssh_error(
        "failed to spawn ssh to 10.0.0.1: ..."
    ));
}

#[test]
fn test_fj261_is_transient_ssh_error_non_transient() {
    assert!(!is_transient_ssh_error("Permission denied (publickey)"));
    assert!(!is_transient_ssh_error("Host key verification failed"));
    assert!(!is_transient_ssh_error("exit code 1: command not found"));
}

#[test]
fn test_fj261_retry_local_skips_retry() {
    // Local targets should never retry — only 1 attempt
    let machine = Machine {
        hostname: "local".to_string(),
        addr: "127.0.0.1".to_string(),
        user: "root".to_string(),
        arch: "x86_64".to_string(),
        ssh_key: None,
        roles: vec![],
        transport: None,
        container: None,
        pepita: None,
        cost: 0,
        allowed_operators: vec![],
    };
    let out = exec_script_retry(&machine, "echo ok", None, 3).unwrap();
    assert!(out.success());
    assert_eq!(out.stdout.trim(), "ok");
}

#[test]
fn test_fj261_retry_default_one_is_no_retry() {
    // ssh_retries=1 means one attempt, no retry
    let machine = Machine {
        hostname: "local".to_string(),
        addr: "127.0.0.1".to_string(),
        user: "root".to_string(),
        arch: "x86_64".to_string(),
        ssh_key: None,
        roles: vec![],
        transport: None,
        container: None,
        pepita: None,
        cost: 0,
        allowed_operators: vec![],
    };
    let out = exec_script_retry(&machine, "echo once", None, 1).unwrap();
    assert!(out.success());
    assert_eq!(out.stdout.trim(), "once");
}

#[test]
fn test_fj261_retry_clamped_to_max_4() {
    // ssh_retries > 4 should be clamped to 4. For local, still runs once.
    let machine = Machine {
        hostname: "local".to_string(),
        addr: "127.0.0.1".to_string(),
        user: "root".to_string(),
        arch: "x86_64".to_string(),
        ssh_key: None,
        roles: vec![],
        transport: None,
        container: None,
        pepita: None,
        cost: 0,
        allowed_operators: vec![],
    };
    let out = exec_script_retry(&machine, "echo clamped", None, 100).unwrap();
    assert!(out.success());
    assert_eq!(out.stdout.trim(), "clamped");
}

#[test]
fn test_fj261_retry_with_timeout() {
    let machine = Machine {
        hostname: "local".to_string(),
        addr: "127.0.0.1".to_string(),
        user: "root".to_string(),
        arch: "x86_64".to_string(),
        ssh_key: None,
        roles: vec![],
        transport: None,
        container: None,
        pepita: None,
        cost: 0,
        allowed_operators: vec![],
    };
    let out = exec_script_retry(&machine, "echo fast", Some(10), 2).unwrap();
    assert!(out.success());
    assert_eq!(out.stdout.trim(), "fast");
}

#[test]
fn test_fj261_retry_zero_clamped_to_one() {
    // ssh_retries=0 should clamp to 1 (at least one attempt)
    let machine = Machine {
        hostname: "local".to_string(),
        addr: "127.0.0.1".to_string(),
        user: "root".to_string(),
        arch: "x86_64".to_string(),
        ssh_key: None,
        roles: vec![],
        transport: None,
        container: None,
        pepita: None,
        cost: 0,
        allowed_operators: vec![],
    };
    let out = exec_script_retry(&machine, "echo zero", None, 0).unwrap();
    assert!(out.success());
    assert_eq!(out.stdout.trim(), "zero");
}

// ── FJ-29: Base64 payload stripping for I8 lint ──

#[test]
fn test_fj29_strip_data_payloads_simple() {
    let script = "set -euo pipefail\necho 'SGVsbG8gV29ybGQ=' | base64 -d > '/tmp/hello'\nchmod '0755' '/tmp/hello'";
    let stripped = strip_data_payloads(script);
    assert!(
        !stripped.contains("SGVsbG8gV29ybGQ="),
        "base64 payload should be stripped"
    );
    assert!(
        stripped.contains("FORJAR_BASE64_STRIPPED"),
        "placeholder should replace base64"
    );
    assert!(
        stripped.contains("chmod '0755' '/tmp/hello'"),
        "non-payload lines preserved"
    );
}

#[test]
fn test_fj29_strip_data_payloads_large_binary() {
    // Simulate a large binary (like a 22MB forjar ELF) base64-encoded
    // Generate fake base64 that contains sequences bashrs would misparse
    let fake_b64 = "doZmaQBpbg=="; // contains "do", "fi", "in" substrings
    let script = format!(
        "set -euo pipefail\nmkdir -p '/home/user/.cargo/bin'\necho '{fake_b64}' | base64 -d > '/home/user/.cargo/bin/forjar'\nchown 'noah' '/home/user/.cargo/bin/forjar'\nchmod '0755' '/home/user/.cargo/bin/forjar'"
    );
    let stripped = strip_data_payloads(&script);
    assert!(!stripped.contains(fake_b64));
    // The structural commands must survive
    assert!(stripped.contains("set -euo pipefail"));
    assert!(stripped.contains("mkdir -p"));
    assert!(stripped.contains("chown 'noah'"));
    assert!(stripped.contains("chmod '0755'"));
}

#[test]
fn test_fj29_strip_preserves_non_base64_scripts() {
    let script = "set -euo pipefail\necho 'hello world'\nexit 0";
    let stripped = strip_data_payloads(script);
    assert_eq!(
        stripped, script,
        "scripts without base64 should be unchanged"
    );
}

#[test]
fn test_fj29_validate_before_exec_accepts_base64_script() {
    // This script would fail I8 without the fix because the base64
    // contains sequences that look like shell keywords
    let fake_b64 = "doZmaQBpbg==";
    let script = format!(
        "set -euo pipefail\necho '{fake_b64}' | base64 -d > '/tmp/test'\nchmod '0755' '/tmp/test'"
    );
    let result = validate_before_exec(&script);
    assert!(
        result.is_ok(),
        "base64 file deploy should pass I8: {result:?}"
    );
}

#[test]
fn test_fj29_strip_heredoc_payloads() {
    let script = "set -euo pipefail\nmkdir -p '/home/user'\ncat > '/home/user/.bashrc' <<'FORJAR_EOF'\n#!/usr/bin/env bash\nexport PATH=\"$HOME/.cargo/bin:$PATH\"\nFORJAR_EOF\nchown 'noah' '/home/user/.bashrc'";
    let stripped = strip_data_payloads(script);
    assert!(
        !stripped.contains("export PATH"),
        "heredoc payload should be stripped"
    );
    assert!(
        stripped.contains("# payload stripped for lint"),
        "placeholder should replace heredoc"
    );
    assert!(
        stripped.contains("chown 'noah'"),
        "post-heredoc commands preserved"
    );
}

#[test]
fn test_fj29_validate_before_exec_accepts_heredoc_with_shebang() {
    // SC1128 false positive: shebang inside heredoc content, not at script top
    let script = "set -euo pipefail\ncat > '/tmp/install.sh' <<'FORJAR_EOF'\n#!/usr/bin/env bash\nset -euo pipefail\necho hello\nFORJAR_EOF\nchmod '0755' '/tmp/install.sh'";
    let result = validate_before_exec(script);
    assert!(
        result.is_ok(),
        "heredoc with shebang should pass I8: {result:?}"
    );
}