cargo-allow 0.1.3

Source-tree exception ledger and policy scanner for Rust repositories.
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
use crate::artifact_schema_support::{
    assert_enum_equals, assert_required_fields, assert_schema_type_equals, governed_kind_enum,
    match_status_enum, parse_schema, required_schema_pointer,
};
use crate::worklist::{DIFFICULTY_LEVELS, RISK_LEVELS, WORK_ITEM_KINDS};
use serde_json::Value;
use std::collections::BTreeSet;

#[test]
fn worklist_schema_locks_filters_summary_and_work_items_contract() {
    let schema = parse_schema(
        "worklist",
        include_str!("../../../docs/schemas/worklist.schema.json"),
    );

    assert_required_fields(
        "worklist",
        &schema,
        &[
            "schema_version",
            "schema_id",
            "tool",
            "command",
            "claim_boundary",
            "scanner_limitations",
            "inventory",
            "summary",
            "work_items",
        ],
    );
    assert_eq!(
        schema
            .pointer("/properties/filters/$ref")
            .and_then(Value::as_str),
        Some("#/$defs/filters"),
        "worklist filters should use filters schema"
    );
    assert_eq!(
        schema
            .pointer("/properties/summary/$ref")
            .and_then(Value::as_str),
        Some("#/$defs/summary"),
        "worklist summary should use summary schema"
    );
    assert_eq!(
        schema
            .pointer("/properties/work_items/items/$ref")
            .and_then(Value::as_str),
        Some("#/$defs/work_item"),
        "worklist work_items should use work item rows"
    );

    let filters = required_schema_pointer("worklist", &schema, "/$defs/filters");
    assert_eq!(
        filters.get("additionalProperties").and_then(Value::as_bool),
        Some(false),
        "worklist filters should reject unknown fields"
    );
    assert!(
        filters.get("required").is_none(),
        "worklist filter fields should stay optional for worklist.v1 compatibility"
    );
    assert_worklist_filter_properties(filters);
    for field in [
        "kind",
        "family",
        "item_kind",
        "allow_id",
        "path",
        "source_package",
        "owner",
        "classification",
    ] {
        assert_schema_type_equals(
            "worklist filter string option",
            &schema,
            &format!("/$defs/filters/properties/{field}/type"),
            &["string", "null"],
        );
    }
    assert_nullable_string_enum_equals(
        "worklist",
        &schema,
        "/$defs/filters/properties/status/enum",
        &match_status_enum(),
    );
    assert_nullable_string_enum_equals(
        "worklist",
        &schema,
        "/$defs/filters/properties/risk/enum",
        RISK_LEVELS,
    );
    assert_nullable_string_enum_equals(
        "worklist",
        &schema,
        "/$defs/filters/properties/difficulty/enum",
        DIFFICULTY_LEVELS,
    );
    for field in ["baseline_debt", "broad_scope", "missing_evidence"] {
        assert_eq!(
            schema
                .pointer(&format!("/$defs/filters/properties/{field}/type"))
                .and_then(Value::as_str),
            Some("boolean"),
            "worklist filter {field} should be boolean"
        );
    }

    let summary = required_schema_pointer("worklist", &schema, "/$defs/summary");
    assert_eq!(
        summary.get("additionalProperties").and_then(Value::as_bool),
        Some(false),
        "worklist summary should reject unknown fields"
    );
    assert_required_fields(
        "worklist summary",
        summary,
        &[
            "work_items",
            "high",
            "medium",
            "low",
            "small_difficulty",
            "medium_difficulty",
        ],
    );
    assert!(
        !summary
            .get("required")
            .and_then(Value::as_array)
            .unwrap_or_else(|| std::panic::panic_any(
                "worklist summary required should be an array"
            ))
            .iter()
            .any(|field| field.as_str() == Some("item_kinds")),
        "worklist summary item_kinds should stay optional for worklist.v1 compatibility"
    );
    for field in [
        "work_items",
        "high",
        "medium",
        "low",
        "small_difficulty",
        "medium_difficulty",
    ] {
        assert_eq!(
            schema
                .pointer(&format!("/$defs/summary/properties/{field}/type"))
                .and_then(Value::as_str),
            Some("integer"),
            "worklist summary {field} should be an integer"
        );
    }
    assert_eq!(
        schema
            .pointer("/$defs/summary/properties/item_kinds/$ref")
            .and_then(Value::as_str),
        Some("#/$defs/item_kind_counts"),
        "worklist summary item_kinds should use item-kind count schema"
    );
    let item_kind_counts = required_schema_pointer("worklist", &schema, "/$defs/item_kind_counts");
    assert_eq!(
        item_kind_counts
            .get("additionalProperties")
            .and_then(Value::as_bool),
        Some(false),
        "worklist item_kind_counts should reject unknown queue kinds"
    );
    assert!(
        item_kind_counts.get("required").is_none(),
        "worklist item_kind_counts fields should stay optional"
    );
    assert_worklist_item_kind_count_properties(item_kind_counts);
    for kind in WORK_ITEM_KINDS {
        assert_eq!(
            schema
                .pointer(&format!("/$defs/item_kind_counts/properties/{kind}/type"))
                .and_then(Value::as_str),
            Some("integer"),
            "worklist item_kind_counts {kind} should be an integer"
        );
        assert_eq!(
            schema
                .pointer(&format!(
                    "/$defs/item_kind_counts/properties/{kind}/minimum"
                ))
                .and_then(Value::as_u64),
            Some(0),
            "worklist item_kind_counts {kind} minimum"
        );
    }

    let work_item = required_schema_pointer("worklist", &schema, "/$defs/work_item");
    assert_eq!(
        work_item
            .get("additionalProperties")
            .and_then(Value::as_bool),
        Some(false),
        "worklist items should reject unknown fields"
    );
    assert_required_fields(
        "worklist item",
        work_item,
        &[
            "id",
            "kind",
            "risk",
            "difficulty",
            "status",
            "allow_id",
            "finding_index",
            "path",
            "source_package",
            "message",
            "suggested_actions",
            "proof_commands",
        ],
    );
    let work_item_required = work_item
        .get("required")
        .and_then(Value::as_array)
        .unwrap_or_else(|| std::panic::panic_any("worklist item required should be an array"))
        .iter()
        .map(|field| {
            field.as_str().unwrap_or_else(|| {
                std::panic::panic_any("worklist item required entries should be strings")
            })
        })
        .collect::<BTreeSet<_>>();
    assert!(
        !work_item_required.contains("evidence_reference"),
        "worklist evidence_reference should stay optional for worklist.v1 compatibility"
    );
    assert!(
        !work_item_required.contains("selector_precision"),
        "worklist selector_precision should stay optional for worklist.v1 compatibility"
    );
    assert_schema_type_equals(
        "worklist item selector_precision",
        &schema,
        "/$defs/work_item/properties/selector_precision/type",
        &["integer", "null"],
    );
    assert_eq!(
        schema
            .pointer("/$defs/work_item/properties/selector_precision/minimum")
            .and_then(Value::as_u64),
        Some(0),
        "worklist selector_precision minimum"
    );
    assert_enum_equals(
        "worklist item risk",
        &schema,
        "/$defs/work_item/properties/risk/enum",
        RISK_LEVELS,
    );
    assert_enum_equals(
        "worklist item difficulty",
        &schema,
        "/$defs/work_item/properties/difficulty/enum",
        DIFFICULTY_LEVELS,
    );
    assert_enum_equals(
        "worklist item status",
        &schema,
        "/$defs/work_item/properties/status/enum",
        &match_status_enum(),
    );
    assert_enum_equals(
        "worklist item kind",
        &schema,
        "/$defs/work_item/properties/kind/enum",
        WORK_ITEM_KINDS,
    );
    assert_nullable_string_enum_equals(
        "worklist",
        &schema,
        "/$defs/work_item/properties/exception_kind/enum",
        &governed_kind_enum(),
    );
    assert_schema_type_equals(
        "worklist item source_package",
        &schema,
        "/$defs/work_item/properties/source_package/type",
        &["string", "null"],
    );
    assert_schema_type_equals(
        "worklist item path",
        &schema,
        "/$defs/work_item/properties/path/type",
        &["string", "null"],
    );
    let path_description = schema
        .pointer("/$defs/work_item/properties/path/description")
        .and_then(Value::as_str)
        .unwrap_or_else(|| {
            std::panic::panic_any("worklist item path should document source-tree semantics")
        });
    assert!(
        path_description.contains("Source-tree path"),
        "worklist item path should be documented as source-tree scoped"
    );
    assert!(
        path_description.contains("weak_evidence_reference"),
        "worklist item path should document why weak evidence references use null"
    );
    assert_eq!(
        schema
            .pointer("/$defs/work_item/properties/evidence_reference/$ref")
            .and_then(Value::as_str),
        Some("#/$defs/evidence_reference"),
        "worklist evidence references should use evidence reference rows"
    );
    let evidence_reference =
        required_schema_pointer("worklist", &schema, "/$defs/evidence_reference");
    assert_eq!(
        evidence_reference
            .get("additionalProperties")
            .and_then(Value::as_bool),
        Some(false),
        "worklist evidence references should reject unknown fields"
    );
    assert_required_fields(
        "worklist evidence reference",
        evidence_reference,
        &["raw", "prefix", "target", "status", "message"],
    );
    let evidence_statuses = allow_policy::EvidenceReferenceStatus::ALL
        .iter()
        .map(|status| status.as_str())
        .collect::<Vec<_>>();
    let evidence_categories = allow_policy::EvidenceReferenceCategory::ALL
        .iter()
        .map(|category| category.as_str())
        .collect::<Vec<_>>();
    assert_eq!(
        schema
            .pointer("/$defs/evidence_reference/properties/status/$ref")
            .and_then(Value::as_str),
        Some("#/$defs/evidence_reference_status"),
        "worklist evidence reference status should use the shared status definition"
    );
    assert_enum_equals(
        "worklist evidence reference status",
        &schema,
        "/$defs/evidence_reference_status/enum",
        &evidence_statuses,
    );
    assert_eq!(
        schema
            .pointer("/$defs/evidence_reference/properties/category/$ref")
            .and_then(Value::as_str),
        Some("#/$defs/evidence_reference_category"),
        "worklist evidence reference category should use the shared category definition"
    );
    assert_enum_equals(
        "worklist evidence reference category",
        &schema,
        "/$defs/evidence_reference_category/enum",
        &evidence_categories,
    );
    assert_eq!(
        schema
            .pointer("/$defs/work_item/properties/proof_commands/items/pattern")
            .and_then(Value::as_str),
        Some("^cargo-allow "),
        "worklist proof commands should stay cargo-allow first"
    );
    let proof_command_description = schema
        .pointer("/$defs/work_item/properties/proof_commands/items/description")
        .and_then(Value::as_str)
        .unwrap_or_else(|| {
            std::panic::panic_any("worklist proof commands should document their boundary")
        });
    assert!(
        proof_command_description.contains("standalone cargo-allow commands"),
        "worklist proof commands should document that they are standalone cargo-allow commands"
    );
    assert!(
        proof_command_description.contains("does not execute"),
        "worklist proof commands should document that cargo-allow does not execute them"
    );
}

fn assert_nullable_string_enum_equals(
    name: &str,
    schema: &Value,
    pointer: &str,
    expected_strings: &[&str],
) {
    let Some(items) = schema.pointer(pointer).and_then(Value::as_array) else {
        std::panic::panic_any(format!("{name} {pointer} should be an enum array"));
    };
    let actual = items
        .iter()
        .map(|item| match item {
            Value::String(value) => value.clone(),
            Value::Null => "<null>".to_string(),
            _ => std::panic::panic_any(format!(
                "{name} {pointer} entries should be strings or null"
            )),
        })
        .collect::<BTreeSet<_>>();
    let expected = expected_strings
        .iter()
        .map(|item| (*item).to_string())
        .chain(std::iter::once("<null>".to_string()))
        .collect::<BTreeSet<_>>();
    assert_eq!(actual, expected, "{name} {pointer} enum values");
}

fn assert_worklist_filter_properties(filters: &Value) {
    let Some(properties) = filters.get("properties").and_then(Value::as_object) else {
        std::panic::panic_any("worklist filters properties should be an object");
    };
    let actual = properties
        .keys()
        .map(String::as_str)
        .collect::<BTreeSet<_>>();
    let expected = [
        "kind",
        "family",
        "item_kind",
        "status",
        "allow_id",
        "path",
        "source_package",
        "owner",
        "classification",
        "baseline_debt",
        "broad_scope",
        "risk",
        "difficulty",
        "missing_evidence",
    ]
    .into_iter()
    .collect::<BTreeSet<_>>();

    assert_eq!(actual, expected, "worklist filter schema properties");
}

fn assert_worklist_item_kind_count_properties(item_kind_counts: &Value) {
    let Some(properties) = item_kind_counts
        .get("properties")
        .and_then(Value::as_object)
    else {
        std::panic::panic_any("worklist item_kind_counts properties should be an object");
    };
    let actual = properties
        .keys()
        .map(String::as_str)
        .collect::<BTreeSet<_>>();
    let expected = WORK_ITEM_KINDS.iter().copied().collect::<BTreeSet<_>>();

    assert_eq!(
        actual, expected,
        "worklist item_kind_counts schema properties"
    );
}