#1521·magika

kb: food for thought on cross-checking rust/gen/content_types and YARA rule coverage

Author: reyammerCreated Oct 1, 2026Updated Oct 1, 2026

Following #1515 and #1520, assets/content_types.yaml is now the human-editable source of truth for the content types knowledge base, and scripts/sync_kb.py validates it, derives rule_coverage ("full" | "partial" | "builtin" | "none") and in_ml_model (bool) from rust/rules/rulesets/{full,partial}/*.yar and rust/gen/model/config.min.json, and generates assets/content_types_kb.min.json (checked in CI by .github/workflows/kb-check.yml).

Below are two optional follow-up ideas ("food for thought") around keeping the Rust crate codegen and rule parser tightly aligned with the KB metadata.


1. Validating or deriving rust/gen/content_types from KB coverage

Context

Today, rust/gen/src/main.rs (filter_content_types) filters assets/content_types_kb.min.json down to the subset of content types exposed in the Rust magika::ContentType enum (and C FFI header) by reading rust/gen/content_types (currently 300 lines) and taking the union with the model's target_labels_space:

  • target_labels_space (214 labels in standard_v3_3, including randombytes and randomtxt which are remapped via overwrite_map)
  • Active YARA rule labels (137 labels across full/ and partial/, 81 of which are not in target_labels_space)
  • Built-in outputs (directory, empty, symlink, txt, undefined, unknown, and potentially unsupported from #1517/#1518 if exposed in Rust)

In practice, the 300 entries in rust/gen/content_types currently equal:

{ ct | rule_coverage != "none" or in_ml_model } ∪ target_labels_space

However, rust/gen only automatically adds target_labels_space entries to rust/gen/content_types; labels introduced via new YARA rules in rust/rules/rulesets/{full,partial}/ or new built-in outputs must be added to rust/gen/content_types by hand (and rust/rules has a unit test checking that every active rule's label resolves via ContentType::from_label).

Options to consider

  • Option A (Validation in sync_kb.py --check or rust/gen): Keep rust/gen/content_types explicit so the set of exposed magika::ContentType variants never changes silently, but assert in sync_kb.py --check (or rust/gen) that rust/gen/content_types equals {ct | rule_coverage != "none" or in_ml_model} ∪ target_labels_space (printing a clear diff if a newly added active rule label or builtin is missing).
    • Note: rust/gen rewrites rust/gen/content_types at generation time as existing_lines ∪ target_labels_space, so an external check should validate rather than fight rust/gen.
  • Option B (Derive directly in rust/gen): Have rust/gen use rule_coverage and in_ml_model from assets/content_types_kb.min.json (plus target_labels_space) to compute the exposed set directly and drop rust/gen/content_types.
    • Trade-off: Eliminates a hand-maintained file, but means promoting/demoting a rule between notworking/ and full//partial/ automatically adds/removes a magika::ContentType enum variant (though ContentType is #[non_exhaustive], removing a variant is still a semver-Relevant change).

2. Rust-side cross-check of rule_coverage against magika-rules

Context

To keep scripts/sync_kb.py lightweight and self-contained (a PEP 723 Python script with only pyyaml), it scans rust/rules/rulesets/{full,partial}/*.yar using a regex-based parser that mirrors the metadata and bucket invariants of rust/rules/src/source.rs (Source::parse_in_bucket):

  • Skips private rules and rejects global rules / duplicate rule IDs.
  • Reads meta: (label, class, enforced / enabled) and enforces class == bucket and active status (enforced = true or enabled = true).
  • Computes rule_coverage with precedence full > partial > builtin > none.

Idea

Add a small unit test in rust/rules that:

  1. Loads ../../assets/content_types_kb.min.json.
  2. Parses rulesets/full/*.yar and rulesets/partial/*.yar using the authoritative yara-x-parser AST in Source::parse_in_bucket.
  3. Asserts that for every content type in the KB, the rule_coverage field ("full" vs. "partial" vs. "builtin"/"none") matches what Source::parse_in_bucket derives.

This would guard against the Python regex scanner in scripts/sync_kb.py ever drifting from the real YARA AST semantics in rust/rules/src/source.rs.