基于 Git 历史构建人员与文件归属风险图

该资料介绍一个 Python 工具,从 git 历史构建人员与文件二部图,计算归属风险并导出图数据供 Neo4j/Gephi 使用,同时按共享提交的 Jaccard 相似度构建文件共变图,并支持敏感路径规则与低巴士因子查询。

# 安全所有权地图 ## 概览 构建一个来自 git 历史的人和文件的双面图, 然后为 Neo4j/ Gephi 计算所有权风险和导出图件。 同时构建一个文件共变图( 共享承诺上的 Jaccard 相似性 ) , 通过它们如何一起移动而忽略大而吵闹的承诺来构建集群文件 。 ## 所需经费 - Python 3 (英语). - `networkx` (需要;默认启用社区检测) 安装方式 : ```bash pip install networkx ``` ## 工作流程 1. 范围 重播和时间窗口(可选) `--since/--until`). 2. 决定敏感性规则(使用默认或提供CSV配置). 3. 构建所有者地图 `scripts/run_ownership_map.py` (默认情况下是共同更改图表;使用) `--cochange-max-files` 以忽略超节点的操作). 4. 社区是默认计算; 图形ml输出是可选的(`--graphml`). 5. 查询产出为 `scripts/query_ownership.py` 用于捆绑的JSON切片. 6. 坚持和可视化(见 `references/neo4j-import.md`). 默认情况下,共变图忽略了常见的“glue”文件(lockfiles, `.github/*`,编辑器配置),因此集群反映的是实际的代码运动而不是共享的以下编辑。 `--cochange-exclude` 或 时 间 `--no-default-cochange-excludes`。默认情况下排除了依赖犯罪;以 `--no-default-author-excludes` 或通过 `--author-exclude-regex`. 如果您想要排除 Linux 的粘胶 `Kbuild` 从共变群集,通过 : ```bash python skills/skills/security-ownership-map/scripts/run_ownership_map.py \ --repo /path/to/linux \ --out ownership-map-out \ --cochange-exclude "**/Kbuild" ``` ## 快速启动 从 repo 根中运行 : ```bash python skills/skills/security-ownership-map/scripts/run_ownership_map.py \ --repo . \ --out ownership-map-out \ --since "12 months ago" \ --emit-commits ``` 默认: 排除作者身份、 作者日期和合并 。 使用 `--identity committer`, `--date-field committer`,或 `--include-merges` 如果需要的话。 示例(不包括重叠共变): ```bash python skills/skills/security-ownership-map/scripts/run_ownership_map.py \ --repo . \ --out ownership-map-out \ --cochange-exclude "**/Cargo.lock" \ --cochange-exclude "**/.github/**" \ --no-default-cochange-excludes ``` 社区是默认计算。 要禁用 : ```bash python skills/skills/security-ownership-map/scripts/run_ownership_map.py \ --repo . \ --out ownership-map-out \ --no-communities ``` ## 敏感性规则 默认情况下, 脚本标记为常见的 auth/ crypto/ 机密路径。 通过提供 CSV 文件来覆盖 : ``` # pattern,tag,weight **/auth/**,auth,1.0 **/crypto/**,crypto,1.0 **/*.pem,secrets,1.0 ``` 用它来 `--sensitive-config path/to/sensitive.csv`. ## 产出文物 `ownership-map-out/` 包含: - `people.csv` (节点:人) - `files.csv` (节点:文件) - `edges.csv` (对流:触摸) - `cochange_edges.csv` (file-to-file带Jaccard重量的共变边;省略 `--no-cochange`) - `summary.json` (安保所有权调查结果) - `commits.jsonl` (可选,如果 `--emit-commits`) - `communities.json` (在可用时默认从共变边值计算;包括 `maintainers` 每个社区; 禁用 `--no-communities`) - `cochange.graph.json` (NetworkX 节点链接 JSON 与 `community_id` + `community_maintainers`; 倒转至 `ownership.graph.json` 如果没有共变边). - `ownership.graphml` / `cochange.graphml` (可选,如果 `--graphml`) `people.csv` 包括基于作者承诺的时区检测偏移: `primary_tz_offset`, `primary_tz_minutes`,以及 `timezone_offsets`. ## LLM 查询助手 使用( U) `scripts/query_ownership.py` 返回小的、 JSON 绑定的切片,而不将完整的图表装入上下文。 实例: ```bash python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out people --limit 10 python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out files --tag auth --bus-factor-max 1 python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out person --person alice@corp --limit 10 python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out file --file crypto/tls python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out cochange --file crypto/tls --limit 10 python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out summary --section orphaned_sensitive_code python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out community --id 3 ``` 使用( U) `--community-top-owners 5` (默认)控制每个社区存储了多少维护者。 ## 基本安全查询 运行这些选项以解答共同的安全所有权问题, 并附带限制输出 : ```bash # Orphaned sensitive code (stale + low bus factor) python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out summary --section orphaned_sensitive_code # Hidden owners for sensitive tags python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out summary --section hidden_owners # Sensitive hotspots with low bus factor python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out summary --section bus_factor_hotspots # Auth/crypto files with bus factor <= 1 python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out files --tag auth --bus-factor-max 1 python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out files --tag crypto --bus-factor-max 1 # Who is touching sensitive code the most python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out people --sort sensitive_touches --limit 10 # Co-change neighbors (cluster hints for ownership drift) python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out cochange --file path/to/file --min-jaccard 0.05 --limit 20 # Community maintainers (for a cluster) python skills/skills/security-ownership-map/scripts/query_ownership.py --data-dir ownership-map-out community --id 3 # Monthly maintainers for the community containing a file python skills/skills/security-ownership-map/scripts/community_maintainers.py \ --data-dir ownership-map-out \ --file network/card.c \ --since 2025-01-01 \ --top 5 # Quarterly buckets instead of monthly python skills/skills/security-ownership-map/scripts/community_maintainers.py \ --data-dir ownership-map-out \ --file network/card.c \ --since 2025-01-01 \ --bucket quarter \ --top 5 ``` 注释: - 触摸默认到一个作者承诺( 而非每个文件) 。 使用 `--touch-mode file` 以计算每个文件的触摸。 - 使用( U) `--window-days 90` 或 时 间 `--weight recency --half-life-days 180` {\fn黑体\fs22\bord1\shad0\3aHBE\4aH00\fscx67\fscy66\2cHFFFFFF\3cH808080}去欢乐的 - 过滤 bot 与 `--ignore-author-regex '(bot|dependabot)'`. - 使用( U) `--min-share 0.1` 仅显示稳定的维护者。 - 使用( U) `--bucket quarter` 用于日历季度组合。 - 使用( U) `--identity committer` 或 时 间 `--date-field committer` 从作者属性切换。 - 使用( U) `--include-merges` 包括合并承诺(默认情况下除外)。 ### 摘要格式( 默认) 使用此结构,必要时添加字段: ```json { "orphaned_sensitive_code": [ { "path": "crypto/tls/handshake.rs", "last_security_touch": "2023-03-12T18:10:04+00:00", "bus_factor": 1 } ], "hidden_owners": [ { "person": "alice@corp", "controls": "63% of auth code" } ] } ``` ## 图表持久性 使用( U) `references/neo4j-import.md` 当您需要将 CSV 装入 Neo4j 时。它包括限制、导入 Cypher 和可视化提示。 ## 页:1 - `bus_factor_hotspots` 输入 `summary.json` 列出具有低总线因数的敏感文件; `orphaned_sensitive_code` 是 Stale 子集。 - 若为: `git log` 太大了 狭小了 `--since` 或 时 间 `--until`. - 比较 `summary.json` 反对CODEOWNERS 突出所有权漂移.

查看原始来源