Skip to content

linter pass that checks nursery rule namespaces against existing directory structure #3112

Description

@williballenthin

we should add a linter pass that checks nursery rule namespaces against the existing directory structure.

Originally posted by @williballenthin in mandiant/capa-rules#1166 (comment)

Activity

  1. williballenthin commented on Jun 19, 2026

    @williballenthin
    CollaboratorAuthor

    not sure if this should fail the check or not, since a misspelling is bad, but a brand-new namespace is ok. not sure how we can tell this apart, aside from edit distance or something.

  2. MannXo commented on Jun 19, 2026

    @MannXo

    I'll work on this one , can I get assigned?

  3. williballenthin commented on Jun 19, 2026

    @williballenthin
    CollaboratorAuthor

    @MannXo please provide an overview of your idea/design so we can agree on the strategy.

    then when approved, feel free to open a PR referencing this issue.

  4. jatinkumar300403 commented on Jun 20, 2026

    @jatinkumar300403

    Hi @williballenthin, maybe I can help in this issue. Please find below, the proposed design and solution for this issue.

    Design Overview

    Problem faced
    The existing ‘NamespaceDoesntMatchRulePath’ lint ([lint.py#L131-L146] explicitly skips nursery rules (‘if is_nursery_rule(rule): return False’). This means that nursery rules with typos or invalid namespaces (like 'host-interation' instead of 'host-interaction' in previous issue) go undetected.

    Proposed Solution
    Add a new lint class NurseryNamespaceDoesntExist in scripts/lint.py that only applies to nursery rules and validates that their namespace value corresponds to an existing directory path within the non-nursery rules directory structure.
    I am thinking to use edit distance (will use Python's ‘difflib.get_close_matches’) to differentiate these two cases:
    • If the namespace is close to an existing one, then WARN as a likely typo, and suggest closest match
    • If the namespace is far from all existing ones, then it’s likely intentional, don't flag it

    This means the lint would only warn when there's a probable typo, not when someone introduces a genuinely new namespace.

    how working goes: -

    1. collecting valid namespaces – when lint starts, walk the rules directory and collect paths as valid namespaces, normalized as ‘/’-separated paths(relative to roots)
    2. check each segment - For each nursery rule with a namespace, split it by / and compare each segment against the corresponding level of existing directory names.
    3. match with threshold - Use ‘difflib.get_close_matches’ function, where a 0.8 cutoff means we only flag strings that are very similar(like typos), if different then new namespaces.

    File to be working on: -

    • ‘scripts/lint.py’: Add ‘NurseryNamespaceTypo’ lint class and update ‘Context’ dataclass.

    Would be happy to hear back from you on this!

  5. abhinav-phi commented on Jul 15, 2026

    @abhinav-phi

    Hi @williballenthin, I'd like to work on this issue.

    My initial idea is to add a dedicated linter pass for nursery rules that validates their namespaces against the existing rule directory structure. Rather than failing immediately, I'd treat likely typos (based on similarity to existing namespaces) as warnings while allowing genuinely new namespaces to pass. This should help catch common mistakes without blocking intentional additions.

    If this approach aligns with your expectations, I'll proceed with the implementation and open a PR referencing this issue.

  6. asucodes commented on Oct 25, 2026

    @asucodes

    I'd like to take this on. I'll add a linter pass that validates nursery rule namespaces against the actual directory structure in capa-rules. The approach will be to extend the existing linter framework, add a check that compares each nursery rule's namespace prefix with its file path, and include test fixtures covering both valid and invalid cases (such as a nursery rule incorrectly claiming a top-level namespace). I'll make sure the implementation follows the patterns already established in the codebase and that CI passes cleanly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions