Skip to main content

bastion_toolkit/
python_check.rs

1//! # Python Check - 推奨ライブラリ検証モジュール
2//!
3//! Pythonプロジェクトの requirements.txt をチェックし、
4//! セキュリティ上推奨されるライブラリが含まれているかを検証する。
5
6use anyhow::Result;
7use colored::*;
8use std::fs;
9
10/// 推奨するセキュリティライブラリのリスト
11const RECOMMENDED_PACKAGES: &[(&str, &str)] = &[
12    ("defusedxml", "XML処理の安全化(XXE攻撃対策)"),
13    ("bandit", "静的解析によるセキュリティ脆弱性検出"),
14    ("pip-audit", "依存関係の脆弱性チェック"),
15];
16
17/// requirements.txt を読み込み、推奨ライブラリの有無をチェックする
18pub fn check_secure_requirements(requirements_path: &str) -> Result<()> {
19    println!(
20        "\n{}",
21        "[+] Checking recommended security packages...".yellow()
22    );
23
24    let content = fs::read_to_string(requirements_path)?;
25    let content_lower = content.to_lowercase();
26
27    let mut missing_count = 0;
28
29    for (package, description) in RECOMMENDED_PACKAGES {
30        if content_lower.contains(&package.to_lowercase()) {
31            println!("  {} {} is present", "✓".green().bold(), package);
32        } else {
33            println!(
34                "  {} {} is missing — {}",
35                "✗".red().bold(),
36                package,
37                description
38            );
39            missing_count += 1;
40        }
41    }
42
43    if missing_count > 0 {
44        println!(
45            "\n  {} Run '{}' to generate recommended requirements.",
46            "TIP:".cyan().bold(),
47            "bastion init python".bold()
48        );
49    } else {
50        println!(
51            "  {}",
52            "All recommended security packages are present!".green()
53        );
54    }
55
56    Ok(())
57}