gqlprobe 1.0.0

Fast GraphQL security scanner - schema-aware introspection, injection, DoS, CSRF, auth bypass, SARIF
Documentation
//! gqlprobe  -  GraphQL security scanner
//!
//! A comprehensive GraphQL security testing tool that detects:
//! - Introspection queries
//! - Query depth `DoS` vulnerabilities
//! - Batching abuse
//! - Field suggestion enumeration
//! - Directive abuse
//!
//! ## Usage
//!
//! ```bash
//! # Basic scan
//! gqlprobe https://api.example.com/graphql
//!
//! # JSON output
//! gqlprobe https://api.example.com/graphql --output json
//!
//! # With custom timeout
//! gqlprobe https://api.example.com/graphql --timeout 60
//! ```

use std::io::Write;
use std::process;

use clap::Parser;
use tracing::error;

use gqlprobe::cli::{init_logging, output_findings, run_scan, validate_target, Cli};

#[tokio::main]
async fn main() {
    let cli = Cli::parse();

    if cli.list_modules {
        gqlprobe::cli::print_modules();
        return;
    }

    // Initialize logging
    init_logging(cli.verbose, cli.debug);

    let _ = writeln!(
        std::io::stderr().lock(),
        "gqlprobe v{} - GraphQL security scanner (authorized targets only)",
        env!("CARGO_PKG_VERSION")
    );

    // Validate target URL
    let target = match &cli.target {
        Some(t) => t.as_str(),
        None => {
            let _ = writeln!(
                std::io::stderr().lock(),
                "Error: URL is required. Example: gqlprobe https://api.example.com/graphql"
            );
            process::exit(2);
        }
    };

    if let Err(e) = validate_target(target) {
        error!("Invalid target URL: {}", e);
        let _ = writeln!(std::io::stderr().lock(), "Error: {e}");
        process::exit(2);
    }

    // Run the scan
    match run_scan(&cli).await {
        Ok(findings) => {
            // Output findings
            if let Err(e) = output_findings(&findings, cli.output) {
                error!("Failed to output findings: {}", e);
                process::exit(1);
            }

            // Exit with appropriate code
            let exit_code = i32::from(!findings.is_empty());

            if findings.is_empty() {
                let _ = writeln!(std::io::stdout().lock(), "\n✓ No security issues found");
            } else {
                let (mut critical, mut high, mut medium, mut low) = (0, 0, 0, 0);
                for f in &findings {
                    match f.severity() {
                        secfinding::Severity::Critical => critical += 1,
                        secfinding::Severity::High => high += 1,
                        secfinding::Severity::Medium => medium += 1,
                        secfinding::Severity::Low => low += 1,
                        _ => {}
                    }
                }

                let mut out = std::io::stdout().lock();
                let _ = writeln!(out, "\n⚠ Found {} security issues:", findings.len());
                if critical > 0 {
                    let _ = writeln!(out, "  Critical: {critical}");
                }
                if high > 0 {
                    let _ = writeln!(out, "  High: {high}");
                }
                if medium > 0 {
                    let _ = writeln!(out, "  Medium: {medium}");
                }
                if low > 0 {
                    let _ = writeln!(out, "  Low: {low}");
                }
            }

            process::exit(exit_code);
        }
        Err(e) => {
            error!("Scan failed: {}", e);
            let _ = writeln!(std::io::stderr().lock(), "Error: Scan failed: {e}");
            process::exit(1);
        }
    }
}