kb: food for thought on cross-checking rust/gen/content_types and YARA rule coverage
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(214labels instandard_v3_3, includingrandombytesandrandomtxtwhich are remapped viaoverwrite_map)- Active YARA rule labels (
137labels acrossfull/andpartial/,81of which are not intarget_labels_space) - Built-in outputs (
directory,empty,symlink,txt,undefined,unknown, and potentiallyunsupportedfrom #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_spaceHowever, 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 --checkorrust/gen): Keeprust/gen/content_typesexplicit so the set of exposedmagika::ContentTypevariants never changes silently, but assert insync_kb.py --check(orrust/gen) thatrust/gen/content_typesequals{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/genrewritesrust/gen/content_typesat generation time asexisting_lines ∪ target_labels_space, so an external check should validate rather than fightrust/gen.
- Note:
- Option B (Derive directly in
rust/gen): Haverust/genuserule_coverageandin_ml_modelfromassets/content_types_kb.min.json(plustarget_labels_space) to compute the exposed set directly and droprust/gen/content_types.- Trade-off: Eliminates a hand-maintained file, but means promoting/demoting a rule between
notworking/andfull//partial/automatically adds/removes amagika::ContentTypeenum variant (thoughContentTypeis#[non_exhaustive], removing a variant is still a semver-Relevant change).
- Trade-off: Eliminates a hand-maintained file, but means promoting/demoting a rule between
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
privaterules and rejectsglobalrules / duplicate rule IDs. - Reads
meta:(label,class,enforced/enabled) and enforcesclass == bucketand active status (enforced = trueorenabled = true). - Computes
rule_coveragewith precedencefull > partial > builtin > none.
Idea
Add a small unit test in rust/rules that:
- Loads
../../assets/content_types_kb.min.json. - Parses
rulesets/full/*.yarandrulesets/partial/*.yarusing the authoritativeyara-x-parserAST inSource::parse_in_bucket. - Asserts that for every content type in the KB, the
rule_coveragefield ("full"vs."partial"vs."builtin"/"none") matches whatSource::parse_in_bucketderives.
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.
Source: google/magika