forjar 1.29.0

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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Unit tests for the gate's parts, in isolation from any transport.

use super::locate::resource_line;
use super::*;
use crate::core::parser::parse_config;

/// A Stripe-shaped fixture, ASSEMBLED AT RUNTIME so the literal never appears
/// in the source. The detector under test matches
/// `[sr]k_(live|test)_[A-Za-z0-9]{20,}`, so the fixture must have that exact
/// shape — and GitHub push protection matches the same shape, which blocked a
/// push of this repo outright.
fn fake_stripe_key() -> String {
    format!("sk_{}_{}", "live", "A".repeat(24))
}

fn cfg(yaml: &str) -> crate::core::types::ForjarConfig {
    parse_config(yaml).expect("fixture must parse")
}

const SEALED: &str = "ENC[age,YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo=]";

fn secret_config(value: &str) -> String {
    format!(
        r#"
version: "1.0"
name: t
machines:
  m1:
    hostname: m1
    addr: 1.2.3.4
resources:
  app-config:
    type: file
    machine: m1
    path: /etc/app.conf
    content: "api_key={value}"
"#
    )
}

// ── GateReport::passed at the level boundary ────────────────────────

#[test]
fn only_errors_block() {
    let mut report = GateReport {
        findings: vec![GateFinding::new("X", GateLevel::Warning, "r", "m")],
        scripts_analysed: 0,
        resources_checked: 1,
    };
    assert!(report.passed(), "a Warning must not block");
    report
        .findings
        .push(GateFinding::new("Y", GateLevel::Note, "r", "m"));
    assert!(report.passed(), "a Note must not block");
    report
        .findings
        .push(GateFinding::new("Z", GateLevel::Error, "r", "m"));
    assert!(!report.passed(), "an Error must block");
    assert_eq!(report.error_count(), 1);
    assert_eq!(report.advisory_count(), 2);
}

// ── locate.rs ───────────────────────────────────────────────────────

#[test]
fn locate_finds_a_resource_key() {
    let yaml = "version: \"1.0\"\nname: t\nresources:\n  first:\n    type: file\n  second:\n    type: file\n";
    assert_eq!(resource_line(yaml, "first"), Some(4));
    assert_eq!(resource_line(yaml, "second"), Some(6));
}

#[test]
fn locate_answers_none_for_an_absent_resource() {
    let yaml = "resources:\n  first:\n    type: file\n";
    assert_eq!(
        resource_line(yaml, "elsewhere"),
        None,
        "a resource that arrived via `includes:` is not in this file, and \
         inventing a line for it points a reviewer at the wrong place"
    );
}

#[test]
fn locate_ignores_a_resources_key_inside_content() {
    // `resources:` at column 0 is the only one that counts; the nested one is
    // file DATA being written to the target host.
    let yaml = "resources:\n  writer:\n    type: file\n    content: |\n      resources:\n        fake:\n          type: file\n";
    assert_eq!(resource_line(yaml, "writer"), Some(2));
    assert_eq!(
        resource_line(yaml, "fake"),
        None,
        "a key inside a block scalar is not a resource declaration"
    );
}

#[test]
fn locate_stops_at_the_end_of_the_resources_block() {
    let yaml = "resources:\n  a:\n    type: file\npolicies:\n  - type: require\n    field: owner\n";
    assert_eq!(resource_line(yaml, "owner"), None);
}

#[test]
fn locate_answers_none_when_there_is_no_resources_key() {
    assert_eq!(resource_line("version: \"1.0\"\n", "a"), None);
}

// ── check 2: the discrimination that makes the verdict honest ───────

#[test]
fn a_plaintext_secret_in_content_is_an_error() {
    let text = secret_config(&fake_stripe_key());
    let report = evaluate(&cfg(&text), Some(&text), &GateThresholds::default());
    let sec: Vec<_> = report
        .findings
        .iter()
        .filter(|f| f.rule_id == "FJQ-SEC-002")
        .collect();
    assert_eq!(sec.len(), 1, "findings: {:?}", report.findings);
    assert_eq!(sec[0].level, GateLevel::Error);
    assert_eq!(sec[0].resource_id, "app-config");
    assert!(!report.passed());
}

/// A task whose command matches `sshpass_inline` WHATEVER the password is.
///
/// The pattern fires on `sshpass -p <anything>`, so a sealed value here is
/// suppressed by the ENC discrimination and by nothing else. A fixture the
/// pattern cannot match either way would pass this test vacuously.
fn sshpass_config(value: &str) -> String {
    format!(
        r#"
version: "1.0"
name: t
machines:
  m1:
    hostname: m1
    addr: 1.2.3.4
resources:
  deploy:
    type: task
    machine: m1
    phony: true
    command: "sshpass -p {value} ssh deploy@host"
"#
    )
}

#[test]
fn the_same_field_fails_plaintext_and_passes_sealed() {
    let plain = sshpass_config("hunter2");
    let sealed = sshpass_config(SEALED);

    let bad = evaluate(&cfg(&plain), Some(&plain), &GateThresholds::default());
    assert!(
        bad.findings.iter().any(|f| f.rule_id == "FJQ-SEC-002"),
        "a plaintext sshpass password was not reported: {:?}",
        bad.findings
    );
    assert!(bad.findings.iter().any(|f| f.rule_id == "FJQ-SEC-001"));

    let good = evaluate(&cfg(&sealed), Some(&sealed), &GateThresholds::default());
    assert!(
        good.passed(),
        "an ENC[age,…] value is ciphertext, not a plaintext secret — and the \
         `sshpass -p <anything>` pattern matches both, so only the ENC \
         discrimination can tell them apart: {:?}",
        good.findings
    );
}

#[test]
fn a_sealed_secret_in_content_is_not_a_finding() {
    let text = secret_config(SEALED);
    let report = evaluate(&cfg(&text), Some(&text), &GateThresholds::default());
    assert!(
        !report.findings.iter().any(|f| f.rule_id == "FJQ-SEC-002"),
        "an ENC[age,…] value is ciphertext, not a plaintext secret: {:?}",
        report.findings
    );
}

#[test]
fn prose_about_enc_markers_does_not_seal_a_real_secret() {
    // `has_encrypted_markers` requires >= 20 chars of decodable base64, so a
    // comment mentioning the marker cannot be used to smuggle a secret past
    // the check on the same line.
    let text = secret_config(&format!("{} # use ENC[age,...] here", fake_stripe_key()));
    let report = evaluate(&cfg(&text), Some(&text), &GateThresholds::default());
    assert!(
        report.findings.iter().any(|f| f.rule_id == "FJQ-SEC-002"),
        "prose suppressed a real secret: {:?}",
        report.findings
    );
}

#[test]
fn a_finding_carries_the_line_its_resource_is_declared_on() {
    let text = secret_config(&fake_stripe_key());
    let expected = text
        .lines()
        .position(|l| l.trim_start().starts_with("app-config:"))
        .map(|i| i + 1);
    let report = evaluate(&cfg(&text), Some(&text), &GateThresholds::default());
    let f = report
        .findings
        .iter()
        .find(|f| f.rule_id == "FJQ-SEC-002")
        .unwrap();
    assert_eq!(f.yaml_line, expected);
}

#[test]
fn without_yaml_text_a_finding_carries_no_line() {
    let text = secret_config(&fake_stripe_key());
    let report = evaluate(&cfg(&text), None, &GateThresholds::default());
    let f = report
        .findings
        .iter()
        .find(|f| f.rule_id == "FJQ-SEC-002")
        .unwrap();
    assert_eq!(f.yaml_line, None);
}

// ── check 1: the ceiling is read, not decorative ────────────────────

const BRANCHY: &str = r#"
version: "1.0"
name: t
machines:
  m1:
    hostname: m1
    addr: 1.2.3.4
resources:
  branchy:
    type: task
    machine: m1
    phony: true
    command: |
      for f in a b c d e f g h; do
        case "$f" in
          a) echo a ;;
          b) echo b ;;
          c) echo c ;;
          d) echo d ;;
          e) echo e ;;
          f) echo f ;;
          g) echo g ;;
          h) echo h ;;
          i) echo i ;;
          j) echo j ;;
          k) echo k ;;
          l) echo l ;;
          m) echo m ;;
          n) echo n ;;
          o) echo o ;;
          p) echo p ;;
          q) echo q ;;
          r) echo r ;;
          s) echo s ;;
          t) echo t ;;
          u) echo u ;;
          v) echo v ;;
          w) echo w ;;
          x) echo x ;;
          *) echo other ;;
        esac
      done
"#;

#[test]
fn a_ceiling_of_one_flags_a_branchy_script() {
    let t = GateThresholds {
        max_cyclomatic: Some(1),
        ..Default::default()
    };
    let report = evaluate(&cfg(BRANCHY), None, &t);
    assert!(
        report.findings.iter().any(|f| f.rule_id == "FJQ-CPX-001"),
        "a for/if/case chain did not exceed a ceiling of 1: {:?}",
        report.findings
    );
}

#[test]
fn a_none_ceiling_disables_the_check_entirely() {
    let t = GateThresholds {
        max_cyclomatic: None,
        ..Default::default()
    };
    let report = evaluate(&cfg(BRANCHY), None, &t);
    assert!(
        !report.findings.iter().any(|f| f.rule_id == "FJQ-CPX-001"),
        "None must SKIP the check, not fall back to some default ceiling. The \
         fixture's apply script scores ~28, so any silent default under that \
         would show up here: {:?}",
        report.findings
    );
}

#[test]
fn a_ceiling_above_the_score_is_silent() {
    let t = GateThresholds {
        max_cyclomatic: Some(1000),
        ..Default::default()
    };
    let report = evaluate(&cfg(BRANCHY), None, &t);
    assert!(
        !report.findings.iter().any(|f| f.rule_id == "FJQ-CPX-001"),
        "a ceiling of 1000 fired, so the comparison is not against the number \
         that was passed: {:?}",
        report.findings
    );
}

#[test]
fn complexity_is_advisory_until_opted_in() {
    let advisory = GateThresholds {
        max_cyclomatic: Some(1),
        ..Default::default()
    };
    let report = evaluate(&cfg(BRANCHY), None, &advisory);
    let f = report
        .findings
        .iter()
        .find(|f| f.rule_id == "FJQ-CPX-001")
        .unwrap();
    assert_eq!(
        f.level,
        GateLevel::Warning,
        "the shell scored here is EMITTED by forjar, so blocking an operator's \
         apply for its complexity punishes the wrong party"
    );

    let blocking = GateThresholds {
        max_cyclomatic: Some(1),
        complexity_is_error: true,
        ..Default::default()
    };
    let report = evaluate(&cfg(BRANCHY), None, &blocking);
    let f = report
        .findings
        .iter()
        .find(|f| f.rule_id == "FJQ-CPX-001")
        .unwrap();
    assert_eq!(f.level, GateLevel::Error);
}

// ── check 4: in-config policies reach the gate ──────────────────────

#[test]
fn an_in_config_policy_violation_becomes_a_finding() {
    let yaml = r#"
version: "1.0"
name: t
machines:
  m1:
    hostname: m1
    addr: 1.2.3.4
resources:
  cfg:
    type: file
    machine: m1
    path: /etc/app.conf
policies:
  - type: require
    message: "files must have owner"
    id: "SEC-001"
    field: owner
    remediation: "Add owner field"
"#;
    let report = evaluate(&cfg(yaml), Some(yaml), &GateThresholds::default());
    let f = report
        .findings
        .iter()
        .find(|f| f.rule_id == "SEC-001")
        .expect("policy violation missing from the gate");
    assert_eq!(f.level, GateLevel::Error);
    assert_eq!(f.remediation.as_deref(), Some("Add owner field"));
    assert!(!report.passed());
}

// ── the SARIF projection ────────────────────────────────────────────

#[test]
fn sarif_carries_a_region_only_when_the_line_is_known() {
    let findings = vec![
        GateFinding {
            yaml_line: Some(7),
            ..GateFinding::new("A", GateLevel::Error, "r1", "m1")
        },
        GateFinding::new("B", GateLevel::Warning, "r2", "m2"),
    ];
    let sarif = sarif::findings_to_sarif(&findings, "some/forjar.yaml");
    let results = sarif["runs"][0]["results"].as_array().unwrap();
    assert_eq!(results.len(), 2);
    let loc = &results[0]["locations"][0]["physicalLocation"];
    assert_eq!(loc["artifactLocation"]["uri"], "some/forjar.yaml");
    assert_eq!(loc["region"]["startLine"], 7);
    assert!(
        results[1]["locations"][0]["physicalLocation"]["region"].is_null(),
        "a finding with no known line must carry no region at all"
    );
    assert_eq!(results[0]["level"], "error");
    assert_eq!(results[1]["level"], "warning");
    assert_eq!(results[0]["message"]["text"], "r1: m1");
}

#[test]
fn sarif_rules_are_deduped() {
    let findings = vec![
        GateFinding::new("SAME", GateLevel::Error, "a", "first"),
        GateFinding::new("SAME", GateLevel::Error, "b", "second"),
    ];
    let sarif = sarif::findings_to_sarif(&findings, "forjar.yaml");
    let rules = sarif["runs"][0]["tool"]["driver"]["rules"]
        .as_array()
        .unwrap();
    assert_eq!(rules.len(), 1);
    assert_eq!(rules[0]["shortDescription"]["text"], "first");
    assert_eq!(sarif["runs"][0]["results"].as_array().unwrap().len(), 2);
}

// ── rendering is shared, so two surfaces cannot disagree ────────────

#[test]
fn render_lists_errors_and_tallies_the_rest() {
    let report = GateReport {
        findings: vec![
            GateFinding::new("FJQ-SEC-002", GateLevel::Error, "app", "leak"),
            GateFinding::new("FJQ-SH-SC2086", GateLevel::Warning, "app", "quote it")
                .in_script("apply", 3),
        ],
        scripts_analysed: 3,
        resources_checked: 1,
    };
    let lines = report.render();
    assert_eq!(lines.len(), 2, "{lines:?}");
    assert_eq!(lines[0], "FJQ-SEC-002 app: leak");
    assert!(lines[1].contains("1 error(s), 1 advisory"));
}

#[test]
fn render_is_empty_for_a_clean_report() {
    let report = GateReport {
        findings: vec![],
        scripts_analysed: 0,
        resources_checked: 0,
    };
    assert!(report.render().is_empty());
}

// ── the gate judges the text the EXECUTOR will judge ────────────────

#[test]
fn a_file_payload_is_not_linted_as_shell() {
    // `content:` becomes a heredoc payload (or a base64 blob) in the generated
    // script. bashrs reads it as shell if nobody strips it, so a gate that
    // lints the RAW script refuses an apply the transport would run without
    // comment. This content is deliberately shell-shaped nonsense.
    let yaml = r#"
version: "1.0"
name: t
machines:
  m1:
    hostname: m1
    addr: 1.2.3.4
resources:
  payload:
    type: file
    machine: m1
    path: /etc/payload.conf
    content: |
      for x in a; do
      then ]] fi esac
      done
"#;
    let report = evaluate(&cfg(yaml), Some(yaml), &GateThresholds::default());
    assert!(
        report.passed(),
        "file DATA was linted as shell, so the gate refuses what the executor \
         accepts: {:?}",
        report.findings
    );
}