# htrgouvea/zarn architecture

> Architecture diagram of the GitHub repository htrgouvea/zarn, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/htrgouvea/zarn
- Repository: https://github.com/htrgouvea/zarn

## Overview

##### Main Components
- Main Entry Point: The CLI script (zarn.pl) that initiates the analysis process.
- Engine Subsystem: The core analysis logic encapsulated in the lib/Zarn/Engine directory. This includes:
- AST Module (AST.pm): Responsible for parsing source Perl files into AST representations.
- Source-to-Sink (Source_to_Sink.pm): Likely helps with mapping data flow from origins to endpoints.
- Taint Analysis (Taint_Analysis.pm): Implements taint tracking to assess if the recognized tokens in the AST may be exploitable.
- Helper Utilities: Found under lib/Zarn/Helper, which include:
- Files Utility (Files.pm): For file manipulations and reading source code.
- Rules Utility (Rules.pm): Loads and manages security rules.
- Sarif Utility (Sarif.pm): Converts the analysis results into SARIF format for integration with tools like GitHub CodeQL.
- Rules Repository: The YAML files in the “rules” directory (e.g., default.yml, prototype.yml) that define the various security rules.
- Testing Suite: The tests directory with .t files provides the test cases ensuring component functionality and integration.
- CI/CD Integration: The GitHub Actions workflows (in .github/workflows) that run linting, security scans, and tests on each commit or pull request.
- Build & Deployment: The Makefile, Dockerfile, and cpanfile indicate how the project is built, packaged, and deployed.

##### Relationships and Data Flow
- The analysis begins when the CLI script (zarn.pl) is invoked with command-line arguments (like source location, rule files, output file for SARIF reports).
- The script delegates work to the Engine components:
- The AST module parses the individual Perl files to build a syntax tree.
- The Taint Analysis module then performs the taint tracking on this AST to detect if data flows from source to sink in a potentially vulnerable way.
- The Source-to-Sink module may be used to figure out the propagation of data through the code.
- The Rules Helper module then loads the security rules from the rules directory and cross-references the AST tokens found during analysis.
- Once analysis is complete, the Sarif Helper module produces an output in SARIF format, which is then consumed either by the CLI output or integrated with GitHub Actions for sending results to GitHub Security Tools.
- The testing components ensure that each of the above functions operate correctly, simulating real-world files and rule processing.

##### Architectural Patterns and Technologies
- Modular Design: Separation of different responsibilities into discrete modules (Engine, Helper, Rules).
- Layered Architecture: There is a clear separation between the input (CLI), processing (AST, analysis engines), rule evaluation, and output (SARIF report generation).
- Extensibility: The rules are defined in YAML and easily extensible, and the Engine modules encapsulate different analysis strategies (with possible future enhancements like call graph analysis).
- CI/CD and Integration: Built-in GitHub Actions workflows ensure that the static analysis and testing are run automatically, reinforcing a secure development lifecycle.

##### Additional Notes
- Importance of capturing as many architectural elements as possible, separating the system into distinct components based on functionality.
- Annotations and clear labels should ensure that even someone unfamiliar with dynamic languages like Perl will understand the phases of static analysis (i.e., parsing, analysis, rule evaluation, reporting).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Interface"
        CLI["CLI Entry (zarn.pl)"]:::cli
    end

    subgraph "Engine Processing"
        AST["AST Parsing (lib/Zarn/Engine/AST)"]:::engine
        TAINT["Taint Analysis (lib/Zarn/Engine/Taint_Analysis)"]:::engine
        SINK["Source-to-Sink Analysis (lib/Zarn/Engine/Source_to_Sink)"]:::engine
        FUTURE["Future Enhancements (e.g., CallGraph Analysis)"]:::engine
    end

    subgraph "Helper Utilities"
        FILES["Files Utility (lib/Zarn/Helper/Files)"]:::helper
        RULES_HELPER["Rules Loader (lib/Zarn/Helper/Rules)"]:::helper
        SARIF["SARIF Generator (lib/Zarn/Helper/Sarif)"]:::helper
    end

    subgraph "Configuration & Testing"
        RULES_REPO["Rules Repository (YAML files)"]:::config
        TEST_SUITE["Testing Suite (.t files)"]:::testing
    end

    subgraph "CI/CD & Integration"
        CICD["CI/CD Integration (GitHub Actions)"]:::cicd
    end

    subgraph "Build & Deployment"
        MAKEFILE["Makefile"]:::build
        DOCKER["Dockerfile"]:::build
        CPANFILE["cpanfile"]:::build
    end

    %% Relationships and Data Flow
    CLI -->|"initiates"| AST
    FILES -->|"provides source"| AST
    AST -->|"outputs AST"| TAINT
    AST -->|"outputs AST"| SINK
    TAINT -->|"analysis results"| RULES_HELPER
    SINK -->|"analysis results"| RULES_HELPER
    RULES_REPO -->|"loads rules"| RULES_HELPER
    RULES_HELPER -->|"compiles findings"| SARIF
    SARIF -->|"reports"| CLI
    SARIF -->|"reports"| CICD
    CICD -->|"triggers tests"| TEST_SUITE
    CICD -->|"build process"| MAKEFILE
    CICD -->|"build process"| DOCKER
    CICD -->|"build process"| CPANFILE

    %% Click Events
    click CLI "https://github.com/htrgouvea/zarn/blob/main/zarn.pl"
    click AST "https://github.com/htrgouvea/zarn/blob/main/lib/Zarn/Engine/AST.pm"
    click TAINT "https://github.com/htrgouvea/zarn/blob/main/lib/Zarn/Engine/Taint_Analysis.pm"
    click SINK "https://github.com/htrgouvea/zarn/blob/main/lib/Zarn/Engine/Source_to_Sink.pm"
    click FILES "https://github.com/htrgouvea/zarn/blob/main/lib/Zarn/Helper/Files.pm"
    click RULES_HELPER "https://github.com/htrgouvea/zarn/blob/main/lib/Zarn/Helper/Rules.pm"
    click SARIF "https://github.com/htrgouvea/zarn/blob/main/lib/Zarn/Helper/Sarif.pm"
    click RULES_REPO "https://github.com/htrgouvea/zarn/tree/main/rules/"
    click TEST_SUITE "https://github.com/htrgouvea/zarn/tree/main/tests/"
    click CICD "https://github.com/htrgouvea/zarn/tree/main/.github/workflows/"
    click MAKEFILE "https://github.com/htrgouvea/zarn/tree/main/Makefile"
    click DOCKER "https://github.com/htrgouvea/zarn/tree/main/Dockerfile"
    click CPANFILE "https://github.com/htrgouvea/zarn/tree/main/cpanfile"

    %% Class Definitions for Colors
    classDef cli fill:#f9d5e5,stroke:#333,stroke-width:2px;
    classDef engine fill:#cbe86b,stroke:#333,stroke-width:2px;
    classDef helper fill:#f7a072,stroke:#333,stroke-width:2px;
    classDef config fill:#a2d2ff,stroke:#333,stroke-width:2px;
    classDef testing fill:#ffafcc,stroke:#333,stroke-width:2px;
    classDef cicd fill:#d0f4de,stroke:#333,stroke-width:2px;
    classDef build fill:#e0aaff,stroke:#333,stroke-width:2px;
```
